001: package persist.gettingStarted;
002:
003: import java.io.File;
004:
005: import com.sleepycat.je.DatabaseException;
006: import com.sleepycat.persist.EntityCursor;
007:
008: public class ExampleInventoryRead {
009:
010: private static File myDbEnvPath = new File("/tmp/JEDB");
011:
012: private DataAccessor da;
013:
014: // Encapsulates the database environment.
015: private static MyDbEnv myDbEnv = new MyDbEnv();
016:
017: // The item to locate if the -s switch is used
018: private static String locateItem;
019:
020: private static void usage() {
021: System.out.println("ExampleInventoryRead [-h <env directory>]"
022: + "[-s <item to locate>]");
023: System.exit(-1);
024: }
025:
026: public static void main(String args[]) {
027: ExampleInventoryRead eir = new ExampleInventoryRead();
028: try {
029: eir.run(args);
030: } catch (DatabaseException dbe) {
031: System.err.println("ExampleInventoryRead: "
032: + dbe.toString());
033: dbe.printStackTrace();
034: } finally {
035: myDbEnv.close();
036: }
037: System.out.println("All done.");
038: }
039:
040: private void run(String args[]) throws DatabaseException {
041: // Parse the arguments list
042: parseArgs(args);
043:
044: myDbEnv.setup(myDbEnvPath, // path to the environment home
045: true); // is this environment read-only?
046:
047: // Open the data accessor. This is used to retrieve
048: // persistent objects.
049: da = new DataAccessor(myDbEnv.getEntityStore());
050:
051: // If a item to locate is provided on the command line,
052: // show just the inventory items using the provided name.
053: // Otherwise, show everything in the inventory.
054: if (locateItem != null) {
055: showItem();
056: } else {
057: showAllInventory();
058: }
059: }
060:
061: // Shows all the inventory items that exist for a given
062: // inventory name.
063: private void showItem() throws DatabaseException {
064:
065: // Use the inventory name secondary key to retrieve
066: // these objects.
067: EntityCursor<Inventory> items = da.inventoryByName.subIndex(
068: locateItem).entities();
069: try {
070: for (Inventory item : items) {
071: displayInventoryRecord(item);
072: }
073: } finally {
074: items.close();
075: }
076: }
077:
078: // Displays all the inventory items in the store
079: private void showAllInventory() throws DatabaseException {
080:
081: // Get a cursor that will walk every
082: // inventory object in the store.
083: EntityCursor<Inventory> items = da.inventoryBySku.entities();
084:
085: try {
086: for (Inventory item : items) {
087: displayInventoryRecord(item);
088: }
089: } finally {
090: items.close();
091: }
092: }
093:
094: private void displayInventoryRecord(Inventory theInventory)
095: throws DatabaseException {
096:
097: System.out.println(theInventory.getSku() + ":");
098: System.out.println("\t " + theInventory.getItemName());
099: System.out.println("\t " + theInventory.getCategory());
100: System.out.println("\t " + theInventory.getVendor());
101: System.out.println("\t\tNumber in stock: "
102: + theInventory.getVendorInventory());
103: System.out.println("\t\tPrice per unit: "
104: + theInventory.getVendorPrice());
105: System.out.println("\t\tContact: ");
106:
107: Vendor theVendor = da.vendorByName
108: .get(theInventory.getVendor());
109: assert theVendor != null;
110:
111: System.out.println("\t\t " + theVendor.getAddress());
112: System.out.println("\t\t " + theVendor.getCity() + ", "
113: + theVendor.getState() + " " + theVendor.getZipcode());
114: System.out.println("\t\t Business Phone: "
115: + theVendor.getBusinessPhoneNumber());
116: System.out.println("\t\t Sales Rep: " + theVendor.getRepName());
117: System.out.println("\t\t "
118: + theVendor.getRepPhoneNumber());
119: }
120:
121: protected ExampleInventoryRead() {
122: }
123:
124: private static void parseArgs(String args[]) {
125: for (int i = 0; i < args.length; ++i) {
126: if (args[i].startsWith("-")) {
127: switch (args[i].charAt(1)) {
128: case 'h':
129: myDbEnvPath = new File(args[++i]);
130: break;
131: case 's':
132: locateItem = args[++i];
133: break;
134: default:
135: usage();
136: }
137: }
138: }
139: }
140: }
|