FileDocCategorySizeDatePackage
LocationPersistentStorage.javaAPI DocphoneME MR2 API (J2ME)17889Wed May 02 18:00:40 BST 2007com.sun.j2me.location

LocationPersistentStorage

public class LocationPersistentStorage extends Object
This class is an implementation of the persistent storage to store landmarks.

Fields Summary
private static final int
suiteID
private static final String
prefixLandmarksFile
private static final String
prefixCategoriesFile
private static final String
defaultLandmarksFile
private static final String
defaultCategoriesFile
private static com.sun.midp.security.SecurityToken
token
private static LocationPersistentStorage
storage
Constructors Summary
private LocationPersistentStorage()

    
Methods Summary
public synchronized voidaddCategory(java.lang.String categoryName, java.lang.String storeName)

        Vector catVector = getCategories(storeName);
	if (catVector.indexOf(categoryName) != -1) {
            throw new IllegalArgumentException("Category `" + categoryName +
                            "' already exists in LandmarkStore " + storeName);
        }
	try {
	    RecordStoreImpl categoryStore =
	        RecordStoreImpl.openRecordStore(token, suiteID,
		    getCategoriesFileName(storeName), true);
	    byte[] bytesCatName = categoryName.getBytes();
	    categoryStore.addRecord(bytesCatName, 0, bytesCatName.length);
	    categoryStore.closeRecordStore();
	} catch (RecordStoreException e) {
	    throw new IOException(e.getMessage());
	}
    
public synchronized voidaddLandmark(java.lang.String storeName, LandmarkImpl landmark, java.lang.String category)

        try {
            if ((landmark.getRecordId()) > -1 &&
                (landmark.getStoreName() == storeName)) {
                landmark.addCategory(category);
                updateLandmark(storeName, landmark);
                return;
            }
        } catch (LandmarkException e) {
            // if it is happened, just continue to add Landmark
        }

        // new landmark will belong to specified category only
        landmark.removeCategories();
        if (category != null) {
            if (!getCategories(storeName).contains(category)) {
                throw new IllegalArgumentException("The category: " +
                    category + " was not added to this landmark store");
            }
            landmark.addCategory(category);
        } else {
            landmark.addCategory("");
        }
        byte[] data = landmark.serialize();
        landmark.setRecordId(addLandmark(storeName, data));
        landmark.setStoreName(storeName);
    
public synchronized intaddLandmark(java.lang.String storeName, byte[] lm)

	try {
	    int recordID = -1;
	    RecordStoreImpl landmarkStore =
		RecordStoreImpl.openRecordStore(token, suiteID,
		    getLandmarksFileName(storeName), false);
	    recordID = landmarkStore.addRecord(lm, 0, lm.length);
	    landmarkStore.closeRecordStore();
	    return recordID;
	} catch (RecordStoreException e) {
	    throw new IOException(e.getMessage());
	}
    
public synchronized voidaddStoreName(java.lang.String storeName)

	RecordStoreImpl landmarkStore = null;
	try {
            landmarkStore = RecordStoreImpl.openRecordStore(token, 
		suiteID, getLandmarksFileName(storeName), true);
	    landmarkStore.closeRecordStore();
	} catch (RecordStoreException e) {
	    throw new IOException(e.getMessage());
	}
    
public synchronized voiddeleteCategory(java.lang.String categoryName, java.lang.String storeName)

        Enumeration e = getLandmarksEnumeration(storeName, categoryName, null, 
						 -90, 90, -180, 180);
        if (e != null) {
            while (e.hasMoreElements()) {
                LandmarkImpl m = (LandmarkImpl)e.nextElement();
                removeLandmarkFromCategory(storeName, m, categoryName);
            }
        }
        Vector catVector = getCategories(storeName);
        int ind = catVector.indexOf(categoryName);
        if (ind != -1) { // remove a category
            try {
                RecordStoreImpl categoryStore =
                    RecordStoreImpl.openRecordStore(token, suiteID,
                        getCategoriesFileName(storeName), false);
                int[] recordIDs = categoryStore.getRecordIDs();
                for (int i = 0; i < recordIDs.length; i++) {
                    int recID = recordIDs[i];
                    int recSize = categoryStore.getRecordSize(recID);
                    byte[] record = new byte[recSize];
                    categoryStore.getRecord(recID, record, 0);
                    if (categoryName.equals(new String(record))) {
                        // category for deleting is found - delete it
                        categoryStore.deleteRecord(recID);
                        break;
                    }
                }
                categoryStore.closeRecordStore();
            } catch (RecordStoreException ex) {
                throw new IOException(ex.getMessage());
            }
        }
    
public synchronized voiddeleteLandmark(java.lang.String storeName, LandmarkImpl lm)


        // If the store names are not the same or
        // they are the same and both are null (for the default LMS)
        // but the landmark does not belong to a LMS
        if ((lm.getStoreName() != storeName) ||
                    (lm.getStoreName() == null && lm.getRecordId() == -2)) {
            throw new LandmarkException("This landmark belongs to a " +
		"different store: " + lm.getStoreName());
	}
        if (lm.getRecordId() > -1) {
            deleteLandmark(storeName, lm.getRecordId());
        }
        lm.setRecordId(-1);
        
    
public synchronized voiddeleteLandmark(java.lang.String storeName, int recordId)

	RecordStoreImpl landmarkStore = null;
	try {
	    landmarkStore = RecordStoreImpl.openRecordStore(token, suiteID,
		getLandmarksFileName(storeName), false);
	    landmarkStore.deleteRecord(recordId);
	    landmarkStore.closeRecordStore();
	} catch (InvalidRecordIDException e) {
	    // landmark was not found; quit silently
	    try {
		landmarkStore.closeRecordStore();
	    } catch (RecordStoreException x) {
		throw new IOException(x.getMessage());
	    }
	} catch (RecordStoreException e) {
	    throw new IOException(e.getMessage());
	}
    
