KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > expressions > ExpressionOperatorConverter


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, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.expressions;
23
24 import oracle.toplink.essentials.exceptions.*;
25 import oracle.toplink.essentials.expressions.ExpressionOperator;
26 import oracle.toplink.essentials.internal.helper.Helper;
27 import oracle.toplink.essentials.mappings.converters.ObjectTypeConverter;
28 import oracle.toplink.essentials.sessions.Session;
29 import oracle.toplink.essentials.internal.sessions.AbstractSession;
30
31 /**
32  * INTERNAL:
33  * Used by function operators in deployment xml generation to accomodate custom functions.
34  * There is no more validation on read because any custom function has to be accepted.
35  * The custom function is assumed to be a normal prefix function. The first element in the
36  * databaseStrings of the operator is in the format of databaseString(, e.g. AVG(. "(" will
37  * be removed on write and attached back on read.
38  */

39
40 public class ExpressionOperatorConverter extends ObjectTypeConverter {
41     /**
42      * INTERNAL:
43      * Convert to the data value.
44      */

45     public Object JavaDoc convertObjectValueToDataValue(Object JavaDoc attributeValue, Session session) {
46         Object JavaDoc fieldValue;
47         if (attributeValue == null) {
48             fieldValue = getAttributeToFieldValues().get(Helper.getNullWrapper());
49         } else {
50             fieldValue = getAttributeToFieldValues().get(attributeValue);
51             if (fieldValue == null) {
52                 //Custom function. Remove "(".
53
if (((ExpressionOperator)attributeValue).getDatabaseStrings() != null) {
54                     String JavaDoc databaseString = ((ExpressionOperator)attributeValue).getDatabaseStrings()[0];
55                     fieldValue = databaseString.substring(0, databaseString.length()-1);
56                 } else {
57                     throw DescriptorException.noAttributeValueConversionToFieldValueProvided(attributeValue, getMapping());
58                 }
59             }
60         }
61         return fieldValue;
62     }
63
64     /**
65      * INTERNAL:
66      * Returns the corresponding attribute value for the specified field value.
67      */

68     public Object JavaDoc convertDataValueToObjectValue(Object JavaDoc fieldValue, Session session) {
69         Object JavaDoc attributeValue = null;
70
71         if (fieldValue == null) {
72             attributeValue = getFieldToAttributeValues().get(Helper.getNullWrapper());
73         } else {
74             try {
75                 fieldValue = ((AbstractSession)session).getDatasourcePlatform().getConversionManager().convertObject(fieldValue, getFieldClassification());
76             } catch (ConversionException e) {
77                 throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e);
78             }
79
80             attributeValue = getFieldToAttributeValues().get(fieldValue);
81             //Custom function. Create an operator for it.
82
if (attributeValue == null) {
83                 attributeValue = ExpressionOperator.simpleFunction(0, (String JavaDoc)fieldValue);
84             }
85         }
86         return attributeValue;
87     }
88 }
89
Popular Tags