1 package persist.gettingStarted; 2 3 import java.io.File ; 4 5 import com.sleepycat.je.DatabaseException; 6 import com.sleepycat.persist.EntityCursor; 7 8 public class ExampleInventoryRead { 9 10 private static File myDbEnvPath = 11 new File ("/tmp/JEDB"); 12 13 private DataAccessor da; 14 15 private static MyDbEnv myDbEnv = new MyDbEnv(); 17 18 private static String locateItem; 20 21 private static void usage() { 22 System.out.println("ExampleInventoryRead [-h <env directory>]" + 23 "[-s <item to locate>]"); 24 System.exit(-1); 25 } 26 27 public static void main(String args[]) { 28 ExampleInventoryRead eir = new ExampleInventoryRead(); 29 try { 30 eir.run(args); 31 } catch (DatabaseException dbe) { 32 System.err.println("ExampleInventoryRead: " + dbe.toString()); 33 dbe.printStackTrace(); 34 } finally { 35 myDbEnv.close(); 36 } 37 System.out.println("All done."); 38 } 39 40 private void run(String args[]) 41 throws DatabaseException { 42 parseArgs(args); 44 45 myDbEnv.setup(myDbEnvPath, true); 48 da = new DataAccessor(myDbEnv.getEntityStore()); 51 52 if (locateItem != null) { 56 showItem(); 57 } else { 58 showAllInventory(); 59 } 60 } 61 62 private void showItem() throws DatabaseException { 65 66 EntityCursor<Inventory> items = 69 da.inventoryByName.subIndex(locateItem).entities(); 70 try { 71 for (Inventory item : items) { 72 displayInventoryRecord(item); 73 } 74 } finally { 75 items.close(); 76 } 77 } 78 79 private void showAllInventory() 81 throws DatabaseException { 82 83 EntityCursor<Inventory> items = 86 da.inventoryBySku.entities(); 87 88 try { 89 for (Inventory item : items) { 90 displayInventoryRecord(item); 91 } 92 } finally { 93 items.close(); 94 } 95 } 96 97 private void displayInventoryRecord(Inventory theInventory) 98 throws DatabaseException { 99 100 System.out.println(theInventory.getSku() + ":"); 101 System.out.println("\t " + theInventory.getItemName()); 102 System.out.println("\t " + theInventory.getCategory()); 103 System.out.println("\t " + theInventory.getVendor()); 104 System.out.println("\t\tNumber in stock: " + 105 theInventory.getVendorInventory()); 106 System.out.println("\t\tPrice per unit: " + 107 theInventory.getVendorPrice()); 108 System.out.println("\t\tContact: "); 109 110 Vendor theVendor = 111 da.vendorByName.get(theInventory.getVendor()); 112 assert theVendor != null; 113 114 System.out.println("\t\t " + theVendor.getAddress()); 115 System.out.println("\t\t " + theVendor.getCity() + ", " + 116 theVendor.getState() + " " + theVendor.getZipcode()); 117 System.out.println("\t\t Business Phone: " + 118 theVendor.getBusinessPhoneNumber()); 119 System.out.println("\t\t Sales Rep: " + 120 theVendor.getRepName()); 121 System.out.println("\t\t " + 122 theVendor.getRepPhoneNumber()); 123 } 124 125 protected ExampleInventoryRead() {} 126 127 private static void parseArgs(String args[]) { 128 for(int i = 0; i < args.length; ++i) { 129 if (args[i].startsWith("-")) { 130 switch(args[i].charAt(1)) { 131 case 'h': 132 myDbEnvPath = new File (args[++i]); 133 break; 134 case 's': 135 locateItem = args[++i]; 136 break; 137 default: 138 usage(); 139 } 140 } 141 } 142 } 143 } 144 | Popular Tags |