1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
package persist.gettingStarted;
import java.io.File;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.persist.EntityCursor;
public class ExampleInventoryRead {
private static File myDbEnvPath =
new File("/tmp/JEDB");
private DataAccessor da;
// Encapsulates the database environment.
private static MyDbEnv myDbEnv = new MyDbEnv();
// The item to locate if the -s switch is used
private static String locateItem;
private static void usage() {
System.out.println("ExampleInventoryRead [-h <env directory>]" +
"[-s <item to locate>]");
System.exit(-1);
}
public static void main(String args[]) {
ExampleInventoryRead eir = new ExampleInventoryRead();
try {
eir.run(args);
} catch (DatabaseException dbe) {
System.err.println("ExampleInventoryRead: " + dbe.toString());
dbe.printStackTrace();
} finally {
myDbEnv.close();
}
System.out.println("All done.");
}
private void run(String args[])
throws DatabaseException {
// Parse the arguments list
parseArgs(args);
myDbEnv.setup(myDbEnvPath, // path to the environment home
true); // is this environment read-only?
// Open the data accessor. This is used to retrieve
// persistent objects.
da = new DataAccessor(myDbEnv.getEntityStore());
// If a item to locate is provided on the command line,
// show just the inventory items using the provided name.
// Otherwise, show everything in the inventory.
if (locateItem != null) {
showItem();
} else {
showAllInventory();
}
}
// Shows all the inventory items that exist for a given
// inventory name.
private void showItem() throws DatabaseException {
// Use the inventory name secondary key to retrieve
// these objects.
EntityCursor<Inventory> items =
da.inventoryByName.subIndex(locateItem).entities();
try {
for (Inventory item : items) {
displayInventoryRecord(item);
}
} finally {
items.close();
}
}
// Displays all the inventory items in the store
private void showAllInventory()
throws DatabaseException {
// Get a cursor that will walk every
// inventory object in the store.
EntityCursor<Inventory> items =
da.inventoryBySku.entities();
try {
for (Inventory item : items) {
displayInventoryRecord(item);
}
} finally {
items.close();
}
}
private void displayInventoryRecord(Inventory theInventory)
throws DatabaseException {
System.out.println(theInventory.getSku() + ":");
System.out.println("\t " + theInventory.getItemName());
System.out.println("\t " + theInventory.getCategory());
System.out.println("\t " + theInventory.getVendor());
System.out.println("\t\tNumber in stock: " +
theInventory.getVendorInventory());
System.out.println("\t\tPrice per unit: " +
theInventory.getVendorPrice());
System.out.println("\t\tContact: ");
Vendor theVendor =
da.vendorByName.get(theInventory.getVendor());
assert theVendor != null;
System.out.println("\t\t " + theVendor.getAddress());
System.out.println("\t\t " + theVendor.getCity() + ", " +
theVendor.getState() + " " + theVendor.getZipcode());
System.out.println("\t\t Business Phone: " +
theVendor.getBusinessPhoneNumber());
System.out.println("\t\t Sales Rep: " +
theVendor.getRepName());
System.out.println("\t\t " +
theVendor.getRepPhoneNumber());
}
protected ExampleInventoryRead() {}
private static void parseArgs(String args[]) {
for(int i = 0; i < args.length; ++i) {
if (args[i].startsWith("-")) {
switch(args[i].charAt(1)) {
case 'h':
myDbEnvPath = new File(args[++i]);
break;
case 's':
locateItem = args[++i];
break;
default:
usage();
}
}
}
}
}
|