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:02 cwl Exp $
007: */
008:
009: package collections.ship.sentity;
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.sentity.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. To specify a different home
030: * directory, use the -home option. The home directory must exist before
031: * running the sample. To recreate the sample database from scratch, delete
032: * all files in the home directory before running the sample. </p>
033: *
034: * @author Mark Hayes
035: */
036: public class Sample {
037:
038: private SampleDatabase db;
039: private SampleViews views;
040:
041: /**
042: * Run the sample program.
043: */
044: public static void main(String[] args) {
045:
046: System.out.println("\nRunning sample: " + Sample.class);
047:
048: // Parse the command line arguments.
049: //
050: String homeDir = "./tmp";
051: for (int i = 0; i < args.length; i += 1) {
052: if (args[i].equals("-h") && i < args.length - 1) {
053: i += 1;
054: homeDir = args[i];
055: } else {
056: System.err.println("Usage:\n java "
057: + Sample.class.getName()
058: + "\n [-h <home-directory>]");
059: System.exit(2);
060: }
061: }
062:
063: // Run the sample.
064: //
065: Sample sample = null;
066: try {
067: sample = new Sample(homeDir);
068: sample.run();
069: } catch (Exception e) {
070: // If an exception reaches this point, the last transaction did not
071: // complete. If the exception is RunRecoveryException, follow
072: // the Berkeley DB recovery procedures before running again.
073: e.printStackTrace();
074: } finally {
075: if (sample != null) {
076: try {
077: // Always attempt to close the database cleanly.
078: sample.close();
079: } catch (Exception e) {
080: System.err
081: .println("Exception during database close:");
082: e.printStackTrace();
083: }
084: }
085: }
086: }
087:
088: /**
089: * Open the database and views.
090: */
091: private Sample(String homeDir) throws DatabaseException,
092: FileNotFoundException {
093:
094: db = new SampleDatabase(homeDir);
095: views = new SampleViews(db);
096: }
097:
098: /**
099: * Close the database cleanly.
100: */
101: private void close() throws DatabaseException {
102:
103: db.close();
104: }
105:
106: /**
107: * Run two transactions to populate and print the database. A
108: * TransactionRunner is used to ensure consistent handling of transactions,
109: * including deadlock retries. But the best transaction handling mechanism
110: * to use depends on the application.
111: */
112: private void run() throws Exception {
113:
114: TransactionRunner runner = new TransactionRunner(db
115: .getEnvironment());
116: runner.run(new PopulateDatabase());
117: runner.run(new PrintDatabase());
118: }
119:
120: /**
121: * Populate the database in a single transaction.
122: */
123: private class PopulateDatabase implements TransactionWorker {
124:
125: public void doWork() throws Exception {
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() throws Exception {
142: printValues("Parts", views.getPartSet().iterator());
143: printValues("Suppliers", views.getSupplierSet().iterator());
144: printValues("Suppliers for City Paris", views
145: .getSupplierByCityMap().duplicates("Paris")
146: .iterator());
147: printValues("Shipments", views.getShipmentSet().iterator());
148: printValues("Shipments for Part P1", views
149: .getShipmentByPartMap().duplicates(
150: new PartKey("P1")).iterator());
151: printValues("Shipments for Supplier S1", views
152: .getShipmentBySupplierMap().duplicates(
153: new SupplierKey("S1")).iterator());
154: }
155: }
156:
157: /**
158: * Populate the part entities in the database. If the part set is not
159: * empty, assume that this has already been done.
160: */
161: private void addParts() {
162:
163: Set parts = views.getPartSet();
164: if (parts.isEmpty()) {
165: System.out.println("Adding Parts");
166: parts.add(new Part("P1", "Nut", "Red", new Weight(12.0,
167: Weight.GRAMS), "London"));
168: parts.add(new Part("P2", "Bolt", "Green", new Weight(17.0,
169: Weight.GRAMS), "Paris"));
170: parts.add(new Part("P3", "Screw", "Blue", new Weight(17.0,
171: Weight.GRAMS), "Rome"));
172: parts.add(new Part("P4", "Screw", "Red", new Weight(14.0,
173: Weight.GRAMS), "London"));
174: parts.add(new Part("P5", "Cam", "Blue", new Weight(12.0,
175: Weight.GRAMS), "Paris"));
176: parts.add(new Part("P6", "Cog", "Red", new Weight(19.0,
177: Weight.GRAMS), "London"));
178: }
179: }
180:
181: /**
182: * Populate the supplier entities in the database. If the supplier set is
183: * not empty, assume that this has already been done.
184: */
185: private void addSuppliers() {
186:
187: Set suppliers = views.getSupplierSet();
188: if (suppliers.isEmpty()) {
189: System.out.println("Adding Suppliers");
190: suppliers.add(new Supplier("S1", "Smith", 20, "London"));
191: suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
192: suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
193: suppliers.add(new Supplier("S4", "Clark", 20, "London"));
194: suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
195: }
196: }
197:
198: /**
199: * Populate the shipment entities in the database. If the shipment set
200: * is not empty, assume that this has already been done.
201: */
202: private void addShipments() {
203:
204: Set shipments = views.getShipmentSet();
205: if (shipments.isEmpty()) {
206: System.out.println("Adding Shipments");
207: shipments.add(new Shipment("P1", "S1", 300));
208: shipments.add(new Shipment("P2", "S1", 200));
209: shipments.add(new Shipment("P3", "S1", 400));
210: shipments.add(new Shipment("P4", "S1", 200));
211: shipments.add(new Shipment("P5", "S1", 100));
212: shipments.add(new Shipment("P6", "S1", 100));
213: shipments.add(new Shipment("P1", "S2", 300));
214: shipments.add(new Shipment("P2", "S2", 400));
215: shipments.add(new Shipment("P2", "S3", 200));
216: shipments.add(new Shipment("P2", "S4", 200));
217: shipments.add(new Shipment("P4", "S4", 300));
218: shipments.add(new Shipment("P5", "S4", 400));
219: }
220: }
221:
222: /**
223: * Print the objects returned by an iterator of entity value objects.
224: */
225: private void printValues(String label, Iterator iterator) {
226:
227: System.out.println("\n--- " + label + " ---");
228: while (iterator.hasNext()) {
229: System.out.println(iterator.next().toString());
230: }
231: }
232: }
|