FileDocCategorySizeDatePackage
BookStore.javaAPI DocExample7038Sun Nov 04 15:20:06 GMT 2001ora.ch6

BookStore

public class BookStore extends Object implements javax.microedition.rms.RecordComparator, javax.microedition.rms.RecordFilter

Fields Summary
private static final String
STORE_NAME
private javax.microedition.rms.RecordStore
store
private String
searchISBN
Constructors Summary
public BookStore()


    // Creates a bookstore and opens it
      
        try {
            store = RecordStore.openRecordStore(STORE_NAME, true);
        } catch (RecordStoreException ex) {
            System.err.println(ex);
        }
    
Methods Summary
public voidaddRecordListener(javax.microedition.rms.RecordListener l)

        if (store != null) {
            store.addRecordListener(l);
        }
    
public voidclose()

        if (store != null) {
            store.closeRecordStore();
        }
    
public intcompare(byte[] book1, byte[] book2)

        try {
            DataInputStream stream1 =
                new DataInputStream(new ByteArrayInputStream(book1));
            DataInputStream stream2 =
                new DataInputStream(new ByteArrayInputStream(book2));

            // Match based on the ISBN, but sort based on the title.
            String isbn1 = stream1.readUTF();
            String isbn2 = stream2.readUTF();
            if (isbn1.equals(isbn2)) {
                return RecordComparator.EQUIVALENT;
            }
            String title1 = stream1.readUTF();
            String title2 = stream2.readUTF();
            int result = title1.compareTo(title2);
            if (result == 0) {
                return RecordComparator.EQUIVALENT;
            }
            return result < 0 ? RecordComparator.PRECEDES :
                                RecordComparator.FOLLOWS;
        } catch (IOException ex) {
            return RecordComparator.EQUIVALENT;
        }
    
public voiddeleteBook(BookInfo bookInfo)

        if (store != null) {
            store.deleteRecord(bookInfo.id);
        }
    
public intgetBookCount()

        if (store != null) {
            return store.getNumRecords();
        }
        return 0;
    
public BookInfogetBookInfo(java.lang.String isbn)

        BookInfo bookInfo = null;
        searchISBN = isbn; 
        
        // Look for a book with the given ISBN
        RecordEnumeration enum = store.enumerateRecords(
                                        this, null, false);
        
        // If found, get its identifier and
        // fetch its BookInfo object
        if (enum.numRecords() > 0) {
            int id = enum.nextRecordId();
            bookInfo = getBookInfo(id);
        }
        
        // Release the enumeration
        enum.destroy();
        
        return bookInfo;
    
public BookInfogetBookInfo(int id)

        byte[] bytes = store.getRecord(id);
        DataInputStream is = new DataInputStream(
                            new ByteArrayInputStream(bytes));

        String isbn = is.readUTF();
        BookInfo info = new BookInfo(isbn);
        info.id = id;
        info.title = is.readUTF();
        info.ranking = is.readInt();
        info.reviews = is.readInt();
        info.lastRanking = is.readInt();
        info.lastReviews = is.readInt();

        return info;
    
public javax.microedition.rms.RecordEnumerationgetBooks()

        if (store != null) {
            return store.enumerateRecords(null, this, false);
        }
        return null;
    
public booleanmatches(byte[] book)

        if (searchISBN != null) {
            try {
                DataInputStream stream =
                    new DataInputStream(new ByteArrayInputStream(book));

                // Match based on the ISBN.
                return searchISBN.equals(stream.readUTF());
            } catch (IOException ex) {
                System.err.println(ex);
            }
        }

        // Default is not to match
        return false;
    
public voidremoveRecordListener(javax.microedition.rms.RecordListener l)

        if (store != null) {
            store.removeRecordListener(l);
        }
    
public voidsaveBookInfo(BookInfo bookInfo)

        if (store != null) {
            searchISBN = bookInfo.getIsbn();
            RecordEnumeration enum = store.enumerateRecords(
                                        this, null, false);
            if (enum.numRecords() > 0) {
                // A matching record exists. Set the id
                // of the BookInfo to match the existing record
                bookInfo.id = enum.nextRecordId();
                byte[] bytes = toByteArray(bookInfo);
                store.setRecord(bookInfo.id, bytes, 0, bytes.length);
            } else {
                // Create a new record
                bookInfo.id = store.getNextRecordID();
                byte[] bytes = toByteArray(bookInfo);
                store.addRecord(bytes, 0, bytes.length);
            }

            // Finally, destroy the RecordEnumeration
            enum.destroy();
        }
    
private byte[]toByteArray(BookInfo bookInfo)

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream os = new DataOutputStream(baos);

        os.writeUTF(bookInfo.isbn);
        os.writeUTF(bookInfo.title == null ? "" : bookInfo.title);
        os.writeInt(bookInfo.ranking);
        os.writeInt(bookInfo.reviews);
        os.writeInt(bookInfo.lastRanking);
        os.writeInt(bookInfo.lastReviews);

        return baos.toByteArray();