package net.sf.nfp.mini.model;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import net.sf.mvc.mobile.Model;
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.LogicDAO;
import net.sf.nfp.mini.data.Exporter;
import net.sf.nfp.mini.data.MucusRegistry;
import net.sf.nfp.mini.data.Observation;
import net.sf.nfp.mini.data.Period;
import net.sf.nfp.mini.main.NFPControler;
public class CommonModel implements Model {
private static final String CSV_PATH = System.getProperty("fileconn.dir.photos") + "${csv_name}.csv";
protected final NFPControler controler;
public CommonModel(NFPControler controler) {
this.controler = controler;
}
public void load(Object parameter) throws Exception {
}
public void setView(Displayable view) {
view.addCommand(new NavigationCommand("quit", "${quit}", Command.SCREEN, 9));
//#ifdef FILE_API
view.addCommand(new ActionCommand("${export}", Command.SCREEN, 8) {
public Navigation execute(Displayable d) throws Exception {
Exporter exporter = new Exporter();
javax.microedition.io.file.FileConnection fc =
(javax.microedition.io.file.FileConnection) Connector.open(CSV_PATH, Connector.READ_WRITE);
try {
if (fc.exists())
fc.delete();
fc.create();
exporter.exportToCSV(fc.openOutputStream(), controler);
} finally {
fc.close();
}
return null;
}
});
view.addCommand(new ActionCommand("${import}", Command.SCREEN, 8) {
public Navigation execute(Displayable d) throws Exception {
Exporter exporter = new Exporter();
javax.microedition.io.file.FileConnection fc =
(javax.microedition.io.file.FileConnection) Connector.open(CSV_PATH,
Connector.READ);
try {
exporter.importFromCSV(fc.openDataInputStream(), fc.fileSize(), controler);
} finally {
fc.close();
}
return null;
}
});
//#endif
//#ifdef DEBUG
view.addCommand(new ActionCommand("D:sample data", Command.SCREEN, 101) {
public Navigation execute(Displayable d) throws Exception {
Calendar date = Calendar.getInstance();
Random random = new Random();
controler.progressListner.setMaxValue(30);
Observation o = null;
Period period = new Period(new Date());
controler.getPeriodDAO().setObservationIds(period, new Vector());
for(int i=1; i < 5; ++i) {
date.set(Calendar.DAY_OF_MONTH, i);
o = new Observation(date.getTime(),
Observation.NO_TEMPERATURE,
MucusRegistry.instance().find(1),
true, random.nextInt() % 5 == 0, "");
LogicDAO.addObservationToPeriod(o, new Period[]{period}, controler);
controler.progressListner.setValue(i);
}
for (int i = 5; i < 30; ++i) {
date.set(Calendar.DAY_OF_MONTH, i);
o = new Observation(date.getTime(),
3620 + random.nextInt() % 60,
MucusRegistry.instance().find(i % 3 + 1),
false, random.nextInt() % 10 == 0, "");
LogicDAO.addObservationToPeriod(o, new Period[]{period}, controler);
controler.progressListner.setValue(i);
}
return new Navigation("graph", period, o);
}
});
view.addCommand(new ActionCommand("D:reset rms", Command.SCREEN, 102){
public Navigation execute(Displayable d) throws Exception {
controler.getPeriodDAO().deleteAll();
controler.getObservationDAO().deleteAll();
return new Navigation("quit",null);
}
});
//#endif
}
}