01: /**
02: * Copyright (C) 2001-2006 France Telecom R&D
03: */package simpleclass;
04:
05: import org.objectweb.util.monolog.api.BasicLevel;
06:
07: import common.MainHelper;
08:
09: import java.util.ArrayList;
10: import java.util.Collection;
11: import java.util.Iterator;
12: import java.util.List;
13:
14: import javax.jdo.PersistenceManager;
15: import javax.jdo.Query;
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 size = 5;
25: List as = getData(size);
26: logger.log(BasicLevel.INFO, "Create " + size
27: + "' simple Addresses :" + as);
28: PersistenceManager pm = pmf.getPersistenceManager();
29: pm.currentTransaction().begin();
30: pm.makePersistentAll(as);
31: pm.currentTransaction().commit();
32:
33: logger.log(BasicLevel.INFO, "Empty Cache.");
34: as = null;
35: pm.evictAll();
36:
37: pm.currentTransaction().begin();
38: logger.log(BasicLevel.INFO, "Find all Addresses.");
39: Query q = pm.newQuery(Address.class);
40: Collection c = (Collection) q.execute();
41: for (Iterator it = c.iterator(); it.hasNext();) {
42: Address a = (Address) it.next();
43: logger.log(BasicLevel.INFO, "- Simple address found:"
44: + a.toString());
45: }
46: as = new ArrayList(c);
47: q.closeAll();
48: pm.currentTransaction().commit();
49:
50: pm.currentTransaction().begin();
51: logger.log(BasicLevel.INFO, "Delete " + as.size()
52: + " simple addresses.");
53: pm.deletePersistentAll(as);
54: pm.currentTransaction().commit();
55: pm.close();
56: return this ;
57: }
58:
59: private List getData(final int size) {
60: List as = new ArrayList(size);
61: for (int i = 0; i < size; i++) {
62: Address a = new Address();
63: a.setCity("city_" + i);
64: a.setDeliveryInstructions("deliv_" + i);
65: byte[] map = new byte[20 * (i + 1)];
66: for (int j = 0; j < map.length; j++) {
67: map[j] = (byte) i;
68: }
69: a.setState("state_" + i);
70: a.setStreet("street_" + i);
71: a.setZip("zip_" + i);
72: as.add(a);
73: }
74: return as;
75: }
76: }
|