001: /**
002: * Copyright (C) 2001-2006 France Telecom R&D
003: */package inheritance.horizontal.speedoid;
004:
005: import org.objectweb.util.monolog.api.BasicLevel;
006:
007: import java.util.Collection;
008: import java.util.Date;
009: import java.util.Iterator;
010:
011: import javax.jdo.PersistenceManager;
012: import javax.jdo.Query;
013:
014: import common.MainHelper;
015:
016: public class Main extends MainHelper {
017:
018: public static void main(String[] args) {
019: new Main().init(args).run().end();
020: }
021:
022: public MainHelper run() {
023: final int NB_PERSON = 3;
024: Date d = new Date();
025: PersistenceManager pm = pmf.getPersistenceManager();
026: pm.currentTransaction().begin();
027: for (int i = 0; i < NB_PERSON; i++) {
028: Address a = new Address("p_street_" + i, "p_city_" + i,
029: "p_state_" + i, "p_zipcode_" + i);
030: Person p = new Person("person_fn_" + i, "person_ln_" + i,
031: a, d);
032: pm.makePersistent(p);
033: }
034: final int NB_WORKER = 4;
035: for (int i = 0; i < NB_WORKER; i++) {
036: Address a = new Address("w_street_" + i, "w_city_" + i,
037: "w_state_" + i, "w_zipcode_" + i);
038: Worker w = new Worker("worker_fn_" + i, "worker_ln_" + i,
039: a, d, "jobName_" + i, "company_" + i,
040: (float) (i * 2000.0));
041: pm.makePersistent(w);
042: }
043: pm.currentTransaction().commit();
044: pm.evictAll();
045: pm.close();
046:
047: //find worker only
048: pm = pmf.getPersistenceManager();
049: pm.currentTransaction().begin();
050: Query q = pm.newQuery(Worker.class);
051: q.setOrdering("firstname ascending");
052: Collection c = (Collection) q.execute();
053: logger.log(BasicLevel.INFO, "Search workers:");
054: int cpt = 0;
055: try {
056: for (Iterator it = c.iterator(); it.hasNext();) {
057: Worker w = (Worker) it.next();
058: logger.log(BasicLevel.INFO, "\t- " + w.getFirstname());
059: cpt++;
060: }
061: } finally {
062: q.closeAll();
063: pm.currentTransaction().commit();
064: pm.evictAll();
065: pm.close();
066: }
067:
068: //find Person instance (without Worker)
069: pm = pmf.getPersistenceManager();
070: pm.currentTransaction().begin();
071: q = pm.newQuery(pm.getExtent(Person.class, false));
072: q.setOrdering("firstname ascending");
073: logger.log(BasicLevel.INFO, "Search persons only (no worker):");
074: c = (Collection) q.execute();
075: cpt = 0;
076: try {
077: for (Iterator it = c.iterator(); it.hasNext();) {
078: Person p = (Person) it.next();
079: logger.log(BasicLevel.INFO, "\t- " + p.getFirstname());
080: cpt++;
081: }
082: } finally {
083: q.closeAll();
084: pm.currentTransaction().commit();
085: pm.evictAll();
086: pm.close();
087: }
088:
089: //find all persons (including Worker)
090: pm = pmf.getPersistenceManager();
091: pm.currentTransaction().begin();
092: q = pm.newQuery(pm.getExtent(Person.class, true));
093: q.setOrdering("firstname ascending");
094: logger.log(BasicLevel.INFO,
095: "Search all persons (including workers):");
096: c = (Collection) q.execute();
097: cpt = 0;
098: try {
099: for (Iterator it = c.iterator(); it.hasNext();) {
100: Person p = (Person) it.next();
101: String expectedFN;
102: if (cpt < NB_PERSON) {
103: expectedFN = "person_fn_" + cpt;
104: } else {
105: expectedFN = "worker_fn_" + (cpt - NB_PERSON);
106: }
107: logger.log(BasicLevel.INFO, "\t- " + p.getFirstname());
108: cpt++;
109: }
110: } finally {
111: q.closeAll();
112: pm.currentTransaction().commit();
113: pm.evictAll();
114: pm.close();
115: }
116:
117: pm = pmf.getPersistenceManager();
118: pm.currentTransaction().begin();
119: q = pm.newQuery(pm.getExtent(Person.class, true));
120: pm.deletePersistentAll((Collection) q.execute());
121: logger.log(BasicLevel.INFO,
122: "Delete all persons (including workers):");
123: q.closeAll();
124: pm.currentTransaction().commit();
125: pm.close();
126: return this;
127: }
128:
129: }
|