1 8 9 package com.sleepycat.bind.serial; 10 11 import com.sleepycat.je.DatabaseEntry; 12 import com.sleepycat.je.DatabaseException; 13 import com.sleepycat.je.ForeignKeyNullifier; 14 import com.sleepycat.je.SecondaryDatabase; 15 import com.sleepycat.je.SecondaryKeyCreator; 16 17 38 public abstract class SerialSerialKeyCreator 39 implements SecondaryKeyCreator, ForeignKeyNullifier { 40 41 protected SerialBinding primaryKeyBinding; 42 protected SerialBinding dataBinding; 43 protected SerialBinding indexKeyBinding; 44 45 57 public SerialSerialKeyCreator(ClassCatalog classCatalog, 58 Class primaryKeyClass, 59 Class dataClass, 60 Class indexKeyClass) { 61 62 this(new SerialBinding(classCatalog, primaryKeyClass), 63 new SerialBinding(classCatalog, dataClass), 64 new SerialBinding(classCatalog, indexKeyClass)); 65 } 66 67 76 public SerialSerialKeyCreator(SerialBinding primaryKeyBinding, 77 SerialBinding dataBinding, 78 SerialBinding indexKeyBinding) { 79 80 this.primaryKeyBinding = primaryKeyBinding; 81 this.dataBinding = dataBinding; 82 this.indexKeyBinding = indexKeyBinding; 83 } 84 85 public boolean createSecondaryKey(SecondaryDatabase db, 87 DatabaseEntry primaryKeyEntry, 88 DatabaseEntry dataEntry, 89 DatabaseEntry indexKeyEntry) 90 throws DatabaseException { 91 92 Object primaryKeyInput = 93 primaryKeyBinding.entryToObject(primaryKeyEntry); 94 Object dataInput = dataBinding.entryToObject(dataEntry); 95 Object indexKey = createSecondaryKey(primaryKeyInput, dataInput); 96 if (indexKey != null) { 97 indexKeyBinding.objectToEntry(indexKey, indexKeyEntry); 98 return true; 99 } else { 100 return false; 101 } 102 } 103 104 public boolean nullifyForeignKey(SecondaryDatabase db, 106 DatabaseEntry dataEntry) 107 throws DatabaseException { 108 109 Object data = dataBinding.entryToObject(dataEntry); 110 data = nullifyForeignKey(data); 111 if (data != null) { 112 dataBinding.objectToEntry(data, dataEntry); 113 return true; 114 } else { 115 return false; 116 } 117 } 118 119 131 public abstract Object createSecondaryKey(Object primaryKey, Object data); 132 133 148 public Object nullifyForeignKey(Object data) { 149 150 return null; 151 } 152 } 153 | Popular Tags |