001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jdo;
018:
019: import java.io.FileNotFoundException;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.Properties;
025:
026: import javax.jdo.Extent;
027: import javax.jdo.JDOHelper;
028: import javax.jdo.PersistenceManager;
029: import javax.jdo.PersistenceManagerFactory;
030: import javax.jdo.Query;
031: import javax.jdo.Transaction;
032:
033: /**
034: * Controlling application for the JPOX Tutorial. Relies on the user defining a
035: * file jpox.properties to be in the CLASSPATH and to include the JDO properties
036: * for the JPOX PersistenceManager.
037: *
038: */
039: public class Main {
040: public static void main(String args[]) throws IOException {
041: System.out.println("JPOX Tutorial");
042: System.out.println("=============");
043:
044: PersistenceManager pm = createPersistenceManager();
045: System.out.println("Created a PersistenceManager");
046:
047: // Persistence of a Product and a Book.
048: Transaction tx = pm.currentTransaction();
049: try {
050: tx.begin();
051: Product product = new Product("Sony Discman",
052: "A standard discman from Sony", 49.99);
053: Book book = new Book("Lord of the Rings by Tolkien",
054: "The classic story", 49.99, "JRR Tolkien",
055: "12345678", "MyBooks Factory");
056: pm.makePersistent(product);
057: pm.makePersistent(book);
058:
059: tx.commit();
060: System.out.println("Product and Book have been persisted");
061: } finally {
062: if (tx.isActive()) {
063: tx.rollback();
064: }
065: pm.close();
066: }
067: System.out.println("");
068:
069: // Basic Extent
070: pm = createPersistenceManager();
071: tx = pm.currentTransaction();
072: try {
073: tx.begin();
074: Extent e = pm.getExtent(Product.class, true);
075: Iterator iter = e.iterator();
076: while (iter.hasNext()) {
077: Object obj = iter.next();
078: System.out.println("Extent returned object : " + obj);
079: }
080: tx.commit();
081: } catch (Exception e) {
082: System.out
083: .println("Exception thrown during retrieval of Extent : "
084: + e.getMessage());
085: } finally {
086: if (tx.isActive()) {
087: tx.rollback();
088: }
089: pm.close();
090: }
091:
092: // Perform some query operations
093: pm = createPersistenceManager();
094: tx = pm.currentTransaction();
095: try {
096: tx.begin();
097: System.out.println("Executing Query");
098: Extent e = pm.getExtent(Product.class, true);
099: Query q = pm.newQuery(e, "price < 150.00");
100: q.setOrdering("price ascending");
101: Collection c = (Collection) q.execute();
102: Iterator iter = c.iterator();
103: while (iter.hasNext()) {
104: Object obj = iter.next();
105: System.out.println("> " + obj);
106:
107: // Give an example of an update
108: if (obj instanceof Book) {
109: Book b = (Book) obj;
110: b
111: .setDescription("This book has been reduced in price!");
112: }
113: }
114:
115: tx.commit();
116: } finally {
117: if (tx.isActive()) {
118: tx.rollback();
119: }
120: pm.close();
121: }
122: System.out.println("");
123: System.out.println("End of Tutorial");
124: }
125:
126: /**
127: * Utility method to create a PersistenceManager
128: *
129: * @return The PersistenceManager
130: */
131: private static PersistenceManager createPersistenceManager()
132: throws IOException {
133: Properties properties = new Properties();
134:
135: InputStream is = Main.class.getClassLoader()
136: .getResourceAsStream("jpox.properties");
137: if (is == null) {
138: throw new FileNotFoundException(
139: "Could not find jpox.properties file that defines the JPOX persistence setup.");
140: }
141: properties.load(is);
142:
143: PersistenceManagerFactory pmfactory = JDOHelper
144: .getPersistenceManagerFactory(properties);
145: PersistenceManager pm = pmfactory.getPersistenceManager();
146:
147: return pm;
148: }
149: }
|