*/
public void importFromCSV(InputStream stream, long size, NFPControler controler) throws Exception {
Log.log("Exporter.importFromCSV() "+stream.available());
InputStreamReader reader = new InputStreamReader(stream);
Log.log("Exporter.importFromCSV() "+stream.available());
PeriodDAO periodDAO = controler.getPeriodDAO();
ObservationDAO observationDAO = controler.getObservationDAO();
Period period = null;
controler.progressListner.setMaxValue((int) size);
long left = size;
try {
left -= readLine(reader).length(); // read CSV header
periodDAO.deleteAll();
observationDAO.deleteAll();
String currentPeriod = null;
String[] fields = new String[10];
for (int lineNo = 1;; ++lineNo) {
StringBuffer line = readLine(reader);
if (line == null)
break;
left -= line.length();
for (int i = 0; i < fields.length; ++i)
fields[i] = grabField(line);
if (!fields[0].equals(currentPeriod)) {
if (period != null)
periodDAO.persist(period);
period = new Period();
currentPeriod = fields[0];
}
Observation observation = new Observation(
parseCSVDate(fields[2]),
parseCSVTemperature(fields[3]),
MucusRegistry.instance().find(Integer.parseInt(fields[4])),
parseCSVBoolean(fields[7]),
parseCSVBoolean(fields[8]),
fields[9]);
LogicDAO.addObservationToPeriod(observation, new Period[]{period}, controler);
controler.progressListner.setValue((int) (size - left));
}
} finally {
reader.close();
stream.close();
}
if(period != null)
periodDAO.setCurrent(period);
}