KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > utility > UtilityExample


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */
2
3 package com.db4odoc.f1.utility;
4
5 import java.io.File JavaDoc;
6
7 import com.db4o.Db4o;
8 import com.db4o.ObjectContainer;
9 import com.db4o.ObjectSet;
10 import com.db4odoc.f1.Util;
11 import com.db4odoc.f1.activating.SensorPanel;
12
13
14 public class UtilityExample extends Util {
15
16
17     public static void main(String JavaDoc[] args) {
18         testDescend();
19         checkActive();
20         checkStored();
21     }
22     // end main
23

24     public static void storeSensorPanel(){
25         new File JavaDoc(Util.YAPFILENAME).delete();
26         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
27         try {
28             // create a linked list with length 10
29
SensorPanel list = new SensorPanel().createList(10);
30             // store all elements with one statement, since all elements are new
31
db.set(list);
32         } finally {
33             db.close();
34         }
35     }
36     // end storeSensorPanel
37

38     public static void testDescend(){
39         storeSensorPanel();
40         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
41         try {
42             db.ext().configure().activationDepth(1);
43             System.out.println("Object container activation depth = 1");
44             ObjectSet result = db.get(new SensorPanel(1));
45             SensorPanel spParent = (SensorPanel)result.get(0);
46             SensorPanel spDescend = (SensorPanel)db.ext().descend((Object JavaDoc)spParent, new String JavaDoc[]{"next","next","next","next","next"});
47             db.ext().activate(spDescend, 5);
48             System.out.println(spDescend);
49         } finally {
50             db.close();
51         }
52     }
53     // end testDescend
54

55     public static void checkActive(){
56         storeSensorPanel();
57         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
58         try {
59             db.ext().configure().activationDepth(2);
60             System.out.println("Object container activation depth = 2");
61             ObjectSet result = db.get(new SensorPanel(1));
62             SensorPanel sensor = (SensorPanel)result.get(0);
63             SensorPanel next = sensor.next;
64             while (next != null){
65                 System.out.println("Object " + next +" is active: " + db.ext().isActive(next));
66                 next = next.next;
67             }
68         } finally {
69             db.close();
70         }
71     }
72     // end checkActive
73

74     public static void checkStored(){
75         // create a linked list with length 10
76
SensorPanel list = new SensorPanel().createList(10);
77         new File JavaDoc(Util.YAPFILENAME).delete();
78         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
79         try {
80             // store all elements with one statement, since all elements are new
81
db.set(list);
82             Object JavaDoc sensor = (Object JavaDoc)list.sensor;
83             SensorPanel sp5 = list.next.next.next.next;
84             System.out.println("Root element "+list+" isStored: " + db.ext().isStored(list));
85             System.out.println("Simple type "+sensor+" isStored: " + db.ext().isStored(sensor));
86             System.out.println("Descend element "+sp5+" isStored: " + db.ext().isStored(sp5));
87             db.delete(list);
88             System.out.println("Root element "+list+" isStored: " + db.ext().isStored(list));
89         } finally {
90             db.close();
91         }
92     }
93     // end checkStored
94
}
95
Popular Tags