KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > sentity > 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.18 2006/10/31 19:56:17 mark Exp $
7  */

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

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

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

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

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

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

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

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

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

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

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

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