/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
*/
package org.objectweb.speedo.tutorial.appli.additional.queries;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import org.objectweb.speedo.tutorial.TutorialHelper;
import org.objectweb.speedo.tutorial.pobjects.additional.queries.ContactDetails;
import org.objectweb.speedo.tutorial.pobjects.additional.queries.Person;
/**
* @author Y.Bersihand
*/
public class TutorialStep4 {
private static PersistenceManagerFactory pmf = null;
/**This steps describes how to:
* Use queries to retrieve persistent objects
* Use extents to iterate over persistent objects
*
*/
public static void queries() {
System.out.println( "***************Queries*****************");
PersistenceManager pm = pmf.getPersistenceManager();
//1- make objects persistent
createObjects(pm);
//2- use extents
iterateExtent(pm);
//3- use queries
basicQuery(pm);
basicQueryOrdering(pm);
parameterPassing(pm);
compositeFilter(pm);
navigationSingleField(pm);
navigationMultiValuedField(pm);
pm.close();
}
/**
* Create persistent objects
*/
public static void createObjects(PersistenceManager pm){
//create 6 persons
Person person1 = new Person("Grange Jean-Luc", 54, new ContactDetails("jeanluc.grange@wanadoo.fr", "1234567890"));
Person person2 = new Person("Bordes Raymond", 75, new ContactDetails("raymond.bordes@wanadoo.fr", "1234567890"));
Person person3 = new Person("Labbe Loic", 16, new ContactDetails("loic.labbe@wanadoo.fr", "1234567890"));
Person person4 = new Person("Lambert Celine", 19, new ContactDetails("celine.lambert@wanadoo.fr", "0987654321"));
Person person5 = new Person("Couture Zelie", 23, new ContactDetails("zelie.couture@wanadoo.fr", "0987654321"));
Person person6 = new Person("Landreau Vincent", 2, new ContactDetails("vincent.landreau@wanadoo.fr", "0987654321"));
person1.addChild(person4);
person1.addChild(person5);
person5.addChild(person6);
Collection persons = new ArrayList();
persons.add(person1);
persons.add(person2);
persons.add(person3);
persons.add(person4);
persons.add(person5);
persons.add(person6);
//make persistent all the persons using a collection
pm.currentTransaction().begin();
pm.makePersistentAll(persons);
pm.currentTransaction().commit();
}
//iterate over all the persons
public static void iterateExtent(PersistenceManager pm){
Extent extent = pm.getExtent(Person.class, true);
Iterator it = extent.iterator();
System.out.println( "All " + Person.class.getName() + " instances:");
while(it.hasNext()){
Person p = (Person) it.next();
System.out.println( p.toString());
}
extent.close(it);
System.out.println();
}
//retrieve all the under-20 persons
public static void basicQuery(PersistenceManager pm){
Query query = pm.newQuery(Person.class, "age < 20");
Collection results = (Collection)query.execute();
System.out.println( "Young persons (<20 years old) :");
Iterator it = results.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
System.out.println( p.toString());
}
query.closeAll();
System.out.println();
}
//retrieve all the over-25 persons with ordering
public static void basicQueryOrdering(PersistenceManager pm){
Query query = pm.newQuery(Person.class, "age > 25");
query.setOrdering("age ascending");
Collection results = (Collection)query.execute();
System.out.println( "Over-24 ordered:");
Iterator it = results.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
System.out.println( p.toString());
}
query.closeAll();
System.out.println();
}
//retrieve a person according to his name
public static void parameterPassing(PersistenceManager pm){
Query query = pm.newQuery(Person.class, "name == myName");
query.declareParameters("String myName");
String sName = "Labbe Loic";
Collection results = (Collection)query.execute(sName);
System.out.println( sName + " has been retrieved:");
Iterator it = results.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
System.out.println( p.toString());
}
query.closeAll();
System.out.println();
}
//retrieve a person according to his name and persons according to their age
public static void compositeFilter(PersistenceManager pm){
Query query = pm.newQuery(Person.class);
query.declareParameters("String myName");
query.setFilter("(name == myName) || (age > 45)");
Collection results = (Collection)query.execute("Labbe Loic");
System.out.println( "Result of (name=Labbe Loic || age > 45):");
Iterator it = results.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
System.out.println( p.toString());
}
query.closeAll();
System.out.println();
}
//retrieve a person according to his contact details
public static void navigationSingleField(PersistenceManager pm){
Query query = pm.newQuery(Person.class);
query.declareParameters("String phoneNumber");
query.setFilter("contactDetails.phone == phoneNumber");
Collection results = (Collection)query.execute("1234567890");
System.out.println( "Result of (contatcDetails.phone == 1234567890):");
Iterator it = results.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
System.out.println( p.toString());
}
query.closeAll();
System.out.println();
}
//retrieve a person according to the age of his children
public static void navigationMultiValuedField(PersistenceManager pm){
Query query = pm.newQuery(Person.class);
query.declareVariables("Person child");
query.setFilter("children.contains(child) & child.age < 5");
Collection results = (Collection)query.execute();
System.out.println( "Person(s) having children under-5:");
Iterator it = results.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
System.out.println( p.toString());
}
query.closeAll();
System.out.println();
}
public static void main(String[] args){
TutorialHelper th = null;
try {
th = new TutorialHelper(args[0]);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
TutorialStep4.pmf = th.getPMF();
TutorialStep4.queries();
}
}