001: /**********************************************************************
002: Copyright (c) 2006 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.store.mapping;
018:
019: import org.jpox.ClassNameConstants;
020: import org.jpox.ObjectManager;
021: import org.jpox.store.expression.IntegerLiteral;
022: import org.jpox.store.expression.LogicSetExpression;
023: import org.jpox.store.expression.NumericExpression;
024: import org.jpox.store.expression.QueryExpression;
025: import org.jpox.store.expression.ScalarExpression;
026:
027: /**
028: * Abstract SCO mapping for a java type that will be stored as a Long type.
029: *
030: * @version $Revision: 1.5 $
031: */
032: public abstract class ObjectAsLongMapping extends SingleFieldMapping
033: implements SimpleDatastoreRepresentation {
034: /**
035: * Method to create a literal of this type for use in a JDOQL query.
036: * @param qs The QueryExpression
037: * @param value The value of the literal
038: * @return The Literal
039: */
040: public ScalarExpression newLiteral(QueryExpression qs, Object value) {
041: return new IntegerLiteral(qs, this , objectToLong(value));
042: }
043:
044: /**
045: * Method to create a new expression for this mapping for use in a JDOQL Query.
046: * @param qs The QueryExpression
047: * @param te Expression for the datastore container
048: * @return The JDOQL expression
049: */
050: public ScalarExpression newScalarExpression(QueryExpression qs,
051: LogicSetExpression te) {
052: return new NumericExpression(qs, this , te);
053: }
054:
055: /**
056: * Method to return the Java type.
057: * @return The Java type being represented.
058: */
059: public abstract Class getJavaType();
060:
061: /**
062: * Accessor for the name of the java-type actually used when mapping the particular datastore
063: * field. This java-type must have an entry in the datastore mappings.
064: * @param index requested datastore field index.
065: * @return the name of java-type for the requested datastore field.
066: */
067: public String getJavaTypeForDatastoreMapping(int index) {
068: // All of the types extending this class will be using java-type of Long for the datastore
069: return ClassNameConstants.JAVA_LANG_LONG;
070: }
071:
072: /**
073: * Method to set the object when updating the the datastore.
074: * @see org.jpox.store.mapping.SingleFieldMapping#setObject(org.jpox.ObjectManager, java.lang.Object, int[], java.lang.Object)
075: */
076: public void setObject(ObjectManager om, Object preparedStatement,
077: int[] exprIndex, Object value) {
078: getDataStoreMapping(0).setObject(preparedStatement,
079: exprIndex[0], objectToLong(value));
080: }
081:
082: /**
083: * Method to get the object from the datastore and convert to an object.
084: * @see org.jpox.store.mapping.SingleFieldMapping#getObject(org.jpox.ObjectManager, java.lang.Object, int[])
085: */
086: public Object getObject(ObjectManager om, Object resultSet,
087: int[] exprIndex) {
088: if (exprIndex == null) {
089: return null;
090: }
091:
092: Object datastoreValue = getDataStoreMapping(0).getObject(
093: resultSet, exprIndex[0]);
094: Object value = null;
095: if (datastoreValue != null) {
096: value = longToObject((Long) datastoreValue);
097: }
098: return value;
099: }
100:
101: /**
102: * Method to set the datastore value based on the object value.
103: * @param object The object
104: * @return The value to pass to the datastore
105: */
106: protected abstract Long objectToLong(Object object);
107:
108: /**
109: * Method to extract the objects value from the datastore object.
110: * @param datastoreValue Value obtained from the datastore
111: * @return The value of this object (derived from the datastore value)
112: */
113: protected abstract Object longToObject(Long datastoreValue);
114: }
|