KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > je > gettingStarted > ExampleInventoryRead


1 // file ExampleInventoryRead
2
// $Id: ExampleInventoryRead.java,v 1.8 2006/04/25 20:08:54 sarette Exp $
3

4 package je.gettingStarted;
5
6 import java.io.File JavaDoc;
7 import java.io.IOException JavaDoc;
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 JavaDoc myDbEnvPath =
22         new File JavaDoc("/tmp/JEDB");
23
24     // Encapsulates the database environment and databases.
25
private static MyDbEnv myDbEnv = new MyDbEnv();
26
27     private static TupleBinding inventoryBinding;
28     private static EntryBinding vendorBinding;
29
30     // The item to locate if the -s switch is used
31
private static String JavaDoc 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 JavaDoc 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 JavaDoc args[])
53         throws DatabaseException {
54         // Parse the arguments list
55
parseArgs(args);
56   
57         myDbEnv.setup(myDbEnvPath, // path to the environment home
58
true); // is this environment read-only?
59

60         // Setup our bindings.
61
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             // searchKey is the key that we want to find in the
78
// secondary db.
79
DatabaseEntry searchKey =
80                 new DatabaseEntry(locateItem.getBytes("UTF-8"));
81
82             // foundKey and foundData are populated from the primary
83
// entry that is associated with the secondary db key.
84
DatabaseEntry foundKey = new DatabaseEntry();
85             DatabaseEntry foundData = new DatabaseEntry();
86
87             // open a secondary cursor
88
secCursor =
89                 myDbEnv.getNameIndexDB().openSecondaryCursor(null, null);
90
91             // Search for the secondary database entry.
92
OperationStatus retVal =
93                 secCursor.getSearchKey(searchKey, foundKey,
94                     foundData, LockMode.DEFAULT);
95
96             // Display the entry, if one is found. Repeat until no more
97
// secondary duplicate entries are found
98
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 JavaDoc 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         // Get a cursor
119
Cursor cursor = myDbEnv.getInventoryDB().openCursor(null, null);
120
121         // DatabaseEntry objects used for reading records
122
DatabaseEntry foundKey = new DatabaseEntry();
123         DatabaseEntry foundData = new DatabaseEntry();
124
125         try { // always want to make sure the cursor gets closed
126
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 JavaDoc 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 JavaDoc theSKU = new String JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc(args[++i]);
192                         break;
193                     case 's':
194                         locateItem = new String JavaDoc(args[++i]);
195                         break;
196                     default:
197                         usage();
198                 }
199             }
200         }
201     }
202 }
203
Popular Tags