1 25 26 package org.objectweb.speedo.tutorial.appli.additional.queries; 27 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 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 45 public class TutorialStep4 { 46 47 private static PersistenceManagerFactory pmf = null; 48 49 54 public static void queries() { 55 System.out.println( "***************Queries*****************"); 56 PersistenceManager pm = pmf.getPersistenceManager(); 57 createObjects(pm); 59 iterateExtent(pm); 61 basicQuery(pm); 63 basicQueryOrdering(pm); 64 parameterPassing(pm); 65 compositeFilter(pm); 66 navigationSingleField(pm); 67 navigationMultiValuedField(pm); 68 pm.close(); 69 } 70 71 74 public static void createObjects(PersistenceManager pm){ 75 76 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 persons = new ArrayList (); 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 pm.currentTransaction().begin(); 99 pm.makePersistentAll(persons); 100 pm.currentTransaction().commit(); 101 } 102 103 public static void iterateExtent(PersistenceManager pm){ 105 Extent extent = pm.getExtent(Person.class, true); 106 Iterator 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 public static void basicQuery(PersistenceManager pm){ 118 Query query = pm.newQuery(Person.class, "age < 20"); 119 Collection results = (Collection )query.execute(); 120 System.out.println( "Young persons (<20 years old) :"); 121 Iterator 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 public static void basicQueryOrdering(PersistenceManager pm){ 132 Query query = pm.newQuery(Person.class, "age > 25"); 133 query.setOrdering("age ascending"); 134 Collection results = (Collection )query.execute(); 135 System.out.println( "Over-24 ordered:"); 136 Iterator 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 public static void parameterPassing(PersistenceManager pm){ 147 Query query = pm.newQuery(Person.class, "name == myName"); 148 query.declareParameters("String myName"); 149 String sName = "Labbe Loic"; 150 Collection results = (Collection )query.execute(sName); 151 System.out.println( sName + " has been retrieved:"); 152 Iterator 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 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 results = (Collection )query.execute("Labbe Loic"); 167 System.out.println( "Result of (name=Labbe Loic || age > 45):"); 168 Iterator 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 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 results = (Collection )query.execute("1234567890"); 183 System.out.println( "Result of (contatcDetails.phone == 1234567890):"); 184 Iterator 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 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 results = (Collection )query.execute(); 199 System.out.println( "Person(s) having children under-5:"); 200 Iterator 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 [] args){ 210 TutorialHelper th = null; 211 try { 212 th = new TutorialHelper(args[0]); 213 } catch (IOException e) { 214 e.printStackTrace(); 215 System.exit(-1); 216 } 217 TutorialStep4.pmf = th.getPMF(); 218 TutorialStep4.queries(); 219 } 220 221 } 222 | Popular Tags |