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