/**
* Copyright (C) 2001-2006 France Telecom R&D
*/
package simplereference;
import org.objectweb.util.monolog.api.BasicLevel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
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 nbDep = 3;
final int nbEmp = 20;
List deps = new ArrayList(nbDep);
for (int i = 0; i < nbDep; i++) {
Department d = new Department();
d.setName("department_" + i);
deps.add(d);
}
List emps = new ArrayList(nbEmp);
for (int i = 0; i < nbEmp; i++) {
Employee e = new Employee();
e.setSsn("employee_" + i);
e.setDepartment((Department) deps.get(i % nbDep));
emps.add(e);
}
logger.log(BasicLevel.INFO, "Create " + nbDep + "' department :" + deps);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistentAll(emps);
pm.currentTransaction().commit();
logger.log(BasicLevel.INFO, "Empty Cache.");
emps.clear();
deps.clear();
pm.evictAll();
pm.currentTransaction().begin();
logger.log(BasicLevel.INFO, "Find Employees.");
Query q = pm.newQuery(Employee.class);
q.setFilter("department.name == p1");
q.declareParameters("String p1");
Collection c = (Collection) q.execute("department_" + (nbDep - 1));
for (Iterator it = c.iterator(); it.hasNext();) {
Employee e = (Employee) it.next();
logger.log(BasicLevel.INFO, "- Employee found:" + e.toString());
}
q.closeAll();
pm.currentTransaction().commit();
pm.currentTransaction().begin();
q = pm.newQuery(Employee.class);
c = (Collection) q.execute();
emps.addAll(c);
q.closeAll();
q = pm.newQuery(Department.class);
c = (Collection) q.execute();
deps.addAll(c);
q.closeAll();
logger.log(BasicLevel.INFO, "Delete " + deps.size() + " departments.");
pm.deletePersistentAll(deps);
logger.log(BasicLevel.INFO, "Delete " + emps.size() + " employees.");
pm.deletePersistentAll(emps);
pm.currentTransaction().commit();
pm.close();
return this;
}
}