KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > activating > ActivationExample


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */
2
3 package com.db4odoc.f1.activating;
4
5 import java.io.File JavaDoc;
6 import java.util.List JavaDoc;
7
8 import com.db4o.Db4o;
9 import com.db4o.ObjectContainer;
10 import com.db4o.ObjectSet;
11 import com.db4odoc.f1.Util;
12 import com.db4o.P2LinkedList;
13
14
15
16 public class ActivationExample {
17     
18     public static void main(String JavaDoc[] args){
19         testActivationDefault();
20         testActivationConfig();
21         testCascadeActivate();
22         testMaxActivate();
23         testMinActivate();
24         testActivateDeactivate();
25         testCollectionDef();
26         testCollectionActivation();
27     }
28     // end main
29

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

44     public static void testActivationConfig(){
45         storeSensorPanel();
46         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
47         try {
48             db.ext().configure().activationDepth(1);
49             System.out.println("Object container activation depth = 1");
50             ObjectSet result = db.get(new SensorPanel(1));
51             listResult(result);
52             if (result.size() >0) {
53                 SensorPanel sensor = (SensorPanel)result.get(0);
54                 SensorPanel next = sensor.next;
55                 while (next != null){
56                     System.out.println(next);
57                     next = next.next;
58                 }
59             }
60         } finally {
61             db.close();
62         }
63     }
64     // end testActivationConfig
65

66     public static void testActivationDefault(){
67         storeSensorPanel();
68         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
69         try {
70             System.out.println("Default activation depth");
71             ObjectSet result = db.get(new SensorPanel(1));
72             listResult(result);
73             if (result.size() >0) {
74                 SensorPanel sensor = (SensorPanel)result.get(0);
75                 SensorPanel next = sensor.next;
76                 while (next != null){
77                     System.out.println(next);
78                     next = next.next;
79                 }
80             }
81         } finally {
82             db.close();
83         }
84     }
85     // end testActivationDefault
86

87     public static void testCascadeActivate(){
88         storeSensorPanel();
89         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
90         db.ext().configure().objectClass(SensorPanel.class).cascadeOnActivate(true);
91         try {
92             System.out.println("Cascade activation");
93             ObjectSet result = db.get(new SensorPanel(1));
94             listResult(result);
95             if (result.size() >0) {
96                 SensorPanel sensor = (SensorPanel)result.get(0);
97                 SensorPanel next = sensor.next;
98                 while (next != null){
99                     System.out.println(next);
100                     next = next.next;
101                 }
102             }
103         } finally {
104             db.close();
105         }
106     }
107     // end testCascadeActivate
108

109     public static void testMinActivate(){
110         storeSensorPanel();
111         // note that the minimum applies for *all* instances in the hierarchy
112
// the system ensures that every instantiated List object will have it's
113
// members set to a depth of 1
114
Db4o.configure().objectClass(SensorPanel.class).minimumActivationDepth(1);
115         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
116         try {
117             System.out.println("Minimum activation depth = 1");
118             ObjectSet result = db.get(new SensorPanel(1));
119             listResult(result);
120             if (result.size() >0) {
121                 SensorPanel sensor = (SensorPanel)result.get(0);
122                 SensorPanel next = sensor.next;
123                 while (next != null){
124                     System.out.println(next);
125                     next = next.next;
126                 }
127             }
128         } finally {
129             db.close();
130             Db4o.configure().objectClass(SensorPanel.class).minimumActivationDepth(0);
131         }
132     }
133     // end testMinActivate
134

135     public static void testMaxActivate() {
136         storeSensorPanel();
137         // note that the maximum is applied to the retrieved root object and limits activation
138
// further down the hierarchy
139
Db4o.configure().objectClass(SensorPanel.class).maximumActivationDepth(2);
140
141         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
142         try {
143             System.out.println("Maximum activation depth = 2 (default = 5)");
144             ObjectSet result = db.get(new SensorPanel(1));
145             listResult(result);
146             if (result.size() > 0) {
147                 SensorPanel sensor = (SensorPanel) result.get(0);
148                 SensorPanel next = sensor.next;
149                 while (next != null) {
150                     System.out.println(next);
151                     next = next.next;
152                 }
153             }
154         } finally {
155             db.close();
156             Db4o.configure().objectClass(SensorPanel.class).maximumActivationDepth(Integer.MAX_VALUE);
157         }
158     }
159     // end testMaxActivate
160

161     public static void testActivateDeactivate(){
162         storeSensorPanel();
163         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
164         db.ext().configure().activationDepth(0);
165         try {
166             System.out.println("Object container activation depth = 0" );
167             ObjectSet result = db.get(new SensorPanel(1));
168             System.out.println("Sensor1:");
169             listResult(result);
170             SensorPanel sensor1 = (SensorPanel)result.get(0);
171             testActivated(sensor1);
172             
173             System.out.println("Sensor1 activated:");
174             db.activate(sensor1,4);
175             testActivated(sensor1);
176             
177             System.out.println("Sensor5 activated:");
178             result = db.get(new SensorPanel(5));
179             SensorPanel sensor5 = (SensorPanel)result.get(0);
180             db.activate(sensor5,4);
181             listResult(result);
182             testActivated(sensor5);
183             
184             System.out.println("Sensor1 deactivated:");
185             db.deactivate(sensor1,5);
186             testActivated(sensor1);
187             
188             // DANGER !!!.
189
// If you use deactivate with a higher value than 1
190
// make sure that you know whereto members might branch
191
// Deactivating list1 also deactivated list5
192
System.out.println("Sensor 5 AFTER DEACTIVATE OF Sensor1.");
193             testActivated(sensor5);
194         } finally {
195             db.close();
196         }
197     }
198     // end testActivateDeactivate
199

200     public static void testActivated(SensorPanel sensor){
201         SensorPanel next = sensor;
202         do {
203             next = next.next;
204             System.out.println(next);
205         } while (next != null);
206     }
207     // end testActivated
208

209     public static void storeCollection(){
210         new File JavaDoc(Util.YAPFILENAME).delete();
211         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
212         try {
213             List JavaDoc list = db.ext().collections().newLinkedList();
214             for (int i =0; i < 10; i++){
215                 SensorPanel sensor = new SensorPanel(i);
216                 list.add(sensor);
217             }
218             db.set(list);
219         } finally {
220             db.close();
221         }
222     }
223     // end storeCollection
224

225     public static void testCollectionDef(){
226         storeCollection();
227         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
228         db.ext().configure().activationDepth(5);
229         try {
230             ObjectSet result = db.get(List JavaDoc.class);
231             listResult(result);
232             P2LinkedList list = (P2LinkedList)result.get(0);
233             System.out.println("Default List activation depth: " + list.activationDepth());
234             for (int i = 0; i < list.size(); i++){
235                 System.out.println("List element: " + list.get(i));
236             }
237         } finally {
238             db.close();
239         }
240     }
241     // end testCollectionDef
242

243     public static void testCollectionActivation(){
244         storeCollection();
245         ObjectContainer db = Db4o.openFile(Util.YAPFILENAME);
246         db.ext().configure().activationDepth(5);
247         try {
248             ObjectSet result = db.get(List JavaDoc.class);
249             listResult(result);
250             P2LinkedList list = (P2LinkedList)result.get(0);
251             System.out.println("Setting list activation depth to 0 ");
252             list.activationDepth(0);
253             for (int i = 0; i < list.size(); i++){
254                 System.out.println("List element: " + list.get(i));
255             }
256         } finally {
257             db.close();
258         }
259     }
260     // end testCollectionActivation
261

262     public static void listResult(ObjectSet result) {
263         System.out.println(result.size());
264         while(result.hasNext()) {
265             System.out.println(result.next());
266         }
267     }
268     // end listResult
269

270 }
271
Popular Tags