001: /**********************************************************************
002: Copyright (c) 2005 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:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.store.mapping;
019:
020: import java.awt.Point;
021:
022: import org.jpox.ClassLoaderResolver;
023: import org.jpox.ClassNameConstants;
024: import org.jpox.ObjectManager;
025: import org.jpox.metadata.AbstractMemberMetaData;
026: import org.jpox.store.DatastoreAdapter;
027: import org.jpox.store.DatastoreContainerObject;
028: import org.jpox.store.expression.QueryExpression;
029: import org.jpox.store.expression.ScalarExpression;
030: import org.jpox.store.expression.LogicSetExpression;
031:
032: /**
033: * Mapping for java.awt.Point to its datastore representation.
034: * @version $Revision: 1.20 $
035: */
036: public class PointMapping extends SingleFieldMultiMapping {
037: /**
038: * Initialize this JavaTypeMapping with the given DatastoreAdapter for
039: * the given FieldMetaData.
040: *
041: * @param dba The Datastore Adapter that this Mapping should use.
042: * @param fmd FieldMetaData for the field to be mapped (if any)
043: * @param container The datastore container storing this mapping (if any)
044: * @param clr the ClassLoaderResolver
045: */
046: public void initialize(DatastoreAdapter dba,
047: AbstractMemberMetaData fmd,
048: DatastoreContainerObject container, ClassLoaderResolver clr) {
049: super .initialize(dba, fmd, container, clr);
050:
051: addDatastoreField(ClassNameConstants.INT); // X
052: addDatastoreField(ClassNameConstants.INT); // Y
053: }
054:
055: /*
056: * (non-Javadoc)
057: * @see org.jpox.store.mapping.JavaTypeMapping#getJavaType()
058: */
059: public Class getJavaType() {
060: return Point.class;
061: }
062:
063: static Object samplePoint = new Point(0, 0);
064:
065: /*
066: * (non-Javadoc)
067: * @see org.jpox.store.mapping.JavaTypeMapping#getSampleValue()
068: */
069: public Object getSampleValue(ClassLoaderResolver clr) {
070: return samplePoint;
071: }
072:
073: /*
074: * (non-Javadoc)
075: * @see org.jpox.store.mapping.JavaTypeMapping#setObject(org.jpox.ObjectManager,
076: * java.lang.Object, int[], java.lang.Object)
077: */
078: public void setObject(ObjectManager om, Object preparedStatement,
079: int[] exprIndex, Object value) {
080: Point pt = (Point) value;
081: if (pt == null) {
082: getDataStoreMapping(0).setObject(preparedStatement,
083: exprIndex[0], null);
084: getDataStoreMapping(1).setObject(preparedStatement,
085: exprIndex[1], null);
086: } else {
087: getDataStoreMapping(0).setInt(preparedStatement,
088: exprIndex[0], (int) pt.getX());
089: getDataStoreMapping(1).setInt(preparedStatement,
090: exprIndex[1], (int) pt.getY());
091: }
092: }
093:
094: /*
095: * (non-Javadoc)
096: * @see org.jpox.store.mapping.JavaTypeMapping#getObject(org.jpox.ObjectManager,
097: * java.lang.Object, int[])
098: */
099: public Object getObject(ObjectManager om, Object resultSet,
100: int[] exprIndex) {
101: try {
102: // Check for null entries, just by checking first datastore mapping for null
103: if (getDataStoreMapping(0).getObject(resultSet,
104: exprIndex[0]) == null) {
105: return null;
106: }
107: } catch (Exception e) {
108: // Do nothing
109: }
110:
111: int x = getDataStoreMapping(0).getInt(resultSet, exprIndex[0]);
112: int y = getDataStoreMapping(1).getInt(resultSet, exprIndex[1]);
113: return new Point(x, y);
114: }
115:
116: // -------------------------------- JDOQL Query Methods -----------------------------------------
117:
118: /*
119: * (non-Javadoc)
120: * @see org.jpox.store.mapping.JavaTypeMapping#newLiteral(org.jpox.store.query.QueryStatement,
121: * java.lang.Object)
122: */
123: public ScalarExpression newLiteral(QueryExpression qs, Object value) {
124: return null;
125: }
126:
127: /*
128: * (non-Javadoc)
129: * @see org.jpox.store.mapping.JavaTypeMapping#newScalarExpression(org.jpox.store.query.QueryStatement,
130: * org.jpox.store.expression.TableExpression)
131: */
132: public ScalarExpression newScalarExpression(QueryExpression qs,
133: LogicSetExpression te) {
134: return null;
135: }
136: }
|