KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > staticfields > StaticFieldExample


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */
2
3 package com.db4odoc.f1.staticfields;
4
5 import java.awt.Color JavaDoc;
6 import java.io.File JavaDoc;
7
8 import com.db4o.Db4o;
9 import com.db4o.ObjectContainer;
10 import com.db4o.ObjectSet;
11
12
13 public class StaticFieldExample {
14     public final static String JavaDoc YAPFILENAME="formula1.yap";
15
16     public static void main(String JavaDoc[] args) {
17         setPilotsSimple();
18         checkPilots();
19         checkDatabaseFileSize();
20         //
21
setPilotsStatic();
22         checkPilots();
23         checkDatabaseFileSize();
24         updatePilots();
25         updatePilotCategories();
26         checkPilots();
27         deleteTest();
28     }
29     // end main
30

31     public static void setCar(){
32         new File JavaDoc(YAPFILENAME).delete();
33         ObjectContainer db=Db4o.openFile(YAPFILENAME);
34         try {
35             Car car = new Car();
36             car.color = Color.GREEN;
37             db.set(car);
38         } finally {
39             db.close();
40         }
41     }
42     // end setCar
43

44     public static void setPilotsSimple(){
45         System.out.println("In the default setting, static constants are not continously stored and updated.");
46         new File JavaDoc(YAPFILENAME).delete();
47         ObjectContainer db=Db4o.openFile(YAPFILENAME);
48         try {
49             db.set(new Pilot("Michael Schumacher",PilotCategories.WINNER));
50             db.set(new Pilot("Rubens Barrichello",PilotCategories.TALENTED));
51         } finally {
52             db.close();
53         }
54     }
55     // end setPilotsSimple
56

57     public static void setPilotsStatic(){
58         System.out.println("The feature can be turned on for individual classes.");
59         Db4o.configure().objectClass("com.db4odoc.f1.staticfields.PilotCategories").persistStaticFieldValues();
60         new File JavaDoc(YAPFILENAME).delete();
61         ObjectContainer db=Db4o.openFile(YAPFILENAME);
62         try {
63             db.set(new Pilot("Michael Schumacher",PilotCategories.WINNER));
64             db.set(new Pilot("Rubens Barrichello",PilotCategories.TALENTED));
65         } finally {
66             db.close();
67         }
68     }
69     // end setPilotsStatic
70

71     public static void checkPilots(){
72         ObjectContainer db=Db4o.openFile(YAPFILENAME);
73         try {
74             ObjectSet result = db.query(Pilot.class);
75             for(int x = 0; x < result.size(); x++){
76                 Pilot pilot = (Pilot )result.get(x);
77                 if (pilot.getCategory() == PilotCategories.WINNER){
78                     System.out.println("Winner pilot: " + pilot);
79                 } else if (pilot.getCategory() == PilotCategories.TALENTED){
80                     System.out.println("Talented pilot: " + pilot);
81                 } else {
82                     System.out.println("Uncategorized pilot: " + pilot);
83                 }
84             }
85         } finally {
86             db.close();
87         }
88     }
89     // end checkPilots
90

91     public static void updatePilots(){
92         System.out.println("Updating PilotCategory in pilot reference:");
93         ObjectContainer db=Db4o.openFile(YAPFILENAME);
94         try {
95             ObjectSet result = db.query(Pilot.class);
96             for(int x = 0; x < result.size(); x++){
97                 Pilot pilot = (Pilot )result.get(x);
98                 if (pilot.getCategory() == PilotCategories.WINNER){
99                     System.out.println("Winner pilot: " + pilot);
100                     PilotCategories pc = pilot.getCategory();
101                     pc.testChange("WINNER2006");
102                     db.set(pilot);
103                 }
104             }
105         } finally {
106             db.close();
107         }
108         printCategories();
109     }
110     // end updatePilots
111

112     public static void updatePilotCategories(){
113         System.out.println("Updating PilotCategories explicitly:");
114         ObjectContainer db=Db4o.openFile(YAPFILENAME);
115         try {
116             ObjectSet result = db.query(PilotCategories.class);
117             for(int x = 0; x < result.size(); x++){
118                 PilotCategories pc = (PilotCategories)result.get(x);
119                 if (pc == PilotCategories.WINNER){
120                     pc.testChange("WINNER2006");
121                     db.set(pc);
122                 }
123             }
124         } finally {
125             db.close();
126         }
127         printCategories();
128         System.out.println("Change the value back:");
129         db=Db4o.openFile(YAPFILENAME);
130         try {
131             ObjectSet result = db.query(PilotCategories.class);
132             for(int x = 0; x < result.size(); x++){
133                 PilotCategories pc = (PilotCategories)result.get(x);
134                 if (pc == PilotCategories.WINNER){
135                     pc.testChange("WINNER");
136                     db.set(pc);
137                 }
138             }
139         } finally {
140             db.close();
141         }
142         printCategories();
143     }
144     // end updatePilotCategories
145

146     public static void deleteTest(){
147         ObjectContainer db=Db4o.openFile(YAPFILENAME);
148         db.ext().configure().objectClass(Pilot.class).cascadeOnDelete(true);
149         try {
150             System.out.println("Deleting Pilots :");
151             ObjectSet result = db.query(Pilot.class);
152             for(int x = 0; x < result.size(); x++){
153                 Pilot pilot = (Pilot )result.get(x);
154                 db.delete(pilot);
155             }
156             printCategories();
157             System.out.println("Deleting PilotCategories :");
158             result = db.query(PilotCategories.class);
159             for(int x = 0; x < result.size(); x++){
160                 db.delete(result.get(x));
161             }
162             printCategories();
163         } finally {
164             db.close();
165         }
166     }
167     // end deleteTest
168

169     public static void printCategories(){
170         ObjectContainer db=Db4o.openFile(YAPFILENAME);
171         try {
172             ObjectSet result = db.query(PilotCategories.class);
173             System.out.println("Stored categories: " + result.size());
174             for(int x = 0; x < result.size(); x++){
175                 PilotCategories pc = (PilotCategories)result.get(x);
176                 System.out.println("Category: "+pc);
177             }
178         } finally {
179             db.close();
180         }
181     }
182     // end printCategories
183

184     public static void deletePilotCategories(){
185         printCategories();
186         ObjectContainer db=Db4o.openFile(YAPFILENAME);
187         try {
188             ObjectSet result = db.query(PilotCategories.class);
189             for(int x = 0; x < result.size(); x++){
190                 PilotCategories pc = (PilotCategories)result.get(x);
191                 db.delete(pc);
192             }
193         } finally {
194             db.close();
195         }
196         printCategories();
197     }
198     // end deletePilotCategories
199

200     private static void checkDatabaseFileSize(){
201         System.out.println("Database file size: " + new File JavaDoc(YAPFILENAME).length() + "\n");
202     }
203     // end checkDatabaseFileSize
204
}
205
Popular Tags