1 4 package je.gettingStarted; 5 6 import java.io.File ; 7 import java.io.IOException ; 8 9 import com.sleepycat.bind.EntryBinding; 10 import com.sleepycat.bind.serial.SerialBinding; 11 import com.sleepycat.bind.tuple.TupleBinding; 12 import com.sleepycat.je.Cursor; 13 import com.sleepycat.je.DatabaseEntry; 14 import com.sleepycat.je.DatabaseException; 15 import com.sleepycat.je.LockMode; 16 import com.sleepycat.je.OperationStatus; 17 import com.sleepycat.je.SecondaryCursor; 18 19 public class ExampleInventoryRead { 20 21 private static File myDbEnvPath = 22 new File ("/tmp/JEDB"); 23 24 private static MyDbEnv myDbEnv = new MyDbEnv(); 26 27 private static TupleBinding inventoryBinding; 28 private static EntryBinding vendorBinding; 29 30 private static String locateItem; 32 33 private static void usage() { 34 System.out.println("ExampleInventoryRead [-h <env directory>]" + 35 "[-s <item to locate>]"); 36 System.exit(-1); 37 } 38 39 public static void main(String args[]) { 40 ExampleInventoryRead eir = new ExampleInventoryRead(); 41 try { 42 eir.run(args); 43 } catch (DatabaseException dbe) { 44 System.err.println("ExampleInventoryRead: " + dbe.toString()); 45 dbe.printStackTrace(); 46 } finally { 47 myDbEnv.close(); 48 } 49 System.out.println("All done."); 50 } 51 52 private void run(String args[]) 53 throws DatabaseException { 54 parseArgs(args); 56 57 myDbEnv.setup(myDbEnvPath, true); 60 inventoryBinding = new InventoryBinding(); 62 vendorBinding = 63 new SerialBinding(myDbEnv.getClassCatalog(), 64 Vendor.class); 65 66 if (locateItem != null) { 67 showItem(); 68 } else { 69 showAllInventory(); 70 } 71 } 72 73 private void showItem() throws DatabaseException { 74 75 SecondaryCursor secCursor = null; 76 try { 77 DatabaseEntry searchKey = 80 new DatabaseEntry(locateItem.getBytes("UTF-8")); 81 82 DatabaseEntry foundKey = new DatabaseEntry(); 85 DatabaseEntry foundData = new DatabaseEntry(); 86 87 secCursor = 89 myDbEnv.getNameIndexDB().openSecondaryCursor(null, null); 90 91 OperationStatus retVal = 93 secCursor.getSearchKey(searchKey, foundKey, 94 foundData, LockMode.DEFAULT); 95 96 while(retVal == OperationStatus.SUCCESS) { 99 Inventory theInventory = 100 (Inventory)inventoryBinding.entryToObject(foundData); 101 displayInventoryRecord(foundKey, theInventory); 102 retVal = secCursor.getNextDup(searchKey, foundKey, 103 foundData, LockMode.DEFAULT); 104 } 105 } catch (Exception e) { 106 System.err.println("Error on inventory secondary cursor:"); 107 System.err.println(e.toString()); 108 e.printStackTrace(); 109 } finally { 110 if (secCursor != null) { 111 secCursor.close(); 112 } 113 } 114 } 115 116 private void showAllInventory() 117 throws DatabaseException { 118 Cursor cursor = myDbEnv.getInventoryDB().openCursor(null, null); 120 121 DatabaseEntry foundKey = new DatabaseEntry(); 123 DatabaseEntry foundData = new DatabaseEntry(); 124 125 try { while (cursor.getNext(foundKey, foundData, 127 LockMode.DEFAULT) == OperationStatus.SUCCESS) { 128 Inventory theInventory = 129 (Inventory)inventoryBinding.entryToObject(foundData); 130 displayInventoryRecord(foundKey, theInventory); 131 } 132 } catch (Exception e) { 133 System.err.println("Error on inventory cursor:"); 134 System.err.println(e.toString()); 135 e.printStackTrace(); 136 } finally { 137 cursor.close(); 138 } 139 } 140 141 private void displayInventoryRecord(DatabaseEntry theKey, 142 Inventory theInventory) 143 throws DatabaseException { 144 145 146 DatabaseEntry searchKey = null; 147 try { 148 String theSKU = new String (theKey.getData(), "UTF-8"); 149 System.out.println(theSKU + ":"); 150 System.out.println("\t " + theInventory.getItemName()); 151 System.out.println("\t " + theInventory.getCategory()); 152 System.out.println("\t " + theInventory.getVendor()); 153 System.out.println("\t\tNumber in stock: " + 154 theInventory.getVendorInventory()); 155 System.out.println("\t\tPrice per unit: " + 156 theInventory.getVendorPrice()); 157 System.out.println("\t\tContact: "); 158 159 searchKey = 160 new DatabaseEntry(theInventory.getVendor().getBytes("UTF-8")); 161 } catch (IOException willNeverOccur) {} 162 DatabaseEntry foundVendor = new DatabaseEntry(); 163 164 if (myDbEnv.getVendorDB().get(null, searchKey, foundVendor, 165 LockMode.DEFAULT) != OperationStatus.SUCCESS) { 166 System.out.println("Could not find vendor: " + 167 theInventory.getVendor() + "."); 168 System.exit(-1); 169 } else { 170 Vendor theVendor = 171 (Vendor)vendorBinding.entryToObject(foundVendor); 172 System.out.println("\t\t " + theVendor.getAddress()); 173 System.out.println("\t\t " + theVendor.getCity() + ", " + 174 theVendor.getState() + " " + theVendor.getZipcode()); 175 System.out.println("\t\t Business Phone: " + 176 theVendor.getBusinessPhoneNumber()); 177 System.out.println("\t\t Sales Rep: " + 178 theVendor.getRepName()); 179 System.out.println("\t\t " + 180 theVendor.getRepPhoneNumber()); 181 } 182 } 183 184 protected ExampleInventoryRead() {} 185 186 private static void parseArgs(String args[]) { 187 for(int i = 0; i < args.length; ++i) { 188 if (args[i].startsWith("-")) { 189 switch(args[i].charAt(1)) { 190 case 'h': 191 myDbEnvPath = new File (args[++i]); 192 break; 193 case 's': 194 locateItem = new String (args[++i]); 195 break; 196 default: 197 usage(); 198 } 199 } 200 } 201 } 202 } 203 | Popular Tags |