Package net.sf.nfp.mini.dao

Source Code of net.sf.nfp.mini.dao.PeriodDAO

package net.sf.nfp.mini.dao;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Vector;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

import net.sf.log.mobile.Log;
import net.sf.nfp.mini.data.Period;

public class PeriodDAO extends AbstractDAO {
  private RecordStore periodRMS = null;
  private RecordStore currentPeriodRMS = null;
  private RecordStore dataRMS = null;
 
  protected RecordStore getCurrentPeriodRMS() throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException, RecordStoreNotOpenException {
    if(currentPeriodRMS == null) {
      try {
        currentPeriodRMS = RecordStore.openRecordStore("current-period", false);
      }catch(RecordStoreNotFoundException ex) {
        //first run
        currentPeriodRMS = RecordStore.openRecordStore("current-period", true);
        currentPeriodRMS.addRecord(null, 0, 0);
      }
    }
    return currentPeriodRMS;
  }
 
  protected RecordStore getBaseRMS() throws RecordStoreException {
    return periodRMS = lazyOpenRMS(periodRMS, "periods");
  }
   
  protected RecordStore getDataRMS() throws RecordStoreException {
    if(dataRMS == null)
      dataRMS = RecordStore.openRecordStore("period-data", true);
    return dataRMS;
  }

  public Period persist(Period period) throws RecordStoreException, IOException {
    return (Period) super.persist(period);
  }
   
  public Period find(int id) throws RecordStoreException, IOException {
    return (Period) super.find(id, new Period());
  }
 
  public Period getCurrent() throws RecordStoreException, IOException {
    Log.log("PeriodDAO.getCurrent()");
    if(getCurrentPeriodRMS().getNumRecords()!=1)
      return null;
    byte[] bytes = getCurrentPeriodRMS().getRecord(1);
    if(bytes == null || bytes.length == 0) //not set
      return null;
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
    try {
      int periodId = in.readInt();
      Log.log("PeriodDAO.getCurrent(), periodId="+periodId);
      return find(periodId);
    }finally{
      in.close();
    }
  }
 
  public void setCurrent(Period period) throws RecordStoreException, IOException {
    Log.log("PeriodDAO.setCurrent("+period.getId()+")");
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(bout);
    try{
      out.writeInt(period.getId());
      byte[] bytes = bout.toByteArray();
      Log.log("PeriodDAO.setCurrent(): bout.length="+bytes.length);
      getCurrentPeriodRMS().setRecord(1, bytes, 0, bytes.length);
    }finally{
      out.close();
    }
  }

  public Vector getObservationIds(Period period) throws RecordStoreException, IOException {
    Vector ids = new Vector();
    if(period.getDataId() == 0) //not set
      return ids;
    byte[] bytes = getDataRMS().getRecord(period.getDataId());
    if(bytes == null)
      return ids;
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
    while(in.available() > 0)
      ids.addElement(new Integer(in.readInt()));
    return ids;
  }
 
  public void setObservationIds(Period period, Vector observationsIds) throws IOException, RecordStoreException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(bout);
    for(int i=0;i<observationsIds.size();++i) {
      Integer id = (Integer) observationsIds.elementAt(i);
      out.writeInt(id.intValue());
    }
    out.close();
    byte[] bytes = bout.toByteArray();
    if(period.getDataId() == 0) //not set
      period.setDataId(getDataRMS().addRecord(bytes, 0, bytes.length));
    else //update
      getDataRMS().setRecord(period.getDataId(), bytes, 0, bytes.length);
  }
 
  public void close() throws RecordStoreException {
    currentPeriodRMS.closeRecordStore();
    periodRMS.closeRecordStore();
    dataRMS.closeRecordStore();
  }

  public Vector findByYear(final int year) throws RecordStoreException, IOException {
    Vector results = new Vector();
    Calendar cal = Calendar.getInstance();
    RecordEnumeration enumeration = getBaseRMS().enumerateRecords(null, null, false);
    boolean include = false;
    while (enumeration.hasNextElement()) {
      Period period = find(enumeration.nextRecordId());
      include = false;
      cal.setTime(period.getStart());
      if(year == cal.get(Calendar.YEAR))
        include = true;
      cal.setTime(period.getEnd());
      if(year == cal.get(Calendar.YEAR))
        include = true;
      if(include)
        results.addElement(period);
    }
    return results;
  }

  public Vector findAll() throws RecordStoreException, IOException {
    Vector all = new Vector();
    RecordEnumeration enumeration = getBaseRMS().enumerateRecords(null, null, false);
    while (enumeration.hasNextElement()) {
      all.addElement(find(enumeration.nextRecordId()));
    }
    return all;
  }

  public void deleteAll() throws RecordStoreException {
    deleteAll(getBaseRMS());
    deleteAll(getDataRMS());
    deleteAll(getCurrentPeriodRMS());
    periodRMS = null;
    dataRMS = null;
    currentPeriodRMS = null;
  }
}
TOP

Related Classes of net.sf.nfp.mini.dao.PeriodDAO

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.