/**
* Copyright (C) 2001-2006 France Telecom R&D
*/
package inheritance.horizontal.speedoid;
import org.objectweb.util.monolog.api.BasicLevel;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import common.MainHelper;
public class Main extends MainHelper {
public static void main(String[] args) {
new Main().init(args).run().end();
}
public MainHelper run() {
final int NB_PERSON = 3;
Date d = new Date();
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
for (int i = 0; i < NB_PERSON; i++) {
Address a = new Address("p_street_" + i,
"p_city_" + i,
"p_state_" + i,
"p_zipcode_" + i);
Person p = new Person("person_fn_" + i, "person_ln_" + i, a, d);
pm.makePersistent(p);
}
final int NB_WORKER = 4;
for (int i = 0; i < NB_WORKER; i++) {
Address a = new Address("w_street_" + i,
"w_city_" + i,
"w_state_" + i,
"w_zipcode_" + i);
Worker w = new Worker("worker_fn_" + i, "worker_ln_" + i, a, d,
"jobName_" + i, "company_" + i, (float) (i * 2000.0));
pm.makePersistent(w);
}
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
//find worker only
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
Query q = pm.newQuery(Worker.class);
q.setOrdering("firstname ascending");
Collection c = (Collection) q.execute();
logger.log(BasicLevel.INFO, "Search workers:");
int cpt = 0;
try {
for (Iterator it = c.iterator(); it.hasNext();) {
Worker w = (Worker) it.next();
logger.log(BasicLevel.INFO, "\t- " + w.getFirstname());
cpt ++;
}
} finally {
q.closeAll();
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
}
//find Person instance (without Worker)
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
q = pm.newQuery(pm.getExtent(Person.class, false));
q.setOrdering("firstname ascending");
logger.log(BasicLevel.INFO, "Search persons only (no worker):");
c = (Collection) q.execute();
cpt = 0;
try {
for (Iterator it = c.iterator(); it.hasNext();) {
Person p = (Person) it.next();
logger.log(BasicLevel.INFO, "\t- " + p.getFirstname());
cpt ++;
}
} finally {
q.closeAll();
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
}
//find all persons (including Worker)
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
q = pm.newQuery(pm.getExtent(Person.class, true));
q.setOrdering("firstname ascending");
logger.log(BasicLevel.INFO, "Search all persons (including workers):");
c = (Collection) q.execute();
cpt = 0;
try {
for (Iterator it = c.iterator(); it.hasNext();) {
Person p = (Person) it.next();
String expectedFN;
if (cpt < NB_PERSON) {
expectedFN = "person_fn_" + cpt;
} else {
expectedFN = "worker_fn_" + (cpt - NB_PERSON);
}
logger.log(BasicLevel.INFO, "\t- " + p.getFirstname());
cpt ++;
}
} finally {
q.closeAll();
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
}
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
q = pm.newQuery(pm.getExtent(Person.class, true));
pm.deletePersistentAll((Collection) q.execute());
logger.log(BasicLevel.INFO, "Delete all persons (including workers):");
q.closeAll();
pm.currentTransaction().commit();
pm.close();
return this;
}
}