1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>ExampleInventoryRead.java</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="dpl_example.html" title="Chapter��6.��A DPL Example" /> 11 <link rel="prev" href="dpl_exampledatabaseput.html" title="ExampleDatabasePut.java" /> 12 <link rel="next" href="baseapi.html" title="Part��II.��Programming with the Base API" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">ExampleInventoryRead.java</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dpl_exampledatabaseput.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��6.��A DPL Example</th> 23 <td width="20%" align="right">��<a accesskey="n" href="baseapi.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="dpl_exampleinventoryread"></a>ExampleInventoryRead.java</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 <code class="classname">ExampleInventoryRead</code> 38 retrieves 39 inventory information from our entity store and 40 displays it. When it displays each inventory item, it 41 also displays the related vendor contact information. 42 </p> 43 <p> 44 <code class="classname">ExampleInventoryRead</code> 45 can do one of two things. If you provide no search 46 criteria, it displays all of the inventory items in the 47 store. If you provide an item name (using the 48 <code class="literal">-s</code> command line switch), then just 49 those inventory items using that name are displayed. 50 </p> 51 <p> 52 The beginning of our example is almost identical to our 53 <code class="classname">ExampleDatabasePut</code> 54 example program. We 55 repeat that example code here for the sake of 56 completeness. For a complete walk-through of it, see 57 the previous section (<a class="xref" href="dpl_exampledatabaseput.html" title="ExampleDatabasePut.java">ExampleDatabasePut.java</a>). 58 </p> 59 <pre class="programlisting">package persist.gettingStarted; 60 61import java.io.File; 62import java.io.IOException; 63 64import com.sleepycat.db.DatabaseException; 65import com.sleepycat.persist.EntityCursor; 66 67public class ExampleInventoryRead { 68 69 private static File myDbEnvPath = 70 new File("/tmp/JEDB"); 71 72 private DataAccessor da; 73 74 // Encapsulates the database environment. 75 private static MyDbEnv myDbEnv = new MyDbEnv(); 76 77 // The item to locate if the -s switch is used 78 private static String locateItem; 79 80 private static void usage() { 81 System.out.println("ExampleInventoryRead [-h <env directory>]" + 82 "[-s <item to locate>]"); 83 System.exit(-1); 84 } 85 86 public static void main(String args[]) { 87 ExampleInventoryRead eir = new ExampleInventoryRead(); 88 try { 89 eir.run(args); 90 } catch (DatabaseException dbe) { 91 System.err.println("ExampleInventoryRead: " + dbe.toString()); 92 dbe.printStackTrace(); 93 } finally { 94 myDbEnv.close(); 95 } 96 System.out.println("All done."); 97 } 98 99 private void run(String args[]) 100 throws DatabaseException { 101 // Parse the arguments list 102 parseArgs(args); 103 104 myDbEnv.setup(myDbEnvPath, // path to the environment home 105 true); // is this environment read-only? 106 107 // Open the data accessor. This is used to retrieve 108 // persistent objects. 109 da = new DataAccessor(myDbEnv.getEntityStore()); 110 111 // If a item to locate is provided on the command line, 112 // show just the inventory items using the provided name. 113 // Otherwise, show everything in the inventory. 114 if (locateItem != null) { 115 showItem(); 116 } else { 117 showAllInventory(); 118 } 119 } </pre> 120 <p> 121 The first method that we provide is used to show inventory 122 items related to a given inventory name. This method is called 123 only if an inventory name is passed to 124 <code class="classname">ExampleInventoryRead</code> 125 via the <code class="literal">-s</code> option. Given the sample data 126 that we provide with this example, each matching inventory name 127 will result in the display of three inventory objects. 128 </p> 129 <p> 130 To display these objects we use the 131 <code class="classname">Inventory</code> class' 132 <code class="literal">inventoryByName</code> secondary index to retrieve 133 an <code class="classname">EntityCursor</code>, and then we iterate 134 over the resulting objects using the cursor. 135 </p> 136 <p> 137 Notice that this method calls 138 <code class="methodname">displayInventoryRecord()</code> 139 to display each individual object. We show this 140 method a little later in the example. 141 </p> 142 <pre class="programlisting"> // Shows all the inventory items that exist for a given 143 // inventory name. 144 private void showItem() throws DatabaseException { 145 146 // Use the inventory name secondary key to retrieve 147 // these objects. 148 EntityCursor<Inventory> items = 149 da.inventoryByName.subIndex(locateItem).entities(); 150 try { 151 for (Inventory item : items) { 152 displayInventoryRecord(item); 153 } 154 } finally { 155 items.close(); 156 } 157 } </pre> 158 <p> 159 Next we implement <code class="methodname">showAllInventory()</code>, 160 which shows all of the <code class="classname">Inventory</code> 161 objects in the store. To do this, we 162 obtain an <code class="classname">EntityCursor</code> 163 from the <code class="classname">Inventory</code> class' 164 primary index and, again, we iterate using that cursor. 165 </p> 166 <pre class="programlisting"> // Displays all the inventory items in the store 167 private void showAllInventory() 168 throws DatabaseException { 169 170 // Get a cursor that will walk every 171 // inventory object in the store. 172 EntityCursor<Inventory> items = 173 da.inventoryBySku.entities(); 174 175 try { 176 for (Inventory item : items) { 177 displayInventoryRecord(item); 178 } 179 } finally { 180 items.close(); 181 } 182 } </pre> 183 <p> 184 Now we implement 185 <code class="methodname">displayInventoryRecord()</code>. This 186 uses the getter methods on the <code class="classname">Inventory</code> 187 class to obtain the information that we want to display. 188 The only thing interesting about this method is that we 189 obtain <code class="classname">Vendor</code> objects within. 190 The vendor objects are retrieved <code class="classname">Vendor</code> 191 objects using their primary index. We get the key 192 for the retrieval from the <code class="classname">Inventory</code> 193 object that we are displaying at the time. 194 </p> 195 <pre class="programlisting"> private void displayInventoryRecord(Inventory theInventory) 196 throws DatabaseException { 197 198 System.out.println(theInventory.getSku() + ":"); 199 System.out.println("\t " + theInventory.getItemName()); 200 System.out.println("\t " + theInventory.getCategory()); 201 System.out.println("\t " + theInventory.getVendor()); 202 System.out.println("\t\tNumber in stock: " + 203 theInventory.getVendorInventory()); 204 System.out.println("\t\tPrice per unit: " + 205 theInventory.getVendorPrice()); 206 System.out.println("\t\tContact: "); 207 208 Vendor theVendor = 209 da.vendorByName.get(theInventory.getVendor()); 210 assert theVendor != null; 211 212 System.out.println("\t\t " + theVendor.getAddress()); 213 System.out.println("\t\t " + theVendor.getCity() + ", " + 214 theVendor.getState() + " " + theVendor.getZipcode()); 215 System.out.println("\t\t Business Phone: " + 216 theVendor.getBusinessPhoneNumber()); 217 System.out.println("\t\t Sales Rep: " + 218 theVendor.getRepName()); 219 System.out.println("\t\t " + 220 theVendor.getRepPhoneNumber()); 221 } </pre> 222 <p> 223 The last remaining parts of the example are used to parse 224 the command line. This is not very 225 interesting for our purposes here, but we show it anyway 226 for the sake of completeness. 227 </p> 228 <pre class="programlisting"> protected ExampleInventoryRead() {} 229 230 private static void parseArgs(String args[]) { 231 for(int i = 0; i < args.length; ++i) { 232 if (args[i].startsWith("-")) { 233 switch(args[i].charAt(1)) { 234 case 'h': 235 myDbEnvPath = new File(args[++i]); 236 break; 237 case 's': 238 locateItem = args[++i]; 239 break; 240 default: 241 usage(); 242 } 243 } 244 } 245 } 246} </pre> 247 </div> 248 <div class="navfooter"> 249 <hr /> 250 <table width="100%" summary="Navigation footer"> 251 <tr> 252 <td width="40%" align="left"><a accesskey="p" href="dpl_exampledatabaseput.html">Prev</a>��</td> 253 <td width="20%" align="center"> 254 <a accesskey="u" href="dpl_example.html">Up</a> 255 </td> 256 <td width="40%" align="right">��<a accesskey="n" href="baseapi.html">Next</a></td> 257 </tr> 258 <tr> 259 <td width="40%" align="left" valign="top">ExampleDatabasePut.java��</td> 260 <td width="20%" align="center"> 261 <a accesskey="h" href="index.html">Home</a> 262 </td> 263 <td width="40%" align="right" valign="top">��Part��II.��Programming with the Base API</td> 264 </tr> 265 </table> 266 </div> 267 </body> 268</html> 269