1 8 9 package collections.ship.tuple; 10 11 import java.io.FileNotFoundException ; 12 import java.util.Iterator ; 13 import java.util.Set ; 14 15 import com.sleepycat.collections.TransactionRunner; 16 import com.sleepycat.collections.TransactionWorker; 17 import com.sleepycat.je.DatabaseException; 18 19 35 public class Sample { 36 37 private SampleDatabase db; 38 private SampleViews views; 39 40 43 public static void main(String [] args) { 44 45 System.out.println("\nRunning sample: " + Sample.class); 46 47 String homeDir = "./tmp"; 50 for (int i = 0; i < args.length; i += 1) { 51 if (args[i].equals("-h") && i < args.length - 1) { 52 i += 1; 53 homeDir = args[i]; 54 } else { 55 System.err.println("Usage:\n java " + Sample.class.getName() + 56 "\n [-h <home-directory>]"); 57 System.exit(2); 58 } 59 } 60 61 Sample sample = null; 64 try { 65 sample = new Sample(homeDir); 66 sample.run(); 67 } catch (Exception e) { 68 e.printStackTrace(); 72 } finally { 73 if (sample != null) { 74 try { 75 sample.close(); 77 } catch (Exception e) { 78 System.err.println("Exception during database close:"); 79 e.printStackTrace(); 80 } 81 } 82 } 83 } 84 85 88 private Sample(String homeDir) 89 throws DatabaseException, FileNotFoundException { 90 91 db = new SampleDatabase(homeDir); 92 views = new SampleViews(db); 93 } 94 95 98 private void close() 99 throws DatabaseException { 100 101 db.close(); 102 } 103 104 110 private void run() 111 throws Exception { 112 113 TransactionRunner runner = new TransactionRunner(db.getEnvironment()); 114 runner.run(new PopulateDatabase()); 115 runner.run(new PrintDatabase()); 116 } 117 118 121 private class PopulateDatabase implements TransactionWorker { 122 123 public void doWork() 124 throws Exception { 125 addSuppliers(); 126 addParts(); 127 addShipments(); 128 } 129 } 130 131 138 private class PrintDatabase implements TransactionWorker { 139 140 public void doWork() 141 throws Exception { 142 printValues("Parts", 143 views.getPartSet().iterator()); 144 printValues("Suppliers", 145 views.getSupplierSet().iterator()); 146 printValues("Suppliers for City Paris", 147 views.getSupplierByCityMap().duplicates( 148 "Paris").iterator()); 149 printValues("Shipments", 150 views.getShipmentSet().iterator()); 151 printValues("Shipments for Part P1", 152 views.getShipmentByPartMap().duplicates( 153 new PartKey("P1")).iterator()); 154 printValues("Shipments for Supplier S1", 155 views.getShipmentBySupplierMap().duplicates( 156 new SupplierKey("S1")).iterator()); 157 } 158 } 159 160 164 private void addParts() { 165 166 Set parts = views.getPartSet(); 167 if (parts.isEmpty()) { 168 System.out.println("Adding Parts"); 169 parts.add(new Part("P1", "Nut", "Red", 170 new Weight(12.0, Weight.GRAMS), "London")); 171 parts.add(new Part("P2", "Bolt", "Green", 172 new Weight(17.0, Weight.GRAMS), "Paris")); 173 parts.add(new Part("P3", "Screw", "Blue", 174 new Weight(17.0, Weight.GRAMS), "Rome")); 175 parts.add(new Part("P4", "Screw", "Red", 176 new Weight(14.0, Weight.GRAMS), "London")); 177 parts.add(new Part("P5", "Cam", "Blue", 178 new Weight(12.0, Weight.GRAMS), "Paris")); 179 parts.add(new Part("P6", "Cog", "Red", 180 new Weight(19.0, Weight.GRAMS), "London")); 181 } 182 } 183 184 188 private void addSuppliers() { 189 190 Set suppliers = views.getSupplierSet(); 191 if (suppliers.isEmpty()) { 192 System.out.println("Adding Suppliers"); 193 suppliers.add(new Supplier("S1", "Smith", 20, "London")); 194 suppliers.add(new Supplier("S2", "Jones", 10, "Paris")); 195 suppliers.add(new Supplier("S3", "Blake", 30, "Paris")); 196 suppliers.add(new Supplier("S4", "Clark", 20, "London")); 197 suppliers.add(new Supplier("S5", "Adams", 30, "Athens")); 198 } 199 } 200 201 205 private void addShipments() { 206 207 Set shipments = views.getShipmentSet(); 208 if (shipments.isEmpty()) { 209 System.out.println("Adding Shipments"); 210 shipments.add(new Shipment("P1", "S1", 300)); 211 shipments.add(new Shipment("P2", "S1", 200)); 212 shipments.add(new Shipment("P3", "S1", 400)); 213 shipments.add(new Shipment("P4", "S1", 200)); 214 shipments.add(new Shipment("P5", "S1", 100)); 215 shipments.add(new Shipment("P6", "S1", 100)); 216 shipments.add(new Shipment("P1", "S2", 300)); 217 shipments.add(new Shipment("P2", "S2", 400)); 218 shipments.add(new Shipment("P2", "S3", 200)); 219 shipments.add(new Shipment("P2", "S4", 200)); 220 shipments.add(new Shipment("P4", "S4", 300)); 221 shipments.add(new Shipment("P5", "S4", 400)); 222 } 223 } 224 225 228 private void printValues(String label, Iterator iterator) { 229 230 System.out.println("\n--- " + label + " ---"); 231 while (iterator.hasNext()) { 232 System.out.println(iterator.next().toString()); 233 } 234 } 235 } 236 | Popular Tags |