001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Sample.java,v 1.18.2.2 2008/01/07 15:14:00 cwl Exp $
007: */
008:
009: package collections.ship.entity;
010:
011: import java.io.FileNotFoundException;
012: import java.util.Iterator;
013: import java.util.Set;
014:
015: import com.sleepycat.collections.TransactionRunner;
016: import com.sleepycat.collections.TransactionWorker;
017: import com.sleepycat.je.DatabaseException;
018:
019: /**
020: * Sample is the main entry point for the sample program and may be run as
021: * follows:
022: *
023: * <pre>
024: * java collections.ship.entity.Sample
025: * [-h <home-directory> ]
026: * </pre>
027: *
028: * <p> The default for the home directory is ./tmp -- the tmp subdirectory of
029: * the current directory where the sample is run. The home directory must exist
030: * before running the sample. To recreate the sample database from scratch,
031: * delete all files in the home directory before running the sample. </p>
032: *
033: * @author Mark Hayes
034: */
035: public class Sample {
036:
037: private SampleDatabase db;
038: private SampleViews views;
039:
040: /**
041: * Run the sample program.
042: */
043: public static void main(String[] args) {
044:
045: System.out.println("\nRunning sample: " + Sample.class);
046:
047: // Parse the command line arguments.
048: //
049: String homeDir = "./tmp";
050: for (int i = 0; i < args.length; i += 1) {
051: if (args[i].equals("-h") && i < args.length - 1) {
052: i += 1;
053: homeDir = args[i];
054: } else {
055: System.err.println("Usage:\n java "
056: + Sample.class.getName()
057: + "\n [-h <home-directory>]");
058: System.exit(2);
059: }
060: }
061:
062: // Run the sample.
063: //
064: Sample sample = null;
065: try {
066: sample = new Sample(homeDir);
067: sample.run();
068: } catch (Exception e) {
069: // If an exception reaches this point, the last transaction did not
070: // complete. If the exception is RunRecoveryException, follow
071: // the Berkeley DB recovery procedures before running again.
072: e.printStackTrace();
073: } finally {
074: if (sample != null) {
075: try {
076: // Always attempt to close the database cleanly.
077: sample.close();
078: } catch (Exception e) {
079: System.err
080: .println("Exception during database close:");
081: e.printStackTrace();
082: }
083: }
084: }
085: }
086:
087: /**
088: * Open the database and views.
089: */
090: private Sample(String homeDir) throws DatabaseException,
091: FileNotFoundException {
092:
093: db = new SampleDatabase(homeDir);
094: views = new SampleViews(db);
095: }
096:
097: /**
098: * Close the database cleanly.
099: */
100: private void close() 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() throws Exception {
112:
113: TransactionRunner runner = new TransactionRunner(db
114: .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() throws Exception {
125: addSuppliers();
126: addParts();
127: addShipments();
128: }
129: }
130:
131: /**
132: * Print the database in a single transaction. All entities are printed
133: * and the indices are used to print the entities for certain keys.
134: *
135: * <p> Note the use of special iterator() methods. These are used here
136: * with indices to find the shipments for certain keys.</p>
137: */
138: private class PrintDatabase implements TransactionWorker {
139:
140: public void doWork() throws Exception {
141: printValues("Parts", views.getPartSet().iterator());
142: printValues("Suppliers", views.getSupplierSet().iterator());
143: printValues("Suppliers for City Paris", views
144: .getSupplierByCityMap().duplicates("Paris")
145: .iterator());
146: printValues("Shipments", views.getShipmentSet().iterator());
147: printValues("Shipments for Part P1", views
148: .getShipmentByPartMap().duplicates(
149: new PartKey("P1")).iterator());
150: printValues("Shipments for Supplier S1", views
151: .getShipmentBySupplierMap().duplicates(
152: new SupplierKey("S1")).iterator());
153: }
154: }
155:
156: /**
157: * Populate the part entities in the database. If the part set is not
158: * empty, assume that this has already been done.
159: */
160: private void addParts() {
161:
162: Set parts = views.getPartSet();
163: if (parts.isEmpty()) {
164: System.out.println("Adding Parts");
165: parts.add(new Part("P1", "Nut", "Red", new Weight(12.0,
166: Weight.GRAMS), "London"));
167: parts.add(new Part("P2", "Bolt", "Green", new Weight(17.0,
168: Weight.GRAMS), "Paris"));
169: parts.add(new Part("P3", "Screw", "Blue", new Weight(17.0,
170: Weight.GRAMS), "Rome"));
171: parts.add(new Part("P4", "Screw", "Red", new Weight(14.0,
172: Weight.GRAMS), "London"));
173: parts.add(new Part("P5", "Cam", "Blue", new Weight(12.0,
174: Weight.GRAMS), "Paris"));
175: parts.add(new Part("P6", "Cog", "Red", new Weight(19.0,
176: Weight.GRAMS), "London"));
177: }
178: }
179:
180: /**
181: * Populate the supplier entities in the database. If the supplier set is
182: * not empty, assume that this has already been done.
183: */
184: private void addSuppliers() {
185:
186: Set suppliers = views.getSupplierSet();
187: if (suppliers.isEmpty()) {
188: System.out.println("Adding Suppliers");
189: suppliers.add(new Supplier("S1", "Smith", 20, "London"));
190: suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
191: suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
192: suppliers.add(new Supplier("S4", "Clark", 20, "London"));
193: suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
194: }
195: }
196:
197: /**
198: * Populate the shipment entities in the database. If the shipment set
199: * is not empty, assume that this has already been done.
200: */
201: private void addShipments() {
202:
203: Set shipments = views.getShipmentSet();
204: if (shipments.isEmpty()) {
205: System.out.println("Adding Shipments");
206: shipments.add(new Shipment("P1", "S1", 300));
207: shipments.add(new Shipment("P2", "S1", 200));
208: shipments.add(new Shipment("P3", "S1", 400));
209: shipments.add(new Shipment("P4", "S1", 200));
210: shipments.add(new Shipment("P5", "S1", 100));
211: shipments.add(new Shipment("P6", "S1", 100));
212: shipments.add(new Shipment("P1", "S2", 300));
213: shipments.add(new Shipment("P2", "S2", 400));
214: shipments.add(new Shipment("P2", "S3", 200));
215: shipments.add(new Shipment("P2", "S4", 200));
216: shipments.add(new Shipment("P4", "S4", 300));
217: shipments.add(new Shipment("P5", "S4", 400));
218: }
219: }
220:
221: /**
222: * Print the objects returned by an iterator of entity value objects.
223: */
224: private void printValues(String label, Iterator iterator) {
225:
226: System.out.println("\n--- " + label + " ---");
227: while (iterator.hasNext()) {
228: System.out.println(iterator.next().toString());
229: }
230: }
231: }
|