KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > factory > Sample


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Sample.java,v 1.17 2006/10/31 19:56:13 mark Exp $
7  */

8
9 package collections.ship.factory;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.Set JavaDoc;
13
14 import com.sleepycat.collections.TransactionRunner;
15 import com.sleepycat.collections.TransactionWorker;
16
17 /**
18  * Sample is the main entry point for the sample program and may be run as
19  * follows:
20  *
21  * <pre>
22  * java collections.ship.factory.Sample
23  * [-h <home-directory> ]
24  * </pre>
25  *
26  * <p> The default for the home directory is ./tmp -- the tmp subdirectory of
27  * the current directory where the sample is run. To specify a different home
28  * directory, use the -home option. The home directory must exist before
29  * running the sample. To recreate the sample database from scratch, delete
30  * all files in the home directory before running the sample. </p>
31  *
32  * @author Mark Hayes
33  */

34 public class Sample {
35
36     private SampleDatabase db;
37     private SampleViews views;
38
39     /**
40      * Run the sample program.
41      */

42     public static void main(String JavaDoc[] args) {
43
44         System.out.println("\nRunning sample: " + Sample.class);
45
46         // Parse the command line arguments.
47
//
48
String JavaDoc homeDir = "./tmp";
49         for (int i = 0; i < args.length; i += 1) {
50             if (args[i].equals("-h") && i < args.length - 1) {
51                 i += 1;
52                 homeDir = args[i];
53             } else {
54                 System.err.println("Usage:\n java " + Sample.class.getName() +
55                    "\n [-h <home-directory>]");
56                 System.exit(2);
57             }
58         }
59
60         // Run the sample.
61
//
62
Sample sample = null;
63         try {
64             sample = new Sample(homeDir);
65             sample.run();
66         } catch (Exception JavaDoc e) {
67             // If an exception reaches this point, the last transaction did not
68
// complete. If the exception is RunRecoveryException, follow
69
// the Berkeley DB recovery procedures before running again.
70
e.printStackTrace();
71         } finally {
72             if (sample != null) {
73                 try {
74                     // Always attempt to close the database cleanly.
75
sample.close();
76                 } catch (Exception JavaDoc e) {
77                     System.err.println("Exception during database close:");
78                     e.printStackTrace();
79                 }
80             }
81         }
82     }
83
84     /**
85      * Open the database and views.
86      */

87     private Sample(String JavaDoc homeDir)
88         throws Exception JavaDoc {
89
90         db = new SampleDatabase(homeDir);
91         views = new SampleViews(db);
92     }
93
94     /**
95      * Close the database cleanly.
96      */

97     private void close()
98         throws Exception JavaDoc {
99
100         db.close();
101     }
102
103     /**
104      * Run two transactions to populate and print the database. A
105      * TransactionRunner is used to ensure consistent handling of transactions,
106      * including deadlock retries. But the best transaction handling mechanism
107      * to use depends on the application.
108      */

109     private void run()
110         throws Exception JavaDoc {
111
112         TransactionRunner runner = new TransactionRunner(db.getEnvironment());
113         runner.run(new PopulateDatabase());
114         runner.run(new PrintDatabase());
115     }
116
117     /**
118      * Populate the database in a single transaction.
119      */

120     private class PopulateDatabase implements TransactionWorker {
121
122         public void doWork()
123             throws Exception JavaDoc {
124             addSuppliers();
125             addParts();
126             addShipments();
127         }
128     }
129
130     /**
131      * Print the database in a single transaction. All entities are printed
132      * and the indices are used to print the entities for certain keys.
133      *
134      * <p> Note the use of special iterator() methods. These are used here
135      * with indices to find the shipments for certain keys.</p>
136      */

137     private class PrintDatabase implements TransactionWorker {
138
139         public void doWork()
140             throws Exception JavaDoc {
141             printValues("Parts",
142             views.getPartSet().iterator());
143             printValues("Suppliers",
144             views.getSupplierSet().iterator());
145             printValues("Suppliers for City Paris",
146                         views.getSupplierByCityMap().duplicates(
147                                             "Paris").iterator());
148             printValues("Shipments",
149             views.getShipmentSet().iterator());
150             printValues("Shipments for Part P1",
151                         views.getShipmentByPartMap().duplicates(
152                                             new PartKey("P1")).iterator());
153             printValues("Shipments for Supplier S1",
154                         views.getShipmentBySupplierMap().duplicates(
155                                             new SupplierKey("S1")).iterator());
156         }
157     }
158
159     /**
160      * Populate the part entities in the database. If the part set is not
161      * empty, assume that this has already been done.
162      */

163     private void addParts() {
164
165         Set JavaDoc parts = views.getPartSet();
166         if (parts.isEmpty()) {
167             System.out.println("Adding Parts");
168             parts.add(new Part("P1", "Nut", "Red",
169                    new Weight(12.0, Weight.GRAMS), "London"));
170             parts.add(new Part("P2", "Bolt", "Green",
171                    new Weight(17.0, Weight.GRAMS), "Paris"));
172             parts.add(new Part("P3", "Screw", "Blue",
173                    new Weight(17.0, Weight.GRAMS), "Rome"));
174             parts.add(new Part("P4", "Screw", "Red",
175                    new Weight(14.0, Weight.GRAMS), "London"));
176             parts.add(new Part("P5", "Cam", "Blue",
177                    new Weight(12.0, Weight.GRAMS), "Paris"));
178             parts.add(new Part("P6", "Cog", "Red",
179                    new Weight(19.0, Weight.GRAMS), "London"));
180         }
181     }
182
183     /**
184      * Populate the supplier entities in the database. If the supplier set is
185      * not empty, assume that this has already been done.
186      */

187     private void addSuppliers() {
188
189         Set JavaDoc suppliers = views.getSupplierSet();
190         if (suppliers.isEmpty()) {
191             System.out.println("Adding Suppliers");
192             suppliers.add(new Supplier("S1", "Smith", 20, "London"));
193             suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
194             suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
195             suppliers.add(new Supplier("S4", "Clark", 20, "London"));
196             suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
197         }
198     }
199
200     /**
201      * Populate the shipment entities in the database. If the shipment set
202      * is not empty, assume that this has already been done.
203      */

204     private void addShipments() {
205
206         Set JavaDoc shipments = views.getShipmentSet();
207         if (shipments.isEmpty()) {
208             System.out.println("Adding Shipments");
209             shipments.add(new Shipment("P1", "S1", 300));
210             shipments.add(new Shipment("P2", "S1", 200));
211             shipments.add(new Shipment("P3", "S1", 400));
212             shipments.add(new Shipment("P4", "S1", 200));
213             shipments.add(new Shipment("P5", "S1", 100));
214             shipments.add(new Shipment("P6", "S1", 100));
215             shipments.add(new Shipment("P1", "S2", 300));
216             shipments.add(new Shipment("P2", "S2", 400));
217             shipments.add(new Shipment("P2", "S3", 200));
218             shipments.add(new Shipment("P2", "S4", 200));
219             shipments.add(new Shipment("P4", "S4", 300));
220             shipments.add(new Shipment("P5", "S4", 400));
221         }
222     }
223
224     /**
225      * Print the objects returned by an iterator of entity value objects.
226      */

227     private void printValues(String JavaDoc label, Iterator JavaDoc iterator) {
228
229         System.out.println("\n--- " + label + " ---");
230         while (iterator.hasNext()) {
231             System.out.println(iterator.next().toString());
232         }
233     }
234 }
235
Popular Tags