Storage.java

/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 
package com.nokia.example.bcexchanger;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

/**
 * 
 * This class abstracts an RMS system for storing the uid of the
 * contact entry from the address book, selected by user as own entry.
 * 
 */
public class Storage {

    /**
     * Gets the stored uid
     * 
     * @return string containing uid of the the contact entry from the
     *         address book, selected by user as own entry or null if no
     *         contact is previously selected
     * @exception Exception -
     *              in case of errors occur
     */
    static public String getBCUID() throws Exception {
        String uid = null;
        RecordStore db = RecordStore.openRecordStore("uid", true);

        RecordEnumeration e = db.enumerateRecords(null, null, false);

        if (!e.hasNextElement()) {
            uid = null;
        } else {
            byte[] record = e.nextRecord();
            if (record == null) {
                uid = null;
            } else {
                uid = new String(record);
            }
        }
        db.closeRecordStore();

        return uid;
    }

    /**
     * Stores the uid
     * 
     * @param uid -
     *          string containing uid of the the contact entry from the
     *          address book, selected by user as own entry
     * @exception Exception -
     *              in case of errors occur
     */
    static public void setBCUID(String uid) throws Exception {
        RecordStore db = RecordStore.openRecordStore("uid", true);

        // delete all records
        if (db.getNumRecords() != 0) {
            db.closeRecordStore();
            RecordStore.deleteRecordStore("uid");
            db = RecordStore.openRecordStore("uid", true);
        }

        byte[] record = uid.getBytes();
        db.addRecord(record, 0, record.length);

        db.closeRecordStore();
    }
}