001: package persist.gettingStarted;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.FileNotFoundException;
007: import java.io.IOException;
008: import java.io.InputStreamReader;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import com.sleepycat.je.DatabaseException;
013: import com.sleepycat.je.Transaction;
014:
015: public class ExampleDatabasePut {
016:
017: private static File myDbEnvPath = new File("/tmp/JEDB");
018: private static File inventoryFile = new File("./inventory.txt");
019: private static File vendorsFile = new File("./vendors.txt");
020:
021: private DataAccessor da;
022:
023: // Encapsulates the environment and data store.
024: private static MyDbEnv myDbEnv = new MyDbEnv();
025:
026: private static void usage() {
027: System.out.println("ExampleDatabasePut [-h <env directory>]");
028: System.out
029: .println(" [-i <inventory file>] [-v <vendors file>]");
030: System.exit(-1);
031: }
032:
033: public static void main(String args[]) {
034: ExampleDatabasePut edp = new ExampleDatabasePut();
035: try {
036: edp.run(args);
037: } catch (DatabaseException dbe) {
038: System.err.println("ExampleDatabasePut: " + dbe.toString());
039: dbe.printStackTrace();
040: dbe.printStackTrace();
041: } catch (Exception e) {
042: System.out.println("Exception: " + e.toString());
043: e.printStackTrace();
044: } finally {
045: myDbEnv.close();
046: }
047: System.out.println("All done.");
048: }
049:
050: private void run(String args[]) throws DatabaseException {
051: // Parse the arguments list
052: parseArgs(args);
053:
054: myDbEnv.setup(myDbEnvPath, // Path to the environment home
055: false); // Environment read-only?
056:
057: // Open the data accessor. This is used to store
058: // persistent objects.
059: da = new DataAccessor(myDbEnv.getEntityStore());
060:
061: System.out.println("loading vendors db....");
062: loadVendorsDb();
063:
064: System.out.println("loading inventory db....");
065: loadInventoryDb();
066: }
067:
068: private void loadVendorsDb() throws DatabaseException {
069:
070: // loadFile opens a flat-text file that contains our data
071: // and loads it into a list for us to work with. The integer
072: // parameter represents the number of fields expected in the
073: // file.
074: List vendors = loadFile(vendorsFile, 8);
075:
076: // Now load the data into the store.
077: for (int i = 0; i < vendors.size(); i++) {
078: String[] sArray = (String[]) vendors.get(i);
079: Vendor theVendor = new Vendor();
080: theVendor.setVendorName(sArray[0]);
081: theVendor.setAddress(sArray[1]);
082: theVendor.setCity(sArray[2]);
083: theVendor.setState(sArray[3]);
084: theVendor.setZipcode(sArray[4]);
085: theVendor.setBusinessPhoneNumber(sArray[5]);
086: theVendor.setRepName(sArray[6]);
087: theVendor.setRepPhoneNumber(sArray[7]);
088:
089: // Put it in the store. Because we do not explicitly set
090: // a transaction here, and because the store was opened
091: // with transactional support, auto commit is used for each
092: // write to the store.
093: da.vendorByName.put(theVendor);
094: }
095: }
096:
097: private void loadInventoryDb() throws DatabaseException {
098:
099: // loadFile opens a flat-text file that contains our data
100: // and loads it into a list for us to work with. The integer
101: // parameter represents the number of fields expected in the
102: // file.
103: List inventoryArray = loadFile(inventoryFile, 6);
104:
105: // Now load the data into the store. The item's sku is the
106: // key, and the data is an Inventory class object.
107:
108: // Start a transaction. All inventory items get loaded using a
109: // single transaction.
110: Transaction txn = myDbEnv.getEnv().beginTransaction(null, null);
111:
112: for (int i = 0; i < inventoryArray.size(); i++) {
113: String[] sArray = (String[]) inventoryArray.get(i);
114: String sku = sArray[1];
115:
116: Inventory theInventory = new Inventory();
117: theInventory.setItemName(sArray[0]);
118: theInventory.setSku(sArray[1]);
119: theInventory.setVendorPrice((new Float(sArray[2]))
120: .floatValue());
121: theInventory.setVendorInventory((new Integer(sArray[3]))
122: .intValue());
123: theInventory.setCategory(sArray[4]);
124: theInventory.setVendor(sArray[5]);
125:
126: // Put it in the store. Note that this causes our secondary key
127: // to be automatically updated for us.
128: try {
129: da.inventoryBySku.put(txn, theInventory);
130: } catch (DatabaseException dbe) {
131: try {
132: System.out.println("Error putting entry "
133: + sku.getBytes("UTF-8"));
134: } catch (IOException willNeverOccur) {
135: }
136: txn.abort();
137: throw dbe;
138: }
139: }
140: // Commit the transaction. The data is now safely written to the
141: // store.
142: txn.commit();
143: }
144:
145: private static void parseArgs(String args[]) {
146: for (int i = 0; i < args.length; ++i) {
147: if (args[i].startsWith("-")) {
148: switch (args[i].charAt(1)) {
149: case 'h':
150: myDbEnvPath = new File(args[++i]);
151: break;
152: case 'i':
153: inventoryFile = new File(args[++i]);
154: break;
155: case 'v':
156: vendorsFile = new File(args[++i]);
157: break;
158: default:
159: usage();
160: }
161: }
162: }
163: }
164:
165: private List loadFile(File theFile, int numFields) {
166: List<String[]> records = new ArrayList<String[]>();
167: try {
168: String theLine = null;
169: FileInputStream fis = new FileInputStream(theFile);
170: BufferedReader br = new BufferedReader(
171: new InputStreamReader(fis));
172: while ((theLine = br.readLine()) != null) {
173: String[] theLineArray = theLine.split("#");
174: if (theLineArray.length != numFields) {
175: System.out.println("Malformed line found in "
176: + theFile.getPath());
177: System.out.println("Line was: '" + theLine);
178: System.out.println("length found was: "
179: + theLineArray.length);
180: System.exit(-1);
181: }
182: records.add(theLineArray);
183: }
184: // Close the input stream handle
185: 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: }
|