00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package collections.ship.basic;
00011
00012 import java.io.FileNotFoundException;
00013 import java.util.Iterator;
00014 import java.util.Map;
00015
00016 import com.sleepycat.collections.StoredIterator;
00017 import com.sleepycat.collections.TransactionRunner;
00018 import com.sleepycat.collections.TransactionWorker;
00019 import com.sleepycat.db.DatabaseException;
00020
00037 public class Sample {
00038
00039 private SampleDatabase db;
00040 private SampleViews views;
00041
00045 public static void main(String[] args) {
00046
00047 System.out.println("\nRunning sample: " + Sample.class);
00048
00049
00050
00051 String homeDir = "./tmp";
00052 for (int i = 0; i < args.length; i += 1) {
00053 if (args[i].equals("-h") && i < args.length - 1) {
00054 i += 1;
00055 homeDir = args[i];
00056 } else {
00057 System.err.println("Usage:\n java " + Sample.class.getName() +
00058 "\n [-h <home-directory>]");
00059 System.exit(2);
00060 }
00061 }
00062
00063
00064
00065 Sample sample = null;
00066 try {
00067 sample = new Sample(homeDir);
00068 sample.run();
00069 } catch (Exception e) {
00070
00071
00072
00073 e.printStackTrace();
00074 } finally {
00075 if (sample != null) {
00076 try {
00077
00078 sample.close();
00079 } catch (Exception e) {
00080 System.err.println("Exception during database close:");
00081 e.printStackTrace();
00082 }
00083 }
00084 }
00085 }
00086
00090 private Sample(String homeDir)
00091 throws DatabaseException, FileNotFoundException {
00092
00093 db = new SampleDatabase(homeDir);
00094 views = new SampleViews(db);
00095 }
00096
00100 private void close()
00101 throws DatabaseException {
00102
00103 db.close();
00104 }
00105
00112 private void run()
00113 throws Exception {
00114
00115 TransactionRunner runner = new TransactionRunner(db.getEnvironment());
00116 runner.run(new PopulateDatabase());
00117 runner.run(new PrintDatabase());
00118 }
00119
00123 private class PopulateDatabase implements TransactionWorker {
00124
00125 public void doWork()
00126 throws Exception {
00127 addSuppliers();
00128 addParts();
00129 addShipments();
00130 }
00131 }
00132
00136 private class PrintDatabase implements TransactionWorker {
00137
00138 public void doWork()
00139 throws Exception {
00140 printEntries("Parts",
00141 views.getPartEntrySet().iterator());
00142 printEntries("Suppliers",
00143 views.getSupplierEntrySet().iterator());
00144 printEntries("Shipments",
00145 views.getShipmentEntrySet().iterator());
00146 }
00147 }
00148
00153 private void addParts() {
00154
00155 Map parts = views.getPartMap();
00156 if (parts.isEmpty()) {
00157 System.out.println("Adding Parts");
00158 parts.put(new PartKey("P1"),
00159 new PartData("Nut", "Red",
00160 new Weight(12.0, Weight.GRAMS),
00161 "London"));
00162 parts.put(new PartKey("P2"),
00163 new PartData("Bolt", "Green",
00164 new Weight(17.0, Weight.GRAMS),
00165 "Paris"));
00166 parts.put(new PartKey("P3"),
00167 new PartData("Screw", "Blue",
00168 new Weight(17.0, Weight.GRAMS),
00169 "Rome"));
00170 parts.put(new PartKey("P4"),
00171 new PartData("Screw", "Red",
00172 new Weight(14.0, Weight.GRAMS),
00173 "London"));
00174 parts.put(new PartKey("P5"),
00175 new PartData("Cam", "Blue",
00176 new Weight(12.0, Weight.GRAMS),
00177 "Paris"));
00178 parts.put(new PartKey("P6"),
00179 new PartData("Cog", "Red",
00180 new Weight(19.0, Weight.GRAMS),
00181 "London"));
00182 }
00183 }
00184
00189 private void addSuppliers() {
00190
00191 Map suppliers = views.getSupplierMap();
00192 if (suppliers.isEmpty()) {
00193 System.out.println("Adding Suppliers");
00194 suppliers.put(new SupplierKey("S1"),
00195 new SupplierData("Smith", 20, "London"));
00196 suppliers.put(new SupplierKey("S2"),
00197 new SupplierData("Jones", 10, "Paris"));
00198 suppliers.put(new SupplierKey("S3"),
00199 new SupplierData("Blake", 30, "Paris"));
00200 suppliers.put(new SupplierKey("S4"),
00201 new SupplierData("Clark", 20, "London"));
00202 suppliers.put(new SupplierKey("S5"),
00203 new SupplierData("Adams", 30, "Athens"));
00204 }
00205 }
00206
00211 private void addShipments() {
00212
00213 Map shipments = views.getShipmentMap();
00214 if (shipments.isEmpty()) {
00215 System.out.println("Adding Shipments");
00216 shipments.put(new ShipmentKey("P1", "S1"),
00217 new ShipmentData(300));
00218 shipments.put(new ShipmentKey("P2", "S1"),
00219 new ShipmentData(200));
00220 shipments.put(new ShipmentKey("P3", "S1"),
00221 new ShipmentData(400));
00222 shipments.put(new ShipmentKey("P4", "S1"),
00223 new ShipmentData(200));
00224 shipments.put(new ShipmentKey("P5", "S1"),
00225 new ShipmentData(100));
00226 shipments.put(new ShipmentKey("P6", "S1"),
00227 new ShipmentData(100));
00228 shipments.put(new ShipmentKey("P1", "S2"),
00229 new ShipmentData(300));
00230 shipments.put(new ShipmentKey("P2", "S2"),
00231 new ShipmentData(400));
00232 shipments.put(new ShipmentKey("P2", "S3"),
00233 new ShipmentData(200));
00234 shipments.put(new ShipmentKey("P2", "S4"),
00235 new ShipmentData(200));
00236 shipments.put(new ShipmentKey("P4", "S4"),
00237 new ShipmentData(300));
00238 shipments.put(new ShipmentKey("P5", "S4"),
00239 new ShipmentData(400));
00240 }
00241 }
00242
00251 private void printEntries(String label, Iterator iterator) {
00252
00253 System.out.println("\n--- " + label + " ---");
00254 try {
00255 while (iterator.hasNext()) {
00256 Map.Entry entry = (Map.Entry) iterator.next();
00257 System.out.println(entry.getKey().toString());
00258 System.out.println(entry.getValue().toString());
00259 }
00260 } finally {
00261
00262
00263
00264 StoredIterator.close(iterator);
00265 }
00266 }
00267 }