KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > tutorial > appli > additional > queries > TutorialStep4


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.tutorial.appli.additional.queries;
27
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.jdo.Extent;
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.PersistenceManagerFactory;
36 import javax.jdo.Query;
37
38 import org.objectweb.speedo.tutorial.TutorialHelper;
39 import org.objectweb.speedo.tutorial.pobjects.additional.queries.ContactDetails;
40 import org.objectweb.speedo.tutorial.pobjects.additional.queries.Person;
41
42 /**
43  * @author Y.Bersihand
44  */

45 public class TutorialStep4 {
46     
47     private static PersistenceManagerFactory pmf = null;
48     
49     /**This steps describes how to:
50      * Use queries to retrieve persistent objects
51      * Use extents to iterate over persistent objects
52      *
53      */

54     public static void queries() {
55         System.out.println( "***************Queries*****************");
56         PersistenceManager pm = pmf.getPersistenceManager();
57         //1- make objects persistent
58
createObjects(pm);
59         //2- use extents
60
iterateExtent(pm);
61         //3- use queries
62
basicQuery(pm);
63         basicQueryOrdering(pm);
64         parameterPassing(pm);
65         compositeFilter(pm);
66         navigationSingleField(pm);
67         navigationMultiValuedField(pm);
68         pm.close();
69     }
70     
71     /**
72      * Create persistent objects
73      */

74     public static void createObjects(PersistenceManager pm){
75         
76         //create 6 persons
77
Person person1 = new Person("Grange Jean-Luc", 54, new ContactDetails("jeanluc.grange@wanadoo.fr", "1234567890"));
78         Person person2 = new Person("Bordes Raymond", 75, new ContactDetails("raymond.bordes@wanadoo.fr", "1234567890"));
79         Person person3 = new Person("Labbe Loic", 16, new ContactDetails("loic.labbe@wanadoo.fr", "1234567890"));
80         Person person4 = new Person("Lambert Celine", 19, new ContactDetails("celine.lambert@wanadoo.fr", "0987654321"));
81         Person person5 = new Person("Couture Zelie", 23, new ContactDetails("zelie.couture@wanadoo.fr", "0987654321"));
82         Person person6 = new Person("Landreau Vincent", 2, new ContactDetails("vincent.landreau@wanadoo.fr", "0987654321"));
83         
84         person1.addChild(person4);
85         person1.addChild(person5);
86         
87         person5.addChild(person6);
88         
89         Collection JavaDoc persons = new ArrayList JavaDoc();
90         persons.add(person1);
91         persons.add(person2);
92         persons.add(person3);
93         persons.add(person4);
94         persons.add(person5);
95         persons.add(person6);
96         
97         //make persistent all the persons using a collection
98
pm.currentTransaction().begin();
99         pm.makePersistentAll(persons);
100         pm.currentTransaction().commit();
101     }
102     
103     //iterate over all the persons
104
public static void iterateExtent(PersistenceManager pm){
105         Extent extent = pm.getExtent(Person.class, true);
106         Iterator JavaDoc it = extent.iterator();
107         System.out.println( "All " + Person.class.getName() + " instances:");
108         while(it.hasNext()){
109             Person p = (Person) it.next();
110             System.out.println( p.toString());
111         }
112         extent.close(it);
113         System.out.println();
114     }
115     
116     //retrieve all the under-20 persons
117
public static void basicQuery(PersistenceManager pm){
118         Query query = pm.newQuery(Person.class, "age < 20");
119         Collection JavaDoc results = (Collection JavaDoc)query.execute();
120         System.out.println( "Young persons (<20 years old) :");
121         Iterator JavaDoc it = results.iterator();
122         while(it.hasNext()){
123             Person p = (Person) it.next();
124             System.out.println( p.toString());
125         }
126         query.closeAll();
127         System.out.println();
128     }
129     
130     //retrieve all the over-25 persons with ordering
131
public static void basicQueryOrdering(PersistenceManager pm){
132         Query query = pm.newQuery(Person.class, "age > 25");
133         query.setOrdering("age ascending");
134         Collection JavaDoc results = (Collection JavaDoc)query.execute();
135         System.out.println( "Over-24 ordered:");
136         Iterator JavaDoc it = results.iterator();
137         while(it.hasNext()){
138             Person p = (Person) it.next();
139             System.out.println( p.toString());
140         }
141         query.closeAll();
142         System.out.println();
143     }
144     
145     //retrieve a person according to his name
146
public static void parameterPassing(PersistenceManager pm){
147         Query query = pm.newQuery(Person.class, "name == myName");
148         query.declareParameters("String myName");
149         String JavaDoc sName = "Labbe Loic";
150         Collection JavaDoc results = (Collection JavaDoc)query.execute(sName);
151         System.out.println( sName + " has been retrieved:");
152         Iterator JavaDoc it = results.iterator();
153         while(it.hasNext()){
154             Person p = (Person) it.next();
155             System.out.println( p.toString());
156         }
157         query.closeAll();
158         System.out.println();
159     }
160     
161     //retrieve a person according to his name and persons according to their age
162
public static void compositeFilter(PersistenceManager pm){
163         Query query = pm.newQuery(Person.class);
164         query.declareParameters("String myName");
165         query.setFilter("(name == myName) || (age > 45)");
166         Collection JavaDoc results = (Collection JavaDoc)query.execute("Labbe Loic");
167         System.out.println( "Result of (name=Labbe Loic || age > 45):");
168         Iterator JavaDoc it = results.iterator();
169         while(it.hasNext()){
170             Person p = (Person) it.next();
171             System.out.println( p.toString());
172         }
173         query.closeAll();
174         System.out.println();
175     }
176     
177     //retrieve a person according to his contact details
178
public static void navigationSingleField(PersistenceManager pm){
179         Query query = pm.newQuery(Person.class);
180         query.declareParameters("String phoneNumber");
181         query.setFilter("contactDetails.phone == phoneNumber");
182         Collection JavaDoc results = (Collection JavaDoc)query.execute("1234567890");
183         System.out.println( "Result of (contatcDetails.phone == 1234567890):");
184         Iterator JavaDoc it = results.iterator();
185         while(it.hasNext()){
186             Person p = (Person) it.next();
187             System.out.println( p.toString());
188         }
189         query.closeAll();
190         System.out.println();
191     }
192     
193     //retrieve a person according to the age of his children
194
public static void navigationMultiValuedField(PersistenceManager pm){
195         Query query = pm.newQuery(Person.class);
196         query.declareVariables("Person child");
197         query.setFilter("children.contains(child) & child.age < 5");
198         Collection JavaDoc results = (Collection JavaDoc)query.execute();
199         System.out.println( "Person(s) having children under-5:");
200         Iterator JavaDoc it = results.iterator();
201         while(it.hasNext()){
202             Person p = (Person) it.next();
203             System.out.println( p.toString());
204         }
205         query.closeAll();
206         System.out.println();
207     }
208     
209     public static void main(String JavaDoc[] args){
210         TutorialHelper th = null;
211         try {
212             th = new TutorialHelper(args[0]);
213         } catch (IOException JavaDoc e) {
214             e.printStackTrace();
215             System.exit(-1);
216         }
217         TutorialStep4.pmf = th.getPMF();
218         TutorialStep4.queries();
219     }
220
221 }
222
Popular Tags