001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.tutorial.appli.additional.queries;
025:
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Iterator;
030:
031: import javax.jdo.Extent;
032: import javax.jdo.PersistenceManager;
033: import javax.jdo.PersistenceManagerFactory;
034: import javax.jdo.Query;
035:
036: import org.objectweb.speedo.tutorial.TutorialHelper;
037: import org.objectweb.speedo.tutorial.pobjects.additional.queries.ContactDetails;
038: import org.objectweb.speedo.tutorial.pobjects.additional.queries.Person;
039:
040: /**
041: * @author Y.Bersihand
042: */
043: public class TutorialStep4 {
044:
045: private static PersistenceManagerFactory pmf = null;
046:
047: /**This steps describes how to:
048: * Use queries to retrieve persistent objects
049: * Use extents to iterate over persistent objects
050: *
051: */
052: public static void queries() {
053: System.out.println("***************Queries*****************");
054: PersistenceManager pm = pmf.getPersistenceManager();
055: //1- make objects persistent
056: createObjects(pm);
057: //2- use extents
058: iterateExtent(pm);
059: //3- use queries
060: basicQuery(pm);
061: basicQueryOrdering(pm);
062: parameterPassing(pm);
063: compositeFilter(pm);
064: navigationSingleField(pm);
065: navigationMultiValuedField(pm);
066: pm.close();
067: }
068:
069: /**
070: * Create persistent objects
071: */
072: public static void createObjects(PersistenceManager pm) {
073:
074: //create 6 persons
075: Person person1 = new Person("Grange Jean-Luc", 54,
076: new ContactDetails("jeanluc.grange@wanadoo.fr",
077: "1234567890"));
078: Person person2 = new Person("Bordes Raymond", 75,
079: new ContactDetails("raymond.bordes@wanadoo.fr",
080: "1234567890"));
081: Person person3 = new Person("Labbe Loic", 16,
082: new ContactDetails("loic.labbe@wanadoo.fr",
083: "1234567890"));
084: Person person4 = new Person("Lambert Celine", 19,
085: new ContactDetails("celine.lambert@wanadoo.fr",
086: "0987654321"));
087: Person person5 = new Person("Couture Zelie", 23,
088: new ContactDetails("zelie.couture@wanadoo.fr",
089: "0987654321"));
090: Person person6 = new Person("Landreau Vincent", 2,
091: new ContactDetails("vincent.landreau@wanadoo.fr",
092: "0987654321"));
093:
094: person1.addChild(person4);
095: person1.addChild(person5);
096:
097: person5.addChild(person6);
098:
099: Collection persons = new ArrayList();
100: persons.add(person1);
101: persons.add(person2);
102: persons.add(person3);
103: persons.add(person4);
104: persons.add(person5);
105: persons.add(person6);
106:
107: //make persistent all the persons using a collection
108: pm.currentTransaction().begin();
109: pm.makePersistentAll(persons);
110: pm.currentTransaction().commit();
111: }
112:
113: //iterate over all the persons
114: public static void iterateExtent(PersistenceManager pm) {
115: Extent extent = pm.getExtent(Person.class, true);
116: Iterator it = extent.iterator();
117: System.out.println("All " + Person.class.getName()
118: + " instances:");
119: while (it.hasNext()) {
120: Person p = (Person) it.next();
121: System.out.println(p.toString());
122: }
123: extent.close(it);
124: System.out.println();
125: }
126:
127: //retrieve all the under-20 persons
128: public static void basicQuery(PersistenceManager pm) {
129: Query query = pm.newQuery(Person.class, "age < 20");
130: Collection results = (Collection) query.execute();
131: System.out.println("Young persons (<20 years old) :");
132: Iterator it = results.iterator();
133: while (it.hasNext()) {
134: Person p = (Person) it.next();
135: System.out.println(p.toString());
136: }
137: query.closeAll();
138: System.out.println();
139: }
140:
141: //retrieve all the over-25 persons with ordering
142: public static void basicQueryOrdering(PersistenceManager pm) {
143: Query query = pm.newQuery(Person.class, "age > 25");
144: query.setOrdering("age ascending");
145: Collection results = (Collection) query.execute();
146: System.out.println("Over-24 ordered:");
147: Iterator it = results.iterator();
148: while (it.hasNext()) {
149: Person p = (Person) it.next();
150: System.out.println(p.toString());
151: }
152: query.closeAll();
153: System.out.println();
154: }
155:
156: //retrieve a person according to his name
157: public static void parameterPassing(PersistenceManager pm) {
158: Query query = pm.newQuery(Person.class, "name == myName");
159: query.declareParameters("String myName");
160: String sName = "Labbe Loic";
161: Collection results = (Collection) query.execute(sName);
162: System.out.println(sName + " has been retrieved:");
163: Iterator it = results.iterator();
164: while (it.hasNext()) {
165: Person p = (Person) it.next();
166: System.out.println(p.toString());
167: }
168: query.closeAll();
169: System.out.println();
170: }
171:
172: //retrieve a person according to his name and persons according to their age
173: public static void compositeFilter(PersistenceManager pm) {
174: Query query = pm.newQuery(Person.class);
175: query.declareParameters("String myName");
176: query.setFilter("(name == myName) || (age > 45)");
177: Collection results = (Collection) query.execute("Labbe Loic");
178: System.out.println("Result of (name=Labbe Loic || age > 45):");
179: Iterator it = results.iterator();
180: while (it.hasNext()) {
181: Person p = (Person) it.next();
182: System.out.println(p.toString());
183: }
184: query.closeAll();
185: System.out.println();
186: }
187:
188: //retrieve a person according to his contact details
189: public static void navigationSingleField(PersistenceManager pm) {
190: Query query = pm.newQuery(Person.class);
191: query.declareParameters("String phoneNumber");
192: query.setFilter("contactDetails.phone == phoneNumber");
193: Collection results = (Collection) query.execute("1234567890");
194: System.out
195: .println("Result of (contatcDetails.phone == 1234567890):");
196: Iterator it = results.iterator();
197: while (it.hasNext()) {
198: Person p = (Person) it.next();
199: System.out.println(p.toString());
200: }
201: query.closeAll();
202: System.out.println();
203: }
204:
205: //retrieve a person according to the age of his children
206: public static void navigationMultiValuedField(PersistenceManager pm) {
207: Query query = pm.newQuery(Person.class);
208: query.declareVariables("Person child");
209: query.setFilter("children.contains(child) & child.age < 5");
210: Collection results = (Collection) query.execute();
211: System.out.println("Person(s) having children under-5:");
212: Iterator it = results.iterator();
213: while (it.hasNext()) {
214: Person p = (Person) it.next();
215: System.out.println(p.toString());
216: }
217: query.closeAll();
218: System.out.println();
219: }
220:
221: public static void main(String[] args) {
222: TutorialHelper th = null;
223: try {
224: th = new TutorialHelper(args[0]);
225: } catch (IOException e) {
226: e.printStackTrace();
227: System.exit(-1);
228: }
229: TutorialStep4.pmf = th.getPMF();
230: TutorialStep4.queries();
231: }
232:
233: }
|