01: /**
02: * Copyright (C) 2001-2006 France Telecom R&D
03: */package simplecollection;
04:
05: import org.objectweb.util.monolog.api.BasicLevel;
06:
07: import java.util.ArrayList;
08: import java.util.Collection;
09: import java.util.Iterator;
10: import java.util.List;
11:
12: import javax.jdo.PersistenceManager;
13: import javax.jdo.Query;
14:
15: import common.MainHelper;
16:
17: public class Main extends MainHelper {
18:
19: public static void main(String[] args) {
20: new Main().init(args).run().end();
21: }
22:
23: public MainHelper run() {
24: final int nbDep = 3;
25: final int nbEmp = 20;
26: final int nbEmpByDep = 9;
27: List emps = new ArrayList(nbEmp);
28: for (int i = 0; i < nbEmp; i++) {
29: Employee e = new Employee();
30: e.setSsn("employee_" + i);
31: emps.add(e);
32: }
33: List deps = new ArrayList(nbDep);
34: for (int i = 0; i < nbDep; i++) {
35: Department d = new Department();
36: d.setName("department_" + i);
37: ArrayList myEmps = new ArrayList();
38: for (int j = 0; j < nbEmpByDep; j++) {
39: myEmps.add(emps.get((i * j * 7) % nbEmp));
40: }
41: d.setEmployees(myEmps);
42: deps.add(d);
43: }
44: logger.log(BasicLevel.INFO, "Create " + nbDep
45: + "' department :" + deps);
46: PersistenceManager pm = pmf.getPersistenceManager();
47: pm.currentTransaction().begin();
48: pm.makePersistentAll(emps);
49: pm.currentTransaction().commit();
50:
51: logger.log(BasicLevel.INFO, "Empty Cache.");
52: emps.clear();
53: deps.clear();
54: pm.evictAll();
55:
56: pm.currentTransaction().begin();
57: logger.log(BasicLevel.INFO, "Find Employees.");
58: Query q = pm.newQuery(Department.class);
59: q.setFilter("employees.ssn.endsWith(p1)");
60: q.declareParameters("String p1");
61: Collection c = (Collection) q.execute("" + nbEmp / 2);
62: for (Iterator it = c.iterator(); it.hasNext();) {
63: Employee e = (Employee) it.next();
64: logger.log(BasicLevel.INFO, "- Employee found:"
65: + e.toString());
66: }
67: q.closeAll();
68: pm.currentTransaction().commit();
69:
70: pm.currentTransaction().begin();
71: q = pm.newQuery(Employee.class);
72: c = (Collection) q.execute();
73: emps.addAll(c);
74: q.closeAll();
75: q = pm.newQuery(Department.class);
76: c = (Collection) q.execute();
77: deps.addAll(c);
78: q.closeAll();
79: logger.log(BasicLevel.INFO, "Delete " + deps.size()
80: + " departments.");
81: pm.deletePersistentAll(deps);
82: logger.log(BasicLevel.INFO, "Delete " + emps.size()
83: + " employees.");
84: pm.deletePersistentAll(emps);
85: pm.currentTransaction().commit();
86: pm.close();
87: return this;
88: }
89:
90: }
|