Package net.sf.nfp.mini.model

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

package net.sf.nfp.mini.model;

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

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.rms.RecordStoreException;

import net.sf.log.mobile.Log;
import net.sf.mvc.mobile.Navigation;
import net.sf.mvc.mobile.command.ActionCommand;
import net.sf.mvc.mobile.command.NavigationCommand;
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;
import net.sf.nfp.mini.misc.Utils;
import net.sf.nfp.mini.view.InputView;

public class InputModel extends CommonModel {
  private InputView view;
  private Period period;

  public InputModel(NFPControler controler) {
    super(controler);   
    Log.log("InputModel.InputModel()");
  }

  public void setView(Displayable view) {
    Log.log("InputModel.setView("+view+") 1");
    super.setView(view);   
    Log.log("InputModel.setView("+view+") 2");
    this.view = (InputView) view;
    view.addCommand(new ActionCommand("${graph}", Command.SCREEN, 2) {
      public Navigation execute(Displayable d) throws Exception {
        return new Navigation("graph", period, null);
      }
    });
    view.addCommand(new ActionCommand("${save}", Command.BACK, 1) {
      public Navigation execute(Displayable d) throws Exception {
        InputView form = (InputView) d;
        Observation observation = form.getObservation();
        Observation existing = getObservationAt(observation.getDate());
        if (existing != null) {
          return new Navigation("confirm-overwrite", existing,
              new Navigation("save-observation", period, observation),
              new Navigation("graph", period, existing));
        }
        return new Navigation("save-observation", period, observation);
      }
    });
    view.addCommand(new NavigationCommand("mucus-editor","${mucus.editor}",Command.SCREEN, 50));
  }

  public void load(Object parameter) throws Exception {
    System.out.println("InputModel.load(" + parameter + ")");
    if (parameter == null) {
      period = controler.getPeriodDAO().getCurrent();
      view.setObservation(new Observation());
      view.refreshMucusList();
    } else {
      Object[] params = (Object[]) parameter;
      period = (Period) params[0];
      view.setObservation((Observation) params[1]);
    }
    if (period == null)
      view.setHeader("${new.period}");
    else
      view.setHeader(period.toString());
  }

  private Observation getObservationAt(Date date) throws RecordStoreException, IOException {
    System.out.println("InputModel.getObservationAt("+date+")");
   
    final PeriodDAO periodDAO = controler.getPeriodDAO();
    final ObservationDAO observationDAO = controler.getObservationDAO();

    if(period == null)
      return null;
    if (date.getTime() < period.getStart().getTime())
      return null;
    if (date.getTime() > period.getEnd().getTime())
      return null;
    Vector ids = periodDAO.getObservationIds(period);
    controler.progressListner.setMaxValue(ids.size());
    for (int i = 0; i < ids.size(); ++i) {
      int id = ((Integer) ids.elementAt(i)).intValue();
      Observation o = observationDAO.find(id);
      if (Utils.equals(date, o.getDate()))
        return o;
    }
    return null;
  }
}
TOP

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

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.