01: /**
02: * Copyright (C) 2001-2006 France Telecom R&D
03: */package map;
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.Date;
12: import java.util.HashMap;
13: import java.util.Iterator;
14: import java.util.List;
15: import java.util.Map;
16:
17: import javax.jdo.PersistenceManager;
18: import javax.jdo.Query;
19:
20: public class Main extends MainHelper {
21:
22: public static void main(String[] args) {
23: new Main().init(args).run().end();
24: }
25:
26: public MainHelper run() {
27: final int size = 5;
28: List as = getData(size);
29: logger.log(BasicLevel.INFO, "Create " + size
30: + "' multi table addresses :" + as);
31: PersistenceManager pm = pmf.getPersistenceManager();
32: pm.currentTransaction().begin();
33: pm.makePersistentAll(as);
34: pm.currentTransaction().commit();
35:
36: logger.log(BasicLevel.INFO, "Empty Cache.");
37: as = null;
38: pm.evictAll();
39:
40: pm.currentTransaction().begin();
41: logger.log(BasicLevel.INFO, "Find all multi table addresses.");
42: Query q = pm.newQuery(Address.class);
43: Collection c = (Collection) q.execute();
44: for (Iterator it = c.iterator(); it.hasNext();) {
45: Address a = (Address) it.next();
46: logger.log(BasicLevel.INFO, "- Address found:"
47: + a.toString());
48: }
49: as = new ArrayList(c);
50: q.closeAll();
51: pm.currentTransaction().commit();
52:
53: pm.currentTransaction().begin();
54: logger.log(BasicLevel.INFO, "Delete " + as.size()
55: + " multi table addresses.");
56: pm.deletePersistentAll(as);
57: pm.currentTransaction().commit();
58: pm.close();
59: return this ;
60: }
61:
62: private List getData(final int size) {
63: List as = new ArrayList(size);
64: for (int i = 0; i < size; i++) {
65: Address a = new Address();
66: a.setCity("city_" + i);
67: a.setDeliveryInstructions("deliv_" + i);
68: byte[] map = new byte[20 * (i + 1)];
69: for (int j = 0; j < map.length; j++) {
70: map[j] = (byte) i;
71: }
72: Map m = new HashMap();
73: Date d1 = new Date(Date.UTC(2006, 3, 15, 1, 0, 0));
74: m.put(d1, "sign1");
75: Date d2 = new Date(Date.UTC(2006, 3, 15, 2, 0, 0));
76: m.put(d2, "sign2");
77: a.setDeliveryRecords(m);
78: a.setState("state_" + i);
79: a.setStreet("street_" + i);
80: a.setZip("zip_" + i);
81: as.add(a);
82: }
83: return as;
84: }
85: }
|