1 package persist.gettingStarted; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileInputStream ; 6 import java.io.FileNotFoundException ; 7 import java.io.IOException ; 8 import java.io.InputStreamReader ; 9 import java.util.ArrayList ; 10 import java.util.List ; 11 12 import com.sleepycat.je.DatabaseException; 13 import com.sleepycat.je.Transaction; 14 15 public class ExampleDatabasePut { 16 17 private static File myDbEnvPath = new File ("/tmp/JEDB"); 18 private static File inventoryFile = new File ("./inventory.txt"); 19 private static File vendorsFile = new File ("./vendors.txt"); 20 21 private DataAccessor da; 22 23 private static MyDbEnv myDbEnv = new MyDbEnv(); 25 26 private static void usage() { 27 System.out.println("ExampleDatabasePut [-h <env directory>]"); 28 System.out.println(" [-i <inventory file>] [-v <vendors file>]"); 29 System.exit(-1); 30 } 31 32 33 public static void main(String args[]) { 34 ExampleDatabasePut edp = new ExampleDatabasePut(); 35 try { 36 edp.run(args); 37 } catch (DatabaseException dbe) { 38 System.err.println("ExampleDatabasePut: " + dbe.toString()); 39 dbe.printStackTrace(); 40 dbe.printStackTrace(); 41 } catch (Exception e) { 42 System.out.println("Exception: " + e.toString()); 43 e.printStackTrace(); 44 } finally { 45 myDbEnv.close(); 46 } 47 System.out.println("All done."); 48 } 49 50 51 private void run(String args[]) 52 throws DatabaseException { 53 parseArgs(args); 55 56 myDbEnv.setup(myDbEnvPath, false); 59 da = new DataAccessor(myDbEnv.getEntityStore()); 62 63 System.out.println("loading vendors db...."); 64 loadVendorsDb(); 65 66 System.out.println("loading inventory db...."); 67 loadInventoryDb(); 68 } 69 70 private void loadVendorsDb() 71 throws DatabaseException { 72 73 List vendors = loadFile(vendorsFile, 8); 78 79 for (int i = 0; i < vendors.size(); i++) { 81 String [] sArray = (String [])vendors.get(i); 82 Vendor theVendor = new Vendor(); 83 theVendor.setVendorName(sArray[0]); 84 theVendor.setAddress(sArray[1]); 85 theVendor.setCity(sArray[2]); 86 theVendor.setState(sArray[3]); 87 theVendor.setZipcode(sArray[4]); 88 theVendor.setBusinessPhoneNumber(sArray[5]); 89 theVendor.setRepName(sArray[6]); 90 theVendor.setRepPhoneNumber(sArray[7]); 91 92 da.vendorByName.put(theVendor); 97 } 98 } 99 100 private void loadInventoryDb() 101 throws DatabaseException { 102 103 List inventoryArray = loadFile(inventoryFile, 6); 108 109 112 113 Transaction txn = myDbEnv.getEnv().beginTransaction(null, null); 116 117 for (int i = 0; i < inventoryArray.size(); i++) { 118 String [] sArray = (String [])inventoryArray.get(i); 119 String sku = sArray[1]; 120 121 Inventory theInventory = new Inventory(); 122 theInventory.setItemName(sArray[0]); 123 theInventory.setSku(sArray[1]); 124 theInventory.setVendorPrice((new Float (sArray[2])).floatValue()); 125 theInventory.setVendorInventory((new Integer (sArray[3])).intValue()); 126 theInventory.setCategory(sArray[4]); 127 theInventory.setVendor(sArray[5]); 128 129 try { 132 da.inventoryBySku.put(txn, theInventory); 133 } catch (DatabaseException dbe) { 134 try { 135 System.out.println("Error putting entry " + 136 sku.getBytes("UTF-8")); 137 } catch (IOException willNeverOccur) {} 138 txn.abort(); 139 throw dbe; 140 } 141 } 142 txn.commit(); 145 } 146 147 148 private static void parseArgs(String args[]) { 149 for(int i = 0; i < args.length; ++i) { 150 if (args[i].startsWith("-")) { 151 switch(args[i].charAt(1)) { 152 case 'h': 153 myDbEnvPath = new File (args[++i]); 154 break; 155 case 'i': 156 inventoryFile = new File (args[++i]); 157 break; 158 case 'v': 159 vendorsFile = new File (args[++i]); 160 break; 161 default: 162 usage(); 163 } 164 } 165 } 166 } 167 168 private List loadFile(File theFile, int numFields) { 169 List <String []> records = new ArrayList <String []>(); 170 try { 171 String theLine = null; 172 FileInputStream fis = new FileInputStream (theFile); 173 BufferedReader br = new BufferedReader (new InputStreamReader (fis)); 174 while((theLine=br.readLine()) != null) { 175 String [] theLineArray = theLine.split("#"); 176 if (theLineArray.length != numFields) { 177 System.out.println("Malformed line found in " + theFile.getPath()); 178 System.out.println("Line was: '" + theLine); 179 System.out.println("length found was: " + theLineArray.length); 180 System.exit(-1); 181 } 182 records.add(theLineArray); 183 } 184 fis.close(); 186 } catch (FileNotFoundException e) { 187 System.err.println(theFile.getPath() + " does not exist."); 188 e.printStackTrace(); 189 usage(); 190 } catch (IOException e) { 191 System.err.println("IO Exception: " + e.toString()); 192 e.printStackTrace(); 193 System.exit(-1); 194 } 195 return records; 196 } 197 198 protected ExampleDatabasePut() {} 199 } 200
| Popular Tags
|