1// File: ExampleDatabaseLoad.java 2 3package db.GettingStarted; 4 5import java.io.BufferedReader; 6import java.io.File; 7import java.io.FileInputStream; 8import java.io.FileNotFoundException; 9import java.io.IOException; 10import java.io.InputStreamReader; 11import java.util.ArrayList; 12import java.util.List; 13import java.util.StringTokenizer; 14import java.util.Vector; 15 16import com.sleepycat.bind.EntryBinding; 17import com.sleepycat.bind.serial.SerialBinding; 18import com.sleepycat.bind.tuple.TupleBinding; 19import com.sleepycat.db.DatabaseEntry; 20import com.sleepycat.db.DatabaseException; 21 22public class ExampleDatabaseLoad { 23 24 private static String myDbsPath = "./"; 25 private static File inventoryFile = new File("./inventory.txt"); 26 private static File vendorsFile = new File("./vendors.txt"); 27 28 // DatabaseEntries used for loading records 29 private static DatabaseEntry theKey = new DatabaseEntry(); 30 private static DatabaseEntry theData = new DatabaseEntry(); 31 32 // Encapsulates the databases. 33 private static MyDbs myDbs = new MyDbs(); 34 35 private static void usage() { 36 System.out.println("ExampleDatabaseLoad [-h <database home>]"); 37 System.out.println(" [-i <inventory file>] [-v <vendors file>]"); 38 System.exit(-1); 39 } 40 41 42 public static void main(String args[]) { 43 ExampleDatabaseLoad edl = new ExampleDatabaseLoad(); 44 try { 45 edl.run(args); 46 } catch (DatabaseException dbe) { 47 System.err.println("ExampleDatabaseLoad: " + dbe.toString()); 48 dbe.printStackTrace(); 49 } catch (Exception e) { 50 System.out.println("Exception: " + e.toString()); 51 e.printStackTrace(); 52 } finally { 53 myDbs.close(); 54 } 55 System.out.println("All done."); 56 } 57 58 59 private void run(String args[]) 60 throws DatabaseException { 61 // Parse the arguments list 62 parseArgs(args); 63 64 myDbs.setup(myDbsPath); 65 66 System.out.println("loading vendors db...."); 67 loadVendorsDb(); 68 69 System.out.println("loading inventory db...."); 70 loadInventoryDb(); 71 } 72 73 74 private void loadVendorsDb() 75 throws DatabaseException { 76 77 // loadFile opens a flat-text file that contains our data 78 // and loads it into a list for us to work with. The integer 79 // parameter represents the number of fields expected in the 80 // file. 81 List vendors = loadFile(vendorsFile, 8); 82 83 // Now load the data into the database. The vendor's name is the 84 // key, and the data is a Vendor class object. 85 86 // Need a serial binding for the data 87 EntryBinding dataBinding = 88 new SerialBinding(myDbs.getClassCatalog(), Vendor.class); 89 90 for (int i = 0; i < vendors.size(); i++) { 91 String[] sArray = (String[])vendors.get(i); 92 Vendor theVendor = new Vendor(); 93 theVendor.setVendorName(sArray[0]); 94 theVendor.setAddress(sArray[1]); 95 theVendor.setCity(sArray[2]); 96 theVendor.setState(sArray[3]); 97 theVendor.setZipcode(sArray[4]); 98 theVendor.setBusinessPhoneNumber(sArray[5]); 99 theVendor.setRepName(sArray[6]); 100 theVendor.setRepPhoneNumber(sArray[7]); 101 102 // The key is the vendor's name. 103 // ASSUMES THE VENDOR'S NAME IS UNIQUE! 104 String vendorName = theVendor.getVendorName(); 105 try { 106 theKey = new DatabaseEntry(vendorName.getBytes("UTF-8")); 107 } catch (IOException willNeverOccur) {} 108 109 // Convert the Vendor object to a DatabaseEntry object 110 // using our SerialBinding 111 dataBinding.objectToEntry(theVendor, theData); 112 113 // Put it in the database. 114 myDbs.getVendorDB().put(null, theKey, theData); 115 } 116 } 117 118 119 private void loadInventoryDb() 120 throws DatabaseException { 121 122 // loadFile opens a flat-text file that contains our data 123 // and loads it into a list for us to work with. The integer 124 // parameter represents the number of fields expected in the 125 // file. 126 List inventoryArray = loadFile(inventoryFile, 6); 127 128 // Now load the data into the database. The item's sku is the 129 // key, and the data is an Inventory class object. 130 131 // Need a tuple binding for the Inventory class. 132 TupleBinding inventoryBinding = new InventoryBinding(); 133 134 for (int i = 0; i < inventoryArray.size(); i++) { 135 String[] sArray = (String[])inventoryArray.get(i); 136 String sku = sArray[1]; 137 try { 138 theKey = new DatabaseEntry(sku.getBytes("UTF-8")); 139 } catch (IOException willNeverOccur) {} 140 141 Inventory theInventory = new Inventory(); 142 theInventory.setItemName(sArray[0]); 143 theInventory.setSku(sArray[1]); 144 theInventory.setVendorPrice((new Float(sArray[2])).floatValue()); 145 theInventory.setVendorInventory((new Integer(sArray[3])).intValue()); 146 theInventory.setCategory(sArray[4]); 147 theInventory.setVendor(sArray[5]); 148 149 // Place the Vendor object on the DatabaseEntry object using our 150 // the tuple binding we implemented in InventoryBinding.java 151 inventoryBinding.objectToEntry(theInventory, theData); 152 153 // Put it in the database. Note that this causes our secondary database 154 // to be automatically updated for us. 155 myDbs.getInventoryDB().put(null, theKey, theData); 156 } 157 } 158 159 160 private static void parseArgs(String args[]) { 161 for(int i = 0; i < args.length; ++i) { 162 if (args[i].startsWith("-")) { 163 switch(args[i].charAt(1)) { 164 case 'h': 165 myDbsPath = new String(args[++i]); 166 break; 167 case 'i': 168 inventoryFile = new File(args[++i]); 169 break; 170 case 'v': 171 vendorsFile = new File(args[++i]); 172 break; 173 default: 174 usage(); 175 } 176 } 177 } 178 } 179 180 181 private List loadFile(File theFile, int numFields) { 182 List records = new ArrayList(); 183 try { 184 String theLine = null; 185 FileInputStream fis = new FileInputStream(theFile); 186 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 187 while((theLine=br.readLine()) != null) { 188 String[] theLineArray = splitString(theLine, "#"); 189 if (theLineArray.length != numFields) { 190 System.out.println("Malformed line found in " + theFile.getPath()); 191 System.out.println("Line was: '" + theLine); 192 System.out.println("length found was: " + theLineArray.length); 193 System.exit(-1); 194 } 195 records.add(theLineArray); 196 } 197 fis.close(); 198 } catch (FileNotFoundException e) { 199 System.err.println(theFile.getPath() + " does not exist."); 200 e.printStackTrace(); 201 usage(); 202 } catch (IOException e) { 203 System.err.println("IO Exception: " + e.toString()); 204 e.printStackTrace(); 205 System.exit(-1); 206 } 207 return records; 208 } 209 210 211 private static String[] splitString(String s, String delimiter) { 212 Vector resultVector = new Vector(); 213 StringTokenizer tokenizer = new StringTokenizer(s, delimiter); 214 while (tokenizer.hasMoreTokens()) 215 resultVector.add(tokenizer.nextToken()); 216 String[] resultArray = new String[resultVector.size()]; 217 resultVector.copyInto(resultArray); 218 return resultArray; 219 } 220 221 222 protected ExampleDatabaseLoad() {} 223} 224