
/*
 * $Id: ClockingEntryRecordStore.java,v 1.2 2003/06/16 13:31:27 tbayer Exp $
 *
 * Copyright (c) 2002 Orientation in Objects GmbH
 * Weinheimer Str. 68, D - 68309 Mannheim, Germany
 * All rights reserved.
 *
 * Reverse Engineering, Aendern und Erweitern dieser Software ist nur
 * mit ausdruecklicher schriftlicher Genehmigung von Orientation in
 * Objects gestattet.
 */

/*
   $Log: ClockingEntryRecordStore.java,v $
   Revision 1.2  2003/06/16 13:31:27  tbayer
   Umstellung auf JDK 1.4 Homepage

   Revision 1.1  2002/04/03 14:47:10  tbayer
   *** empty log message ***

 */
package de.oio.zeiterfassung.midp;

import java.io.IOException;

import java.util.Vector;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * MIDP RecordStore zum Speichern von ClockingEntry Objekten.
 *
 * @author $Author: tbayer $
 * @version $Revision: 1.2 $<br/>
 * $Date: 2003/06/16 13:31:27 $<br/>
 */
public class ClockingEntryRecordStore {

    private final String                    REC_STORE_NAME = "OIO_CLOCKING_ENTRY_RECORD_STORE";
    private static ClockingEntryRecordStore instance       = null;
    private RecordStore                     rs             = null;

    private ClockingEntryRecordStore() {}

    private void openRecordStore() throws RecordStoreException {
        rs = RecordStore.openRecordStore(REC_STORE_NAME, true);
    }

    private void closeRecordStore() throws RecordStoreException {
        rs.closeRecordStore();
    }

    public static ClockingEntryRecordStore getInstance() {

        if (instance == null) {
            instance = new ClockingEntryRecordStore();
        }

        return instance;
    }

    public void addClockingEntry(ClockingEntry clockingEntry) throws RecordStoreException {

        openRecordStore();

        byte[] data = null;

        try {
            data = clockingEntry.toByteArray();
        } catch (IOException e) {
            throw new RecordStoreException(e.getMessage());
        }

        rs.addRecord(data, 0, data.length);
        closeRecordStore();
    }

    public void removeAllEntries() throws RecordStoreException {
        RecordStore.deleteRecordStore(REC_STORE_NAME);
    }

    public Vector getAllEntries() throws RecordStoreException {

        openRecordStore();

        Vector vector = new Vector();

        try {
            for (int i = 1; i <= rs.getNumRecords(); i++) {
                vector.addElement(ClockingEntry.getInstanceFromByteArray(rs.getRecord(i)));
            }
        } catch (RecordStoreException rse) {
            rse.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        closeRecordStore();

        return vector;
    }

    public void dumpAllEntriesToSystemOut() throws RecordStoreException {

        openRecordStore();

        try {
            for (int i = 1; i <= rs.getNumRecords(); i++) {
                System.out.println(ClockingEntry.getInstanceFromByteArray(rs.getRecord(i)).toString());
            }
        } catch (RecordStoreException rse) {
            rse.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        closeRecordStore();
    }
}    // ClockingEntryRecordStore


