KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > serial > SerialSerialKeyCreator


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: SerialSerialKeyCreator.java,v 1.31 2006/10/30 21:14:07 bostic Exp $
7  */

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 /**
18  * A abstract key creator that uses a serial key and a serial data entry.
19  * This class takes care of serializing and deserializing the key and data
20  * entry automatically.
21  * The following abstract method must be implemented by a concrete subclass
22  * to create the index key using these objects
23  * <ul>
24  * <li> {@link #createSecondaryKey(Object,Object)} </li>
25  * </ul>
26  * <!-- begin JE only -->
27  * <p>If {@link com.sleepycat.je.ForeignKeyDeleteAction#NULLIFY} was
28  * specified when opening the secondary database, the following method must be
29  * overridden to nullify the foreign index key. If NULLIFY was not specified,
30  * this method need not be overridden.</p>
31  * <ul>
32  * <li> {@link #nullifyForeignKey(Object)} </li>
33  * </ul>
34  * <!-- end JE only -->
35  *
36  * @author Mark Hayes
37  */

38 public abstract class SerialSerialKeyCreator
39     implements SecondaryKeyCreator, ForeignKeyNullifier {
40
41     protected SerialBinding primaryKeyBinding;
42     protected SerialBinding dataBinding;
43     protected SerialBinding indexKeyBinding;
44
45     /**
46      * Creates a serial-serial key creator.
47      *
48      * @param classCatalog is the catalog to hold shared class information and
49      * for a database should be a {@link StoredClassCatalog}.
50      *
51      * @param primaryKeyClass is the primary key base class.
52      *
53      * @param dataClass is the data base class.
54      *
55      * @param indexKeyClass is the index key base class.
56      */

57     public SerialSerialKeyCreator(ClassCatalog classCatalog,
58                                   Class JavaDoc primaryKeyClass,
59                                   Class JavaDoc dataClass,
60                                   Class JavaDoc indexKeyClass) {
61
62         this(new SerialBinding(classCatalog, primaryKeyClass),
63              new SerialBinding(classCatalog, dataClass),
64              new SerialBinding(classCatalog, indexKeyClass));
65     }
66
67     /**
68      * Creates a serial-serial entity binding.
69      *
70      * @param primaryKeyBinding is the primary key binding.
71      *
72      * @param dataBinding is the data binding.
73      *
74      * @param indexKeyBinding is the index key binding.
75      */

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     // javadoc is inherited
86
public boolean createSecondaryKey(SecondaryDatabase db,
87                                       DatabaseEntry primaryKeyEntry,
88                                       DatabaseEntry dataEntry,
89                                       DatabaseEntry indexKeyEntry)
90         throws DatabaseException {
91
92         Object JavaDoc primaryKeyInput =
93             primaryKeyBinding.entryToObject(primaryKeyEntry);
94         Object JavaDoc dataInput = dataBinding.entryToObject(dataEntry);
95         Object JavaDoc 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     // javadoc is inherited
105
public boolean nullifyForeignKey(SecondaryDatabase db,
106                                      DatabaseEntry dataEntry)
107         throws DatabaseException {
108
109         Object JavaDoc 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     /**
120      * Creates the index key object from primary key and entry objects.
121      *
122      * @param primaryKey is the deserialized source primary key entry, or
123      * null if no primary key entry is used to construct the index key.
124      *
125      * @param data is the deserialized source data entry, or null if no
126      * data entry is used to construct the index key.
127      *
128      * @return the destination index key object, or null to indicate that
129      * the key is not present.
130      */

131     public abstract Object JavaDoc createSecondaryKey(Object JavaDoc primaryKey, Object JavaDoc data);
132
133     /**
134      * Clears the index key in a data object.
135      *
136      * <p>On entry the data parameter contains the index key to be cleared. It
137      * should be changed by this method such that {@link #createSecondaryKey}
138      * will return false. Other fields in the data object should remain
139      * unchanged.</p>
140      *
141      * @param data is the source and destination data object.
142      *
143      * @return the destination data object, or null to indicate that the
144      * key is not present and no change is necessary. The data returned may
145      * be the same object passed as the data parameter or a newly created
146      * object.
147      */

148     public Object JavaDoc nullifyForeignKey(Object JavaDoc data) {
149
150         return null;
151     }
152 }
153
Popular Tags