/**
* Copyright (C) 2001-2006 France Telecom R&D
*/
package simpleclass;
import org.objectweb.util.monolog.api.BasicLevel;
import common.MainHelper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
public class Main extends MainHelper {
public static void main(String[] args) {
new Main().init(args).run().end();
}
public MainHelper run() {
final int size = 5;
List as = getData(size);
logger.log(BasicLevel.INFO, "Create " + size + "' simple Addresses :" + as);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistentAll(as);
pm.currentTransaction().commit();
logger.log(BasicLevel.INFO, "Empty Cache.");
as = null;
pm.evictAll();
pm.currentTransaction().begin();
logger.log(BasicLevel.INFO, "Find all Addresses.");
Query q = pm.newQuery(Address.class);
Collection c = (Collection) q.execute();
for (Iterator it = c.iterator(); it.hasNext();) {
Address a = (Address) it.next();
logger.log(BasicLevel.INFO, "- Simple address found:" + a.toString());
}
as = new ArrayList(c);
q.closeAll();
pm.currentTransaction().commit();
pm.currentTransaction().begin();
logger.log(BasicLevel.INFO, "Delete " + as.size() + " simple addresses.");
pm.deletePersistentAll(as);
pm.currentTransaction().commit();
pm.close();
return this;
}
private List getData(final int size) {
List as = new ArrayList(size);
for (int i = 0; i < size; i++) {
Address a = new Address();
a.setCity("city_"+ i);
a.setDeliveryInstructions("deliv_"+ i);
byte[] map = new byte[20 * (i+1)];
for (int j = 0; j < map.length; j++) {
map[j] = (byte) i;
}
a.setState("state_" + i);
a.setStreet("street_" + i);
a.setZip("zip_" + i);
as.add(a);
}
return as;
}
}