Package net.sf.nfp.mini.model

Source Code of net.sf.nfp.mini.model.SaveObservationModel

package net.sf.nfp.mini.model;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Ticker;
import javax.microedition.rms.RecordStoreException;

import net.sf.log.mobile.Log;
import net.sf.mvc.mobile.Model;
import net.sf.mvc.mobile.Navigation;
import net.sf.mvc.mobile.command.NavigationCommand;
import net.sf.nfp.mini.dao.LogicDAO;
import net.sf.nfp.mini.dao.ObservationDAO;
import net.sf.nfp.mini.dao.PeriodDAO;
import net.sf.nfp.mini.data.Observation;
import net.sf.nfp.mini.data.Period;
import net.sf.nfp.mini.main.NFPControler;

public class SaveObservationModel implements Model {
  private final NFPControler controler;
  private NavigationCommand okCommand = null;
  private Alert view;
  public static int MAX_PERIOD_DAYS = 60;

  public SaveObservationModel(NFPControler controler) {
    this.controler = controler;
  }
 
  public void load(Object parameter) throws Exception {
    Log.log("SaveObservationModel.load()");
    Object[] params = (Object[]) parameter;
    Period period = (Period) params[0];
    Observation observation = (Observation) params[1];
    Log.log("period:"+period);
    Log.log("observ:"+observation);
    boolean newPeriodStarted = false;
    if (period == null) {
      period = new Period(observation.getDate());
      newPeriodStarted = true;
    } else if (observation.isBleeding()) {
      // only current period is "closed" when adding new bleeding
      // observations
      if (isCurrentPeriod(period) && !isBleeingContinuation(period, observation)) {
        period = new Period(observation.getDate());
        newPeriodStarted = true;
      }
    }
    Period[] ref = { period };
    observation = LogicDAO.addObservationToPeriod(observation, ref, controler);
    period = ref[0];
    if (newPeriodStarted)
      controler.getPeriodDAO().setCurrent(period);
   
    updateView(period, observation, newPeriodStarted);
  }

  private void updateView(Period period, Observation observation, boolean newPeriodStarted) {
    Log.log("SaveObservationModel.updateView("+period+","+observation+","+newPeriodStarted+")");
    StringBuffer msg = new StringBuffer("${observation.saved}:\n");
    msg.append(observation);
    if(newPeriodStarted) {
      msg.append("\n${new.period.started}");
      view.setTicker(new Ticker("${new.period.started}"));
    }
    view.setString(msg.toString());
    view.removeCommand(okCommand);
    Navigation navigation = new Navigation("graph", period, observation);
    okCommand = new NavigationCommand(navigation, "${OK}", Command.OK, 1);
    view.addCommand(okCommand);
  }
 

  private boolean isBleeingContinuation(Period period, Observation observation) throws RecordStoreException, IOException {
    Log.log("SaveObservationModel.isBleeingContinuation("+period+","+observation+")");
    final PeriodDAO periodDAO = controler.getPeriodDAO();
    final ObservationDAO observationDAO = controler.getObservationDAO();
    Vector ids = periodDAO.getObservationIds(period);
    for(int i=0;i<ids.size();++i) {
      Integer id = (Integer) ids.elementAt(i);
      Observation o = observationDAO.find(id.intValue());
      if(o.isBleeding() == false)
        if(o.getDate().getTime() < observation.getDate().getTime())
          //There is a non-bleeding observation before 'observation'
          return false;
    }
    return true;
  }

  private boolean isCurrentPeriod(Period period) throws RecordStoreException, IOException {
    Period current = controler.getPeriodDAO().getCurrent();
    if (current == null)
      return true;
    return current.equals(period);
 
 
  public void setView(Displayable v) {
    view = (Alert) v;
    view.setType(AlertType.INFO);
    view.setTimeout(Alert.FOREVER);
  }
}
TOP

Related Classes of net.sf.nfp.mini.model.SaveObservationModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.