KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > je > gettingStarted > ItemNameKeyCreator


1 // file ItemNameKeyCreator.java
2
// $Id: ItemNameKeyCreator.java,v 1.5 2006/03/30 00:39:55 sarette Exp $
3

4 package je.gettingStarted;
5
6 import com.sleepycat.je.SecondaryKeyCreator;
7 import com.sleepycat.je.DatabaseEntry;
8 import com.sleepycat.je.DatabaseException;
9 import com.sleepycat.je.SecondaryDatabase;
10 import com.sleepycat.bind.tuple.TupleBinding;
11
12 import java.io.IOException JavaDoc;
13
14 public class ItemNameKeyCreator implements SecondaryKeyCreator {
15
16     private TupleBinding theBinding;
17
18     // Use the constructor to set the tuple binding
19
ItemNameKeyCreator(TupleBinding binding) {
20         theBinding = binding;
21     }
22
23     // Abstract method that we must implement
24
public boolean createSecondaryKey(SecondaryDatabase secDb,
25              DatabaseEntry keyEntry, // From the primary
26
DatabaseEntry dataEntry, // From the primary
27
DatabaseEntry resultEntry) // set the key data on this.
28
throws DatabaseException {
29
30         if (dataEntry != null) {
31             // Convert dataEntry to an Inventory object
32
Inventory inventoryItem =
33                   (Inventory)theBinding.entryToObject(dataEntry);
34             // Get the item name and use that as the key
35
String JavaDoc theItem = inventoryItem.getItemName();
36             try {
37                 resultEntry.setData(theItem.getBytes("UTF-8"));
38             } catch (IOException JavaDoc willNeverOccur) {}
39         }
40         return true;
41     }
42 }
43
Popular Tags