Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

ExampleDatabaseLoad.java

00001 // File: ExampleDatabaseLoad.java
00002 
00003 package db.GettingStarted;
00004 
00005 import java.io.BufferedReader;
00006 import java.io.File;
00007 import java.io.FileInputStream;
00008 import java.io.FileNotFoundException;
00009 import java.io.IOException;
00010 import java.io.InputStreamReader;
00011 import java.util.ArrayList;
00012 import java.util.StringTokenizer;
00013 import java.util.Vector;
00014 
00015 import com.sleepycat.bind.EntryBinding;
00016 import com.sleepycat.bind.serial.SerialBinding;
00017 import com.sleepycat.bind.tuple.TupleBinding;
00018 import com.sleepycat.db.DatabaseEntry;
00019 import com.sleepycat.db.DatabaseException;
00020 
00021 public class ExampleDatabaseLoad {
00022 
00023     private static String myDbsPath = "./";
00024     private static File inventoryFile = new File("./inventory.txt");
00025     private static File vendorsFile = new File("./vendors.txt");
00026 
00027     // DatabaseEntries used for loading records
00028     private static DatabaseEntry theKey = new DatabaseEntry();
00029     private static DatabaseEntry theData = new DatabaseEntry();
00030 
00031     // Encapsulates the databases.
00032     private static MyDbs myDbs = new MyDbs();
00033 
00034     private static void usage() {
00035         System.out.println("ExampleDatabaseLoad [-h <database home>]");
00036         System.out.println("      [-s <selections file>] [-v <vendors file>]");
00037         System.exit(-1);
00038     }
00039 
00040 
00041     public static void main(String args[]) {
00042         ExampleDatabaseLoad edl = new ExampleDatabaseLoad();
00043         try {
00044             edl.run(args);
00045         } catch (DatabaseException dbe) {
00046             System.err.println("ExampleDatabaseLoad: " + dbe.toString());
00047             dbe.printStackTrace();
00048         } catch (Exception e) {
00049             System.out.println("Exception: " + e.toString());
00050             e.printStackTrace();
00051         } finally {
00052             myDbs.close();
00053         }
00054         System.out.println("All done.");
00055     }
00056 
00057 
00058     private void run(String args[])
00059         throws DatabaseException {
00060         // Parse the arguments list
00061         parseArgs(args);
00062 
00063         myDbs.setup(myDbsPath);
00064 
00065         System.out.println("loading vendors db....");
00066         loadVendorsDb();
00067 
00068         System.out.println("loading inventory db....");
00069         loadInventoryDb();
00070     }
00071 
00072 
00073     private void loadVendorsDb()
00074             throws DatabaseException {
00075 
00076         // loadFile opens a flat-text file that contains our data
00077         // and loads it into a list for us to work with. The integer
00078         // parameter represents the number of fields expected in the
00079         // file.
00080         ArrayList vendors = loadFile(vendorsFile, 8);
00081 
00082         // Now load the data into the database. The vendor's name is the
00083         // key, and the data is a Vendor class object.
00084 
00085         // Need a serial binding for the data
00086         EntryBinding dataBinding =
00087             new SerialBinding(myDbs.getClassCatalog(), Vendor.class);
00088 
00089         for (int i = 0; i < vendors.size(); i++) {
00090             String[] sArray = (String[])vendors.get(i);
00091             Vendor theVendor = new Vendor();
00092             theVendor.setVendorName(sArray[0]);
00093             theVendor.setAddress(sArray[1]);
00094             theVendor.setCity(sArray[2]);
00095             theVendor.setState(sArray[3]);
00096             theVendor.setZipcode(sArray[4]);
00097             theVendor.setBusinessPhoneNumber(sArray[5]);
00098             theVendor.setRepName(sArray[6]);
00099             theVendor.setRepPhoneNumber(sArray[7]);
00100 
00101             // The key is the vendor's name.
00102             // ASSUMES THE VENDOR'S NAME IS UNIQUE!
00103             String vendorName = theVendor.getVendorName();
00104             try {
00105                 theKey = new DatabaseEntry(vendorName.getBytes("UTF-8"));
00106             } catch (IOException willNeverOccur) {}
00107 
00108             // Convert the Vendor object to a DatabaseEntry object
00109             // using our SerialBinding
00110             dataBinding.objectToEntry(theVendor, theData);
00111 
00112             // Put it in the database.
00113             myDbs.getVendorDB().put(null, theKey, theData);
00114         }
00115     }
00116     
00117 
00118     private void loadInventoryDb()
00119         throws DatabaseException {
00120 
00121         // loadFile opens a flat-text file that contains our data
00122         // and loads it into a list for us to work with. The integer
00123         // parameter represents the number of fields expected in the
00124         // file.
00125         ArrayList inventoryArray = loadFile(inventoryFile, 6);
00126 
00127         // Now load the data into the database. The item's sku is the
00128         // key, and the data is an Inventory class object.
00129 
00130         // Need a tuple binding for the Inventory class.
00131         TupleBinding inventoryBinding = new InventoryBinding();
00132 
00133         for (int i = 0; i < inventoryArray.size(); i++) {
00134             String[] sArray = (String[])inventoryArray.get(i);
00135             String sku = sArray[1];
00136             try {
00137                 theKey = new DatabaseEntry(sku.getBytes("UTF-8"));
00138             } catch (IOException willNeverOccur) {}
00139 
00140             Inventory theInventory = new Inventory();
00141             theInventory.setItemName(sArray[0]);
00142             theInventory.setSku(sArray[1]);
00143             theInventory.setVendorPrice((new Float(sArray[2])).floatValue());
00144             theInventory.setVendorInventory((new Integer(sArray[3])).intValue());
00145             theInventory.setCategory(sArray[4]);
00146             theInventory.setVendor(sArray[5]);
00147 
00148             // Place the Vendor object on the DatabaseEntry object using our
00149             // the tuple binding we implemented in InventoryBinding.java
00150             inventoryBinding.objectToEntry(theInventory, theData);
00151 
00152             // Put it in the database. Note that this causes our secondary database
00153             // to be automatically updated for us.
00154             myDbs.getInventoryDB().put(null, theKey, theData);
00155         }
00156     }
00157 
00158 
00159     private static void parseArgs(String args[]) {
00160         int nArgs = args.length;
00161         for(int i = 0; i < args.length; ++i) {
00162             if (args[i].startsWith("-")) {
00163                 switch(args[i].charAt(1)) {
00164                   case 'h':
00165                     if (i < nArgs) {
00166                         myDbsPath = new String(args[++i]);
00167                     }
00168                     break;
00169                   case 'i':
00170                     if (i < nArgs) {
00171                         inventoryFile = new File(args[++i]);
00172                     }
00173                     break;
00174                   case 'v':
00175                     if (i < nArgs) {
00176                         vendorsFile = new File(args[++i]);
00177                     }
00178                     break;
00179                   default:
00180                     usage();
00181                 }
00182             }
00183         }
00184     }
00185 
00186 
00187     private ArrayList loadFile(File theFile, int numFields) {
00188         ArrayList records = new ArrayList();
00189         try {
00190             String theLine = null;
00191             FileInputStream fis = new FileInputStream(theFile);
00192             BufferedReader br = new BufferedReader(new InputStreamReader(fis));
00193             while((theLine=br.readLine()) != null) {
00194                 String[] theLineArray = splitString(theLine, "#");
00195                 if (theLineArray.length != numFields) {
00196                     System.out.println("Malformed line found in " + theFile.getPath());
00197                     System.out.println("Line was: '" + theLine);
00198                     System.out.println("length found was: " + theLineArray.length);
00199                     System.exit(-1);
00200                 }
00201                 records.add(theLineArray);
00202             }
00203         } catch (FileNotFoundException e) {
00204             System.err.println(theFile.getPath() + " does not exist.");
00205             e.printStackTrace();
00206             usage();
00207         } catch (IOException e)  {
00208             System.err.println("IO Exception: " + e.toString());
00209             e.printStackTrace();
00210             System.exit(-1);
00211         }
00212         return records;
00213     }
00214 
00215 
00216     private static String[] splitString(String s, String delimiter) {
00217         Vector resultVector = new Vector();
00218         StringTokenizer tokenizer = new StringTokenizer(s, delimiter);
00219         while (tokenizer.hasMoreTokens())
00220             resultVector.add(tokenizer.nextToken());
00221         String[] resultArray = new String[resultVector.size()];
00222         resultVector.copyInto(resultArray);
00223         return resultArray;
00224     }
00225 
00226 
00227     protected ExampleDatabaseLoad() {}
00228 }

Generated on Sun Dec 25 12:14:27 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2