package de.dhbw.humbuch.viewmodel;

import java.io.ByteArrayOutputStream;

import com.google.inject.Inject;
import com.vaadin.ui.Upload;

import de.davherrmann.mvvm.ActionHandler;
import de.davherrmann.mvvm.BasicState;
import de.davherrmann.mvvm.State;
import de.davherrmann.mvvm.annotations.HandlesAction;
import de.davherrmann.mvvm.annotations.ProvidesState;
import de.dhbw.humbuch.model.DAO;
import de.dhbw.humbuch.model.entity.Grade;
import de.dhbw.humbuch.model.entity.Parent;
import de.dhbw.humbuch.model.entity.Student;

public class ImportViewModel {

	public interface ImportResult extends State<String> {}
		
	public interface DoImportStudents extends ActionHandler {}
	
	@ProvidesState(ImportResult.class)
	public final BasicState<String> importResult = new BasicState<String>(String.class);

	
	private DAO<Student> daoStudent;
	private DAO<Grade> daoGrade;
	private DAO<Parent> daoParent;

	/**
	 * Constructor
	 * 
	 * @param daoTeachingMaterial
	 *            DAO implementation to access TeachingMaterial entities
	 */
	@Inject
	public ImportViewModel(DAO<Student> daoStudent, DAO<Grade> daoGrade, DAO<Parent> daoParent) {
		this.daoStudent = daoStudent;
		this.daoGrade = daoGrade;
		this.daoParent = daoParent;
	}
	
	@HandlesAction(DoImportStudents.class)
	public void doImportStudents(Upload upload){
		
	}
	
	public DAO<Student> getDAOStudent(){
		return this.daoStudent;
	}
	
	public DAO<Grade> getDAOGrade(){
		return this.daoGrade;
	}
	
	public DAO<Parent> getDAOParent(){
		return this.daoParent;
	}
	
	public void setImportResult(String importResult){
		this.importResult.set(importResult);
	}
	/**
	 * Receives the OutputStream provided by an upload.
	 * @param outputStream
	 */
	public void receiveUploadByteOutputStream(ByteArrayOutputStream outputStream) {
		
	
	}
}

@@ -53,28 +53,142 @@
 yteArray
-OutputStream
+InputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.InputStreamReader;%0A%0Aimport au.com.bytecode.opencsv.CSVReader
 ;%0A%0Aimpor
@@ -633,16 +633,56 @@
 Student;
+%0Aimport de.dhbw.humbuch.util.CSVHandler;
 %0A%0Apublic
@@ -1936,16 +1936,221 @@
 am) %7B%0A%09%09
-%0A%09
+InputStreamReader inputStream = new InputStreamReader(new ByteArrayInputStream(outputStream.toByteArray()));%0A%09%09CSVReader reader = new CSVReader(inputStream);%0A%09%09CSVHandler.createStudentObjectsFromCSV(reader);
 %0A%09%7D%0A%7D%0A

Put input stream from the upload to csv reader