001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package javax.microedition.location;
027:
028: import java.io.*;
029: import java.util.*;
030:
031: import com.sun.j2me.location.*;
032: import com.sun.midp.security.Permissions;
033: import com.sun.midp.main.*;
034: import com.sun.midp.log.*;
035:
036: /**
037: * This class is defined by the JSR-179 specification
038: * <em>Location API for J2ME for J2ME™.</em>
039: */
040: // JAVADOC COMMENT ELIDED
041: public class LandmarkStore {
042:
043: /** LandmarkStore Create property */
044: private static final String CREATE_LANDMARKSTORE_SUPPORTED = "com.sun.j2me.location.CreateLandmarkStoreSupported";
045:
046: /** LandmarkStore Delete property */
047: private static final String DELETE_LANDMARKSTORE_SUPPORTED = "com.sun.j2me.location.DeleteLandmarkStoreSupported";
048:
049: /** Category Delete property */
050: private static final String DELETE_CATEGORY_SUPPORTED = "com.sun.j2me.location.DeleteCategorySupported";
051:
052: /** The default instance of a store */
053: private static final LandmarkStore defaultStore = new LandmarkStore();
054:
055: /** A map of names of stores to actual stores */
056: private static Hashtable stores = new Hashtable();
057:
058: /** Indicates whether landmark stores were loaded */
059: private static boolean storesInitialized;
060:
061: /** The name of the record store */
062: private String storeName;
063:
064: /**
065: * Constructor is private to prevent user from instanciating this class.
066: *
067: * @param storeName name of landmark store
068: */
069: private LandmarkStore(String storeName) {
070: this .storeName = storeName;
071: }
072:
073: /**
074: * Prevent the user from instanciating this class
075: * creates default LandmarkStore if it is not exist
076: */
077: private LandmarkStore() {
078: this .storeName = null;
079: }
080:
081: // JAVADOC COMMENT ELIDED
082: public static synchronized LandmarkStore getInstance(
083: String storeName) {
084: Util.checkForPermission(Permissions.LANDMARK_READ);
085:
086: LandmarkStore current = null;
087: try {
088: if (storeName == null) {
089: return defaultStore;
090: } else {
091: String[] storeNames = LocationPersistentStorage
092: .getInstance().listStoreNames();
093: if (storeNames != null) {
094: for (int i = 0; i < storeNames.length; i++) {
095: if (storeNames[i].equals(storeName)) {
096: current = new LandmarkStore(storeName);
097: break;
098: }
099: }
100: }
101: }
102: } catch (IOException e) { // return null
103: }
104: return current;
105: }
106:
107: // JAVADOC COMMENT ELIDED
108: public static void createLandmarkStore(String storeName)
109: throws IOException, LandmarkException {
110: if (Configuration.getProperty(CREATE_LANDMARKSTORE_SUPPORTED)
111: .equals("true")) {
112: Util.checkForPermission(Permissions.LANDMARK_MANAGE);
113: if (storeName == null) {
114: throw new NullPointerException(
115: "storeName can not be null");
116: }
117: // verify that the name is correct and store does not exist
118: int storeLen = storeName.length();
119: if (storeLen == 0) {
120: throw new IllegalArgumentException(
121: "The store: name has " + "incorrect length");
122: }
123: if (getInstance(storeName) != null) {
124: throw new IllegalArgumentException("The store: "
125: + storeName + " already exists");
126: }
127: LocationPersistentStorage.getInstance().addStoreName(
128: storeName);
129: } else {
130: throw new LandmarkException(
131: "Implementation does not support "
132: + "creating new landmark stores");
133: }
134: }
135:
136: // JAVADOC COMMENT ELIDED
137: public static void deleteLandmarkStore(String storeName)
138: throws IOException, LandmarkException {
139: if (Configuration.getProperty(DELETE_LANDMARKSTORE_SUPPORTED)
140: .equals("true")) {
141: Util.checkForPermission(Permissions.LANDMARK_MANAGE);
142: if (storeName == null) {
143: throw new NullPointerException();
144: }
145: LocationPersistentStorage.getInstance().removeStoreName(
146: storeName);
147: } else {
148: throw new LandmarkException(
149: "Implementation does not support "
150: + "deleting landmark stores");
151: }
152: }
153:
154: // JAVADOC COMMENT ELIDED
155: public static String[] listLandmarkStores() throws IOException {
156: Util.checkForPermission(Permissions.LANDMARK_READ);
157: return LocationPersistentStorage.getInstance().listStoreNames();
158: }
159:
160: // JAVADOC COMMENT ELIDED
161: public void addLandmark(Landmark landmark, String category)
162: throws IOException {
163: Util.checkForPermission(Permissions.LANDMARK_WRITE);
164: if (landmark == null) { // NullPointerException should be caused
165: throw new NullPointerException("Landmark is null");
166: }
167: LocationPersistentStorage.getInstance().addLandmark(storeName,
168: landmark.getInstance(), category);
169: }
170:
171: // JAVADOC COMMENT ELIDED
172: public Enumeration getLandmarks(String category, String name)
173: throws IOException {
174: Enumeration en = LocationPersistentStorage.getInstance()
175: .getLandmarksEnumeration(storeName, category, name,
176: -90, 90, -180, 180);
177: if (en == null) {
178: return null;
179: }
180: Vector vecLandmarks = new Vector();
181: while (en.hasMoreElements()) {
182: vecLandmarks.addElement(new Landmark((LandmarkImpl) en
183: .nextElement()));
184: }
185: return vecLandmarks.elements();
186: }
187:
188: // JAVADOC COMMENT ELIDED
189: public Enumeration getLandmarks() throws IOException {
190: return getLandmarks(null, null);
191: }
192:
193: // JAVADOC COMMENT ELIDED
194: public Enumeration getLandmarks(String category,
195: double minLatitude, double maxLatitude,
196: double minLongitude, double maxLongitude)
197: throws IOException {
198: if ((minLongitude == 180) || (maxLongitude == 180)) {
199: throw new IllegalArgumentException(
200: "Longtitude out of range " + "must not equal 180");
201: }
202: if (minLatitude > maxLatitude) {
203: throw new IllegalArgumentException(
204: "Minimum latitude cannot be "
205: + "larger than the maximum latitude");
206: }
207: Util.checkRange(minLatitude, -90, 90,
208: "Latitude out of range [-90.0, 90]: ");
209: Util.checkRange(maxLatitude, -90, 90,
210: "Latitude out of range [-90.0, 90]: ");
211: Util.checkRange(maxLongitude, -180, 180,
212: "Longitude out of range [-180.0, 180]: ");
213: Util.checkRange(minLongitude, -180, 180,
214: "Longitude out of range [-180.0, 180]: ");
215:
216: Enumeration en = LocationPersistentStorage.getInstance()
217: .getLandmarksEnumeration(storeName, category, null,
218: minLatitude, maxLatitude, minLongitude,
219: maxLongitude);
220: if (en == null) {
221: return null;
222: }
223: Vector vecLandmarks = new Vector();
224: while (en.hasMoreElements()) {
225: vecLandmarks.addElement(new Landmark((LandmarkImpl) en
226: .nextElement()));
227: }
228: return vecLandmarks.elements();
229: }
230:
231: // JAVADOC COMMENT ELIDED
232: public void removeLandmarkFromCategory(Landmark lm, String category)
233: throws IOException {
234: Util.checkForPermission(Permissions.LANDMARK_WRITE);
235: if (lm == null || category == null) {
236: throw new NullPointerException();
237: }
238: LocationPersistentStorage.getInstance()
239: .removeLandmarkFromCategory(storeName,
240: lm.getInstance(), category);
241: }
242:
243: // JAVADOC COMMENT ELIDED
244: public void updateLandmark(Landmark lm) throws IOException,
245: LandmarkException {
246: Util.checkForPermission(Permissions.LANDMARK_WRITE);
247: if (lm == null) {
248: throw new NullPointerException();
249: }
250: LocationPersistentStorage.getInstance().updateLandmark(
251: storeName, lm.getInstance());
252: }
253:
254: // JAVADOC COMMENT ELIDED
255: public void deleteLandmark(Landmark lm) throws IOException,
256: LandmarkException {
257: Util.checkForPermission(Permissions.LANDMARK_WRITE);
258: if (lm == null) {
259: throw new NullPointerException();
260: }
261:
262: LocationPersistentStorage.getInstance().deleteLandmark(
263: storeName, lm.getInstance());
264: }
265:
266: // JAVADOC COMMENT ELIDED
267: public Enumeration getCategories() {
268: try {
269: return getCategoriesVector().elements();
270: } catch (IOException e) {
271: return new Vector().elements();
272: }
273: }
274:
275: // JAVADOC COMMENT ELIDED
276: private Vector getCategoriesVector() throws IOException {
277: return LocationPersistentStorage.getInstance().getCategories(
278: storeName);
279: }
280:
281: // JAVADOC COMMENT ELIDED
282: public void addCategory(String categoryName)
283: throws LandmarkException, IOException {
284: Util.checkForPermission(Permissions.LANDMARK_CATEGORY);
285: if (categoryName == null) {
286: throw new NullPointerException("Category name is null");
287: }
288: // save categories into persistent storage
289: LocationPersistentStorage.getInstance().addCategory(
290: categoryName, storeName);
291: }
292:
293: // JAVADOC COMMENT ELIDED
294: public void deleteCategory(String categoryName)
295: throws LandmarkException, IOException {
296: if (Configuration.getProperty(DELETE_CATEGORY_SUPPORTED)
297: .equals("true")) {
298: Util.checkForPermission(Permissions.LANDMARK_CATEGORY);
299: if (categoryName == null) {
300: throw new NullPointerException();
301: }
302: LocationPersistentStorage.getInstance().deleteCategory(
303: categoryName, storeName);
304: } else {
305: throw new LandmarkException(
306: "Implementation does not support "
307: + "deleting categories");
308: }
309: }
310:
311: }
|