package org.openforis.calc.importer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.openforis.calc.io.flat.FlatRecord;
import org.openforis.calc.model.Interview;
import org.openforis.calc.model.InterviewCategoricalValue;
import org.openforis.calc.model.InterviewNumericValue;
import org.openforis.calc.persistence.ClusterDao;
import org.openforis.calc.persistence.InterviewCategoricalValueDao;
import org.openforis.calc.persistence.InterviewDao;
import org.openforis.calc.persistence.InterviewNumericValueDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Not thread-safe, callers must manage synchronization
 * 
 * @author G. Miceli
 * 
 */
@Component
public class InterviewImporter extends AbstractFlatFileImporter {

	private Integer surveyId;
	private Integer unitId;
	private List<Interview> interviews;
	private List<InterviewCategoricalValue> categoricalValues;
	private List<InterviewNumericValue> numericValues;
	
	@Autowired
	private InterviewDao interviewDao;
	@Autowired
	private InterviewCategoricalValueDao interviewCategoricalValueDao;
	@Autowired
	private InterviewNumericValueDao interviewNumericValueDao;
	@Autowired
	private ClusterDao clusterDao;

	public InterviewImporter() {
		setInsertFrequency(1000);
		setReportFrequency(1000);
	}
	
	@Override
	protected boolean processRecord(FlatRecord record) {
		if ( surveyId == null || unitId == null ) {
			throw new NullPointerException("Survey or unit ID not set");
		}
		// cluster code
		String clusterCode = record.getValue("cluster_id", String.class);
		Integer clusterId = getClusterId(clusterCode);
		if ( clusterCode != null && clusterId == null) {
			log.warn("Invalid cluster code: "+clusterCode);
			return false;
		}
		// interview no
		Integer interviewNo = record.getValue("id", Integer.class);
		if ( interviewNo == null ) {
			log.warn("Missing interview number!");
			return false;
		}
		// interview date
		Date interviewDate = record.getValue("interview_date", Date.class);
		
		Interview iv = new Interview();
		iv.setClusterId(clusterId);
		iv.setInterviewNo(interviewNo);
		iv.setInterviewDate(interviewDate);
		// TODO other fields
		
		return true;
//		p.setPlotNo(plotNo);
//		p.setSamplingPhase(phase);
//		p.setObsUnitId(unitId);
//		GeodeticCoordinate plotLocation = GeodeticCoordinate.toInstance(plotX, plotY, srsId);
//		p.setPlotLocation(plotLocation);
//		p.setGroundPlot(groundPlot);
//		p.setPermanentPlot(permanentPlot);
//		interviews.add(p);
	}

	private Integer getClusterId(String clusterCode) {
		if ( clusterCode == null ) {
			return null;
		} else {
			return clusterDao.getIdByKey(surveyId, clusterCode);
		}
	}

	@Override
	protected void performInserts() {
		interviewDao.insert(interviews);
		interviewCategoricalValueDao.insert(categoricalValues);
		interviewNumericValueDao.insert(numericValues);
		interviews.clear();
		categoricalValues.clear();
		numericValues.clear();
	}

	@Override
	protected void onStart() {
		numericValues = new ArrayList<InterviewNumericValue>();
		categoricalValues = new ArrayList<InterviewCategoricalValue>();
		interviews = new ArrayList<Interview>();
	}

	@Override
	protected void cleanup() {
		interviews = null;
		categoricalValues = null;
		numericValues = null;
	}

	public int getSurveyId() {
		return surveyId;
	}

	public void setSurveyId(int surveyId) {
		this.surveyId = surveyId;
	}

	public int getUnitId() {
		return unitId;
	}

	public void setUnitId(int unitId) {
		this.unitId = unitId;
	}
}

@@ -106,16 +106,73 @@
 .List;%0A%0A
+import org.openforis.calc.geospatial.GeodeticCoordinate;%0A
 import o
@@ -868,79 +868,30 @@
 ract
-FlatFileImporter %7B%0A%0A%09private Integer surveyId;%0A%09private Integer unitId;
+ObservationImporter %7B%0A
 %0A%09pr
@@ -1282,16 +1282,17 @@
 terDao;%0A
+%09
 %0A%09public
@@ -1444,122 +1444,8 @@
 ) %7B%0A
-%09%09if ( surveyId == null %7C%7C unitId == null ) %7B%0A%09%09%09throw new NullPointerException(%22Survey or unit ID not set%22);%0A%09%09%7D%0A
 %09%09//
@@ -1962,16 +1962,456 @@
 class);%0A
+%09%09// location%0A%09%09Double locationX = record.getValue(%22location_x%22, Double.class);%0A%09%09Double locationY = record.getValue(%22location_y%22, Double.class);%0A%09%09String srsId = record.getValue(%22location_srs_id%22, String.class);%0A%09%09if ( locationX == null %7C%7C locationY == null %7C%7C srsId == null ) %7B%0A%09%09%09log.warn(%22Missing or incomplete location%22);%0A%09%09%09return false;%0A%09%09%7D%0A%09%09GeodeticCoordinate location = GeodeticCoordinate.toInstance(locationX, locationX, srsId);%0A
 %09%09%0A%09%09Int
@@ -3034,24 +3034,50 @@
 IdByKey(
-s
+getObservationUnit().getS
 urveyId
+()
 , cluste
@@ -3689,243 +3689,7 @@
 ;%0A%09%7D
-%0A%0A%09public int getSurveyId() %7B%0A%09%09return surveyId;%0A%09%7D%0A%0A%09public void setSurveyId(int surveyId) %7B%0A%09%09this.surveyId = surveyId;%0A%09%7D%0A%0A%09public int getUnitId() %7B%0A%09%09return unitId;%0A%09%7D%0A%0A%09public void setUnitId(int unitId) %7B%0A%09%09this.unitId = unitId;%0A%09%7D
 %0A%7D%0A

Work on interview importer