package net.sf.nfp.mini.dao;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.rms.RecordStoreException;
import net.sf.log.mobile.Log;
import net.sf.nfp.mini.data.Observation;
import net.sf.nfp.mini.data.Period;
import net.sf.nfp.mini.main.NFPControler;
import net.sf.nfp.mini.misc.Utils;
import net.sf.nfp.mini.model.SaveObservationModel;
public class LogicDAO {
/**
* @param observation
* @return saved observation or null if the observation will overwrite
* @throws RecordStoreException
* @throws IOException
*/
public static Observation addObservationToPeriod(Observation observation, Period[] periodRef, NFPControler controler) throws RecordStoreException, IOException {
Log.log("SaveObservationModel.addObservationToPeriod("+observation+","+periodRef[0]);
final PeriodDAO periodDAO = controler.getPeriodDAO();
final ObservationDAO observationDAO = controler.getObservationDAO();
Period period = periodRef[0];
if(period.getStart() == null) {
period.setStart(observation.getDate());
period.setEnd(observation.getDate());
}else{
if (observation.getDate().getTime() < period.getStart().getTime())
period.setStart(observation.getDate());
if (observation.getDate().getTime() > period.getEnd().getTime())
period.setEnd(observation.getDate());
if (Utils.getDaysBetween(period.getStart(), period.getEnd()) > SaveObservationModel.MAX_PERIOD_DAYS)
throw new IllegalArgumentException("${period.days.exceded} " + SaveObservationModel.MAX_PERIOD_DAYS);
}
period = periodDAO.persist(period);
observation = observationDAO.persist(observation);
Vector observationIds = periodDAO.getObservationIds(period);
observationIds.addElement(new Integer(observation.getId()));
periodDAO.setObservationIds(period, observationIds);
periodRef[0] = period;
Log.log("period:"+period);
return observation;
}
public static void removeObservation(NFPControler controler, Observation observation, Period period) throws RecordStoreException, IOException {
controler.getObservationDAO().delete(observation.getId());
Vector ids = controler.getPeriodDAO().getObservationIds(period);
ids.removeElement(new Integer(observation.getId()));
controler.getPeriodDAO().setObservationIds(period, ids);
}
}