package org.molgenis.animaldb.convertors.locations;_x000D_
_x000D_
import java.io.File;_x000D_
import java.util.ArrayList;_x000D_
import java.util.Date;_x000D_
import java.util.List;_x000D_
_x000D_
import org.molgenis.animaldb.commonservice.CommonService;_x000D_
import org.molgenis.framework.db.Database;_x000D_
import org.molgenis.framework.security.Login;_x000D_
import org.molgenis.pheno.Location;_x000D_
import org.molgenis.util.CsvFileReader;_x000D_
import org.molgenis.util.Tuple;_x000D_
_x000D_
public class ImportAteLocations_x000D_
{_x000D_
	private Database db;_x000D_
	private CommonService ct;_x000D_
	private List<String> seenLocs = new ArrayList<String>();_x000D_
	private String userName;_x000D_
_x000D_
	public ImportAteLocations(Database db, Login login) throws Exception_x000D_
	{_x000D_
		this.db = db;_x000D_
		ct = CommonService.getInstance();_x000D_
		ct.setDatabase(this.db);_x000D_
		userName = login.getUserName();_x000D_
	}_x000D_
_x000D_
	public void doImport(String filename) throws Exception_x000D_
	{_x000D_
		final String investigationName = ct.getOwnUserInvestigationNames(userName).get(0);_x000D_
_x000D_
		File file = new File(filename);_x000D_
		CsvFileReader reader = new CsvFileReader(file);_x000D_
		for (Tuple tuple : reader)_x000D_
		{_x000D_
			// gebouw nr -> name of building_x000D_
			String buildingName = tuple.getString("gebouw nr");_x000D_
			if (!seenLocs.contains(buildingName))_x000D_
			{_x000D_
				seenLocs.add(buildingName);_x000D_
				Location newBuilding = new Location();_x000D_
				newBuilding.setName(buildingName);_x000D_
				newBuilding.setInvestigation_Name(investigationName);_x000D_
				db.add(newBuilding);_x000D_
			}_x000D_
			// etage_x000D_
			String floor = tuple.getString("etage");_x000D_
			// kamer nr_x000D_
			String room = tuple.getString("kamer nr");_x000D_
			// omschrijving -> skip_x000D_
_x000D_
			// Make location and link to building_x000D_
			Location newLoc = new Location();_x000D_
			newLoc.setName(floor + "." + room);_x000D_
			newLoc.setInvestigation_Name(investigationName);_x000D_
			db.add(newLoc);_x000D_
			db.add(ct.createObservedValueWithProtocolApplication(investigationName, new Date(), null,_x000D_
					"SetSublocationOf", "Location", newLoc.getName(), null, buildingName));_x000D_
		}_x000D_
	}_x000D_
_x000D_
}_x000D_

// omschrijving -> skip_x000D_
package org.molgenis.animaldb.convertors.locations;_x000D_
_x000D_
import java.io.File;_x000D_
import java.util.ArrayList;_x000D_
import java.util.Date;_x000D_
import java.util.List;_x000D_
_x000D_
import org.molgenis.animaldb.commonservice.CommonService;_x000D_
import org.molgenis.framework.db.Database;_x000D_
import org.molgenis.framework.security.Login;_x000D_
import org.molgenis.pheno.Location;_x000D_
import org.molgenis.util.CsvFileReader;_x000D_
import org.molgenis.util.Tuple;_x000D_
_x000D_
public class ImportAteLocations_x000D_
{_x000D_
	private Database db;_x000D_
	private CommonService ct;_x000D_
	private List<String> seenLocs = new ArrayList<String>();_x000D_
	private String userName;_x000D_
_x000D_
	public ImportAteLocations(Database db, Login login) throws Exception_x000D_
	{_x000D_
		this.db = db;_x000D_
		ct = CommonService.getInstance();_x000D_
		ct.setDatabase(this.db);_x000D_
		userName = login.getUserName();_x000D_
	}_x000D_
_x000D_
	public void doImport(String filename) throws Exception_x000D_
	{_x000D_
		final String investigationName = ct.getOwnUserInvestigationNames(userName).get(0);_x000D_
_x000D_
		File file = new File(filename);_x000D_
		CsvFileReader reader = new CsvFileReader(file);_x000D_
		for (Tuple tuple : reader)_x000D_
		{_x000D_
			// gebouw nr -> name of building_x000D_
			String buildingName = tuple.getString("gebouw nr");_x000D_
			if (!seenLocs.contains(buildingName))_x000D_
			{_x000D_
				seenLocs.add(buildingName);_x000D_
				Location newBuilding = new Location();_x000D_
				newBuilding.setName(buildingName);_x000D_
				newBuilding.setInvestigation_Name(investigationName);_x000D_
				db.add(newBuilding);_x000D_
			}_x000D_
			// etage_x000D_
			String floor = tuple.getString("etage");_x000D_
			// kamer nr_x000D_
			String room = tuple.getString("kamer nr");_x000D_
			// omschrijving -><SUF>
_x000D_
			// Make location and link to building_x000D_
			Location newLoc = new Location();_x000D_
			newLoc.setName(floor + "." + room);_x000D_
			newLoc.setInvestigation_Name(investigationName);_x000D_
			db.add(newLoc);_x000D_
			db.add(ct.createObservedValueWithProtocolApplication(investigationName, new Date(), null,_x000D_
					"SetSublocationOf", "Location", newLoc.getName(), null, buildingName));_x000D_
		}_x000D_
	}_x000D_
_x000D_
}_x000D_