public synchronized java.util.VectorgetCategories(java.lang.String storeName)

        Vector categories = new Vector();
	RecordStoreImpl categoryStore = null;
	try {
	    // open a file contains categories
	    categoryStore = RecordStoreImpl.openRecordStore(token, 
	        suiteID, getCategoriesFileName(storeName), false);
	    int[] recordIDs = categoryStore.getRecordIDs();
	    for (int i = 0; i < recordIDs.length; i++) {
	        int recID = recordIDs[i];
		int recSize = categoryStore.getRecordSize(recID);
		byte[] record = new byte[recSize];
		categoryStore.getRecord(recID, record, 0);
		categories.addElement(new String(record));
	    }
	    categoryStore.closeRecordStore();
	} catch (RecordStoreNotFoundException e) {
	    // categories were not found; return empty vector
	} catch (RecordStoreException e) {
	    throw new IOException(e.getMessage());
	}
        return categories;
    
private static java.lang.StringgetCategoriesFileName(java.lang.String storeName)

	if (storeName == null) {
	    return defaultCategoriesFile;
	}
	return prefixCategoriesFile + storeName;
    
public static com.sun.j2me.location.LocationPersistentStoragegetInstance()


    // JAVADOC COMMENT ELIDED
        
        if (storage == null) {
            storage = new LocationPersistentStorage();
            try {
                storage.addStoreName(null);
            } catch (IOException ex) {
            }
        }
        return storage;
    
public synchronized byte[]getLandmarks(java.lang.String storeName)

	try {
	    byte[] returnValue = null;
	    RecordStoreImpl landmarkStore = null;
	    landmarkStore = RecordStoreImpl.openRecordStore(token, suiteID,
	        getLandmarksFileName(storeName), false);
	    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
	    DataOutputStream stream = 
		new DataOutputStream((OutputStream)byteArrayOut);
	    int[] recordIDs = landmarkStore.getRecordIDs();
	    for (int i = 0; i < recordIDs.length; i++) {
		int recID = recordIDs[i];
		stream.writeInt(recID); // save record ID
		// save the length of landmark
		int recSize = landmarkStore.getRecordSize(recID);
		stream.writeInt(recSize);
		byte[] record = new byte[recSize];
		landmarkStore.getRecord(recID, record, 0);
		stream.write(record, 0, recSize);
	    }
	    landmarkStore.closeRecordStore();
	    returnValue = byteArrayOut.toByteArray();
	    return returnValue;
	} catch (RecordStoreException e) {
	    throw new IOException(e.getMessage());
	}
    
public java.util.EnumerationgetLandmarksEnumeration(java.lang.String storeName, java.lang.String category, java.lang.String name, double minLatitude, double maxLatitude, double minLongitude, double maxLongitude)

        Enumeration en = new LandmarkEnumeration(storeName, category, name, 
						 minLatitude, maxLatitude,
						 minLongitude, maxLongitude);
        
        if (!en.hasMoreElements()) {
            return null;
        }
        return en;
    
private static java.lang.StringgetLandmarksFileName(java.lang.String storeName)

	if (storeName == null) {
	    return defaultLandmarksFile;
	}
	return prefixLandmarksFile + storeName;
    
public synchronized java.lang.String[]listStoreNames()

        String[] returnValue = null;
        String[] fileList = RecordStoreImpl.listRecordStores(token, suiteID);
        if (fileList != null) {
            Vector storeList = new Vector();
            int index = 0;
            for (int i = 0; i < fileList.length; i++) {
                String fileName = fileList[i];
                if (fileName.startsWith(prefixLandmarksFile)) {
                    String storeName =
                        fileName.substring(prefixLandmarksFile.length());
                    storeList.addElement(storeName);
                }
            }
            if (storeList.size() > 0) {
                returnValue = new String[storeList.size()];
                for (int i = 0; i < storeList.size(); i++) {
                    returnValue[i] = (String)storeList.elementAt(i);
                }
            }
        }
	return returnValue;
    
public voidremoveLandmarkFromCategory(java.lang.String storeName, LandmarkImpl lm, java.lang.String category)


        try {
            if (lm.getRecordId() > -1 && lm.isInCategory(category)) {
                lm.removeCategory(category);
                updateLandmark(storeName, lm);
            }
        } catch (LandmarkException e) {
            // if it is happened, silently exit
        }
    
public synchronized voidremoveStoreName(java.lang.String storeName)

	// remove file with landmarks
	try {
	    RecordStoreImpl.deleteRecordStore(token, 
		suiteID, getLandmarksFileName(storeName));
	    RecordStoreImpl.deleteRecordStore(token, 
		suiteID, getCategoriesFileName(storeName));
	} catch (RecordStoreNotFoundException e) {
	    // quit silently
	} catch (RecordStoreException e) {
	    throw new IOException(e.getMessage());
	}
    
public voidupdateLandmark(java.lang.String storeName, LandmarkImpl lm)

        if (lm.getRecordId() < 0) {
	    throw new LandmarkException(
		"The landmark was not loaded from a landmark store");
	}
	byte[] data = lm.serialize();
	lm.setRecordId(updateLandmark(storeName, lm.getRecordId(), data));
    
public synchronized intupdateLandmark(java.lang.String storeName, int recordId, byte[] lmData)

	try {
            deleteLandmark(storeName, recordId);
	    return addLandmark(storeName, lmData);
	} catch (Throwable e) {
	    throw new IOException("Error while updating landmark");
	}