KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > indirection > NoIndirectionPolicy


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.indirection;
23
24 import oracle.toplink.essentials.queryframework.*;
25 import oracle.toplink.essentials.exceptions.*;
26 import oracle.toplink.essentials.internal.helper.*;
27 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
28 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
29 import oracle.toplink.essentials.internal.sessions.AbstractSession;
30
31 /**
32  * <h2>Purpose</h2>:
33  * NoIndirectionPolicy implements the behavior necessary for a
34  * a ForeignReferenceMapping (or TransformationMapping) to
35  * directly use domain objects, as opposed to ValueHolders.
36  *
37  * @see ForeignReferenceMapping
38  * @author Mike Norman
39  * @since TOPLink/Java 2.5
40  */

41 public class NoIndirectionPolicy extends IndirectionPolicy {
42
43     /**
44      * INTERNAL:
45      * Construct a new indirection policy.
46      */

47     public NoIndirectionPolicy() {
48         super();
49     }
50
51     /**
52      * INTERNAL:
53      * Return a clone of the attribute.
54      * @param buildDirectlyFromRow indicates that we are building the clone directly
55      * from a row as opposed to building the original from the row, putting it in
56      * the shared cache, and then cloning the original.
57      */

58     public Object JavaDoc cloneAttribute(Object JavaDoc attributeValue, Object JavaDoc original, Object JavaDoc clone, UnitOfWorkImpl unitOfWork, boolean buildDirectlyFromRow) {
59         // Since valueFromRow was called with the UnitOfWork, attributeValue
60
// is already a registered result.
61
if (buildDirectlyFromRow) {
62             return attributeValue;
63         }
64         boolean isExisting = unitOfWork.isObjectRegistered(clone) && (!(unitOfWork.isOriginalNewObject(original)));
65         return this.getMapping().buildCloneForPartObject(attributeValue, original, clone, unitOfWork, isExisting);
66     }
67
68     /**
69      * INTERNAL:
70      * Return whether the collection type is appropriate for the indirection policy.
71      * In this case, the type MUST be a Vector (or, in the case of jdk1.2,
72      * Collection or Map).
73      */

74     protected boolean collectionTypeIsValid(Class JavaDoc collectionType) {
75         return getCollectionMapping().getContainerPolicy().isValidContainerType(collectionType);
76     }
77
78     /**
79      * INTERNAL:
80      * Return the reference row for the reference object.
81      * This allows the new row to be built without instantiating
82      * the reference object.
83      * Return null if the object has already been instantiated.
84      */

85     public AbstractRecord extractReferenceRow(Object JavaDoc referenceObject) {
86         return null;
87     }
88
89     /**
90      * INTERNAL:
91      * Return the original indirection object for a unit of work indirection object.
92      */

93     public Object JavaDoc getOriginalIndirectionObject(Object JavaDoc unitOfWorkIndirectionObject, AbstractSession session) {
94         // This code appears broken, but actually is unreachable because
95
// only called when indirection is true.
96
return unitOfWorkIndirectionObject;
97     }
98
99     /**
100      * INTERNAL:
101      * Return the "real" attribute value, as opposed to any wrapper.
102      * This will trigger the wrapper to instantiate the value.
103      */

104     public Object JavaDoc getRealAttributeValueFromObject(Object JavaDoc object) {
105         return object;
106     }
107
108     /**
109      * INTERNAL:
110      * Return the null value of the appropriate attribute. That is, the
111      * field from the database is NULL, return what should be
112      * placed in the object's attribute as a result.
113      */

114     public Object JavaDoc nullValueFromRow() {
115         return null;
116     }
117
118     /**
119      * INTERNAL:
120      * Return whether the specified object is instantiated.
121      */

122     public boolean objectIsInstantiated(Object JavaDoc object) {
123         return true;
124     }
125
126     /**
127      * INTERNAL:
128      * Return whether the type is appropriate for the indirection policy.
129      * In this case, the attribute type CANNOT be ValueHolderInterface.
130      */

131     protected boolean typeIsValid(Class JavaDoc attributeType) {
132         return attributeType != ClassConstants.ValueHolderInterface_Class;
133     }
134
135     /**
136      * INTERNAL:
137      * Return whether the indirection policy actually uses indirection.
138      * Here, we must reply false.
139      */

140     public boolean usesIndirection() {
141         return false;
142     }
143
144     /**
145      * INTERNAL:
146      * Verify that attributeType is correct for the
147      * indirection policy. If it is incorrect, add an exception to the
148      * integrity checker.
149      * In this case, the attribute type CANNOT be ValueHolderInterface.
150      */

151     public void validateDeclaredAttributeType(Class JavaDoc attributeType, IntegrityChecker checker) throws DescriptorException {
152         super.validateDeclaredAttributeType(attributeType, checker);
153         if (!this.typeIsValid(attributeType)) {
154             checker.handleError(DescriptorException.attributeAndMappingWithoutIndirectionMismatch(this.getMapping()));
155         }
156     }
157
158     /**
159      * INTERNAL:
160      * Verify that attributeType is an appropriate collection type for the
161      * indirection policy. If it is incorrect, add an exception to the integrity checker.
162      * In this case, the type MUST be a Vector (or, in the case of jdk1.2,
163      * Collection or Map).
164      */

165     public void validateDeclaredAttributeTypeForCollection(Class JavaDoc attributeType, IntegrityChecker checker) throws DescriptorException {
166         super.validateDeclaredAttributeTypeForCollection(attributeType, checker);
167         if (!this.collectionTypeIsValid(attributeType)) {
168             checker.handleError(DescriptorException.attributeTypeNotValid(this.getCollectionMapping()));
169         }
170     }
171
172     /**
173      * INTERNAL:
174      * Verify that getter returnType is correct for the
175      * indirection policy. If it is incorrect, add an exception
176      * to the integrity checker.
177      * In this case, the return type CANNOT be ValueHolderInterface.
178      */

179     public void validateGetMethodReturnType(Class JavaDoc returnType, IntegrityChecker checker) throws DescriptorException {
180         super.validateGetMethodReturnType(returnType, checker);
181         if (!this.typeIsValid(returnType)) {
182             checker.handleError(DescriptorException.returnAndMappingWithoutIndirectionMismatch(this.getMapping()));
183         }
184     }
185
186     /**
187      * INTERNAL:
188      * Verify that getter returnType is an appropriate collection type for the
189      * indirection policy. If it is incorrect, add an exception to the integrity checker.
190      * In this case, the type MUST be a Vector (or, in the case of jdk1.2,
191      * Collection or Map).
192      */

193     public void validateGetMethodReturnTypeForCollection(Class JavaDoc returnType, IntegrityChecker checker) throws DescriptorException {
194         super.validateGetMethodReturnTypeForCollection(returnType, checker);
195         if (!this.collectionTypeIsValid(returnType)) {
196             checker.handleError(DescriptorException.getMethodReturnTypeNotValid(getCollectionMapping()));
197         }
198     }
199
200     /**
201      * INTERNAL:
202      * Verify that setter parameterType is correct for the
203      * indirection policy. If it is incorrect, add an exception
204      * to the integrity checker.
205      * In this case, the parameter type CANNOT be ValueHolderInterface.
206      */

207     public void validateSetMethodParameterType(Class JavaDoc parameterType, IntegrityChecker checker) throws DescriptorException {
208         super.validateSetMethodParameterType(parameterType, checker);
209         if (!this.typeIsValid(parameterType)) {
210             checker.handleError(DescriptorException.parameterAndMappingWithoutIndirectionMismatch(this.getMapping()));
211         }
212     }
213
214     /**
215      * INTERNAL:
216      * Verify that setter parameterType is an appropriate collection type for the
217      * indirection policy. If it is incorrect, add an exception to the integrity checker.
218      * In this case, the type MUST be a Vector (or, in the case of jdk1.2,
219      * Collection or Map).
220      */

221     public void validateSetMethodParameterTypeForCollection(Class JavaDoc parameterType, IntegrityChecker checker) throws DescriptorException {
222         super.validateSetMethodParameterTypeForCollection(parameterType, checker);
223         if (!this.collectionTypeIsValid(parameterType)) {
224             checker.handleError(DescriptorException.setMethodParameterTypeNotValid(getCollectionMapping()));
225         }
226     }
227
228     /**
229      * INTERNAL:
230      * Return the value to be stored in the object's attribute.
231      * This value is determined by the query.
232      * In this case, simply execute the query and return its results.
233      */

234     public Object JavaDoc valueFromQuery(ReadQuery query, AbstractRecord row, AbstractSession session) {
235         return session.executeQuery(query, row);
236     }
237
238     /**
239      * INTERNAL:
240      * Return the value to be stored in the object's attribute.
241      * This value is determined by the row.
242      * In this case, simply return the object.
243      */

244     public Object JavaDoc valueFromRow(Object JavaDoc object) {
245         return object;
246     }
247 }
248
Popular Tags