KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persist > gettingStarted > ExampleInventoryRead


1 package persist.gettingStarted;
2
3 import java.io.File JavaDoc;
4
5 import com.sleepycat.je.DatabaseException;
6 import com.sleepycat.persist.EntityCursor;
7  
8 public class ExampleInventoryRead {
9
10     private static File JavaDoc myDbEnvPath =
11         new File JavaDoc("/tmp/JEDB");
12
13     private DataAccessor da;
14
15     // Encapsulates the database environment.
16
private static MyDbEnv myDbEnv = new MyDbEnv();
17
18     // The item to locate if the -s switch is used
19
private static String JavaDoc 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 JavaDoc 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 JavaDoc args[])
41         throws DatabaseException {
42         // Parse the arguments list
43
parseArgs(args);
44   
45         myDbEnv.setup(myDbEnvPath, // path to the environment home
46
true); // is this environment read-only?
47

48         // Open the data accessor. This is used to retrieve
49
// persistent objects.
50
da = new DataAccessor(myDbEnv.getEntityStore());
51
52         // If a item to locate is provided on the command line,
53
// show just the inventory items using the provided name.
54
// Otherwise, show everything in the inventory.
55
if (locateItem != null) {
56             showItem();
57         } else {
58             showAllInventory();
59         }
60     }
61
62     // Shows all the inventory items that exist for a given
63
// inventory name.
64
private void showItem() throws DatabaseException {
65
66         // Use the inventory name secondary key to retrieve
67
// these objects.
68
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     // Displays all the inventory items in the store
80
private void showAllInventory()
81         throws DatabaseException {
82
83         // Get a cursor that will walk every
84
// inventory object in the store.
85
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 JavaDoc 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 JavaDoc(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