1 8 9 package collections.ship.factory; 10 11 import java.util.Iterator ; 12 import java.util.Set ; 13 14 import com.sleepycat.collections.TransactionRunner; 15 import com.sleepycat.collections.TransactionWorker; 16 17 34 public class Sample { 35 36 private SampleDatabase db; 37 private SampleViews views; 38 39 42 public static void main(String [] args) { 43 44 System.out.println("\nRunning sample: " + Sample.class); 45 46 String 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 Sample sample = null; 63 try { 64 sample = new Sample(homeDir); 65 sample.run(); 66 } catch (Exception e) { 67 e.printStackTrace(); 71 } finally { 72 if (sample != null) { 73 try { 74 sample.close(); 76 } catch (Exception e) { 77 System.err.println("Exception during database close:"); 78 e.printStackTrace(); 79 } 80 } 81 } 82 } 83 84 87 private Sample(String homeDir) 88 throws Exception { 89 90 db = new SampleDatabase(homeDir); 91 views = new SampleViews(db); 92 } 93 94 97 private void close() 98 throws Exception { 99 100 db.close(); 101 } 102 103 109 private void run() 110 throws Exception { 111 112 TransactionRunner runner = new TransactionRunner(db.getEnvironment()); 113 runner.run(new PopulateDatabase()); 114 runner.run(new PrintDatabase()); 115 } 116 117 120 private class PopulateDatabase implements TransactionWorker { 121 122 public void doWork() 123 throws Exception { 124 addSuppliers(); 125 addParts(); 126 addShipments(); 127 } 128 } 129 130 137 private class PrintDatabase implements TransactionWorker { 138 139 public void doWork() 140 throws Exception { 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 163 private void addParts() { 164 165 Set 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 187 private void addSuppliers() { 188 189 Set 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 204 private void addShipments() { 205 206 Set 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 227 private void printValues(String label, Iterator 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 |