00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.bind.serial;
00011
00012 import com.sleepycat.db.DatabaseEntry;
00013 import com.sleepycat.db.DatabaseException;
00014 import com.sleepycat.db.SecondaryDatabase;
00015 import com.sleepycat.db.SecondaryKeyCreator;
00016
00029 public abstract class SerialSerialKeyCreator
00030 implements SecondaryKeyCreator {
00031
00032 protected SerialBinding primaryKeyBinding;
00033 protected SerialBinding dataBinding;
00034 protected SerialBinding indexKeyBinding;
00035
00048 public SerialSerialKeyCreator(ClassCatalog classCatalog,
00049 Class primaryKeyClass,
00050 Class dataClass,
00051 Class indexKeyClass) {
00052
00053 this(new SerialBinding(classCatalog, primaryKeyClass),
00054 new SerialBinding(classCatalog, dataClass),
00055 new SerialBinding(classCatalog, indexKeyClass));
00056 }
00057
00067 public SerialSerialKeyCreator(SerialBinding primaryKeyBinding,
00068 SerialBinding dataBinding,
00069 SerialBinding indexKeyBinding) {
00070
00071 this.primaryKeyBinding = primaryKeyBinding;
00072 this.dataBinding = dataBinding;
00073 this.indexKeyBinding = indexKeyBinding;
00074 }
00075
00076
00077 public boolean createSecondaryKey(SecondaryDatabase db,
00078 DatabaseEntry primaryKeyEntry,
00079 DatabaseEntry dataEntry,
00080 DatabaseEntry indexKeyEntry)
00081 throws DatabaseException {
00082
00083 Object primaryKeyInput =
00084 primaryKeyBinding.entryToObject(primaryKeyEntry);
00085 Object dataInput = dataBinding.entryToObject(dataEntry);
00086 Object indexKey = createSecondaryKey(primaryKeyInput, dataInput);
00087 if (indexKey != null) {
00088 indexKeyBinding.objectToEntry(indexKey, indexKeyEntry);
00089 return true;
00090 } else {
00091 return false;
00092 }
00093 }
00094
00095
00096 public boolean nullifyForeignKey(SecondaryDatabase db,
00097 DatabaseEntry dataEntry)
00098 throws DatabaseException {
00099
00100 Object data = dataBinding.entryToObject(dataEntry);
00101 data = nullifyForeignKey(data);
00102 if (data != null) {
00103 dataBinding.objectToEntry(data, dataEntry);
00104 return true;
00105 } else {
00106 return false;
00107 }
00108 }
00109
00122 public abstract Object createSecondaryKey(Object primaryKey, Object data);
00123
00139 public Object nullifyForeignKey(Object data) {
00140
00141 return null;
00142 }
00143 }