001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: SerialSerialKeyCreator.java,v 1.31.2.2 2008/01/07 15:14:05 cwl Exp $
007: */
008:
009: package com.sleepycat.bind.serial;
010:
011: import com.sleepycat.je.DatabaseEntry;
012: import com.sleepycat.je.DatabaseException;
013: import com.sleepycat.je.ForeignKeyNullifier;
014: import com.sleepycat.je.SecondaryDatabase;
015: import com.sleepycat.je.SecondaryKeyCreator;
016:
017: /**
018: * A abstract key creator that uses a serial key and a serial data entry.
019: * This class takes care of serializing and deserializing the key and data
020: * entry automatically.
021: * The following abstract method must be implemented by a concrete subclass
022: * to create the index key using these objects
023: * <ul>
024: * <li> {@link #createSecondaryKey(Object,Object)} </li>
025: * </ul>
026: * <!-- begin JE only -->
027: * <p>If {@link com.sleepycat.je.ForeignKeyDeleteAction#NULLIFY} was
028: * specified when opening the secondary database, the following method must be
029: * overridden to nullify the foreign index key. If NULLIFY was not specified,
030: * this method need not be overridden.</p>
031: * <ul>
032: * <li> {@link #nullifyForeignKey(Object)} </li>
033: * </ul>
034: * <!-- end JE only -->
035: *
036: * @author Mark Hayes
037: */
038: public abstract class SerialSerialKeyCreator implements
039: SecondaryKeyCreator, ForeignKeyNullifier {
040:
041: protected SerialBinding primaryKeyBinding;
042: protected SerialBinding dataBinding;
043: protected SerialBinding indexKeyBinding;
044:
045: /**
046: * Creates a serial-serial key creator.
047: *
048: * @param classCatalog is the catalog to hold shared class information and
049: * for a database should be a {@link StoredClassCatalog}.
050: *
051: * @param primaryKeyClass is the primary key base class.
052: *
053: * @param dataClass is the data base class.
054: *
055: * @param indexKeyClass is the index key base class.
056: */
057: public SerialSerialKeyCreator(ClassCatalog classCatalog,
058: Class primaryKeyClass, Class dataClass, Class indexKeyClass) {
059:
060: this (new SerialBinding(classCatalog, primaryKeyClass),
061: new SerialBinding(classCatalog, dataClass),
062: new SerialBinding(classCatalog, indexKeyClass));
063: }
064:
065: /**
066: * Creates a serial-serial entity binding.
067: *
068: * @param primaryKeyBinding is the primary key binding.
069: *
070: * @param dataBinding is the data binding.
071: *
072: * @param indexKeyBinding is the index key binding.
073: */
074: public SerialSerialKeyCreator(SerialBinding primaryKeyBinding,
075: SerialBinding dataBinding, SerialBinding indexKeyBinding) {
076:
077: this .primaryKeyBinding = primaryKeyBinding;
078: this .dataBinding = dataBinding;
079: this .indexKeyBinding = indexKeyBinding;
080: }
081:
082: // javadoc is inherited
083: public boolean createSecondaryKey(SecondaryDatabase db,
084: DatabaseEntry primaryKeyEntry, DatabaseEntry dataEntry,
085: DatabaseEntry indexKeyEntry) throws DatabaseException {
086:
087: Object primaryKeyInput = primaryKeyBinding
088: .entryToObject(primaryKeyEntry);
089: Object dataInput = dataBinding.entryToObject(dataEntry);
090: Object indexKey = createSecondaryKey(primaryKeyInput, dataInput);
091: if (indexKey != null) {
092: indexKeyBinding.objectToEntry(indexKey, indexKeyEntry);
093: return true;
094: } else {
095: return false;
096: }
097: }
098:
099: // javadoc is inherited
100: public boolean nullifyForeignKey(SecondaryDatabase db,
101: DatabaseEntry dataEntry) throws DatabaseException {
102:
103: Object data = dataBinding.entryToObject(dataEntry);
104: data = nullifyForeignKey(data);
105: if (data != null) {
106: dataBinding.objectToEntry(data, dataEntry);
107: return true;
108: } else {
109: return false;
110: }
111: }
112:
113: /**
114: * Creates the index key object from primary key and entry objects.
115: *
116: * @param primaryKey is the deserialized source primary key entry, or
117: * null if no primary key entry is used to construct the index key.
118: *
119: * @param data is the deserialized source data entry, or null if no
120: * data entry is used to construct the index key.
121: *
122: * @return the destination index key object, or null to indicate that
123: * the key is not present.
124: */
125: public abstract Object createSecondaryKey(Object primaryKey,
126: Object data);
127:
128: /**
129: * Clears the index key in a data object.
130: *
131: * <p>On entry the data parameter contains the index key to be cleared. It
132: * should be changed by this method such that {@link #createSecondaryKey}
133: * will return false. Other fields in the data object should remain
134: * unchanged.</p>
135: *
136: * @param data is the source and destination data object.
137: *
138: * @return the destination data object, or null to indicate that the
139: * key is not present and no change is necessary. The data returned may
140: * be the same object passed as the data parameter or a newly created
141: * object.
142: */
143: public Object nullifyForeignKey(Object data) {
144:
145: return null;
146: }
147: }
|