001: /**********************************************************************
002: Copyright (c) 2005 Erik Bengtson 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 java.awt.Color;
020:
021: import org.jpox.ClassLoaderResolver;
022: import org.jpox.ClassNameConstants;
023: import org.jpox.ObjectManager;
024: import org.jpox.metadata.AbstractMemberMetaData;
025: import org.jpox.store.DatastoreAdapter;
026: import org.jpox.store.DatastoreContainerObject;
027: import org.jpox.store.expression.QueryExpression;
028: import org.jpox.store.expression.ScalarExpression;
029: import org.jpox.store.expression.LogicSetExpression;
030:
031: /**
032: * Mapping for java.awt.Color mapping the red, green, blue and alpha values to datastore fields.
033: *
034: * @version $Revision: 1.21 $
035: */
036: public class ColorMapping 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); // Red
052: addDatastoreField(ClassNameConstants.INT); // Green
053: addDatastoreField(ClassNameConstants.INT); // Blue
054: addDatastoreField(ClassNameConstants.INT); // Alpha
055: }
056:
057: /* (non-Javadoc)
058: * @see org.jpox.store.mapping.JavaTypeMapping#getJavaType()
059: */
060: public Class getJavaType() {
061: return Color.class;
062: }
063:
064: /* (non-Javadoc)
065: * @see org.jpox.store.mapping.JavaTypeMapping#getSampleValue()
066: */
067: public Object getSampleValue(ClassLoaderResolver clr) {
068: return java.awt.Color.red;
069: }
070:
071: /* (non-Javadoc)
072: * @see org.jpox.store.mapping.JavaTypeMapping#setObject(org.jpox.ObjectManager, java.lang.Object, int[], java.lang.Object)
073: */
074: public void setObject(ObjectManager om, Object preparedStatement,
075: int[] exprIndex, Object value) {
076: Color color = (Color) value;
077: if (color == null) {
078: getDataStoreMapping(0).setObject(preparedStatement,
079: exprIndex[0], null);
080: getDataStoreMapping(1).setObject(preparedStatement,
081: exprIndex[1], null);
082: getDataStoreMapping(2).setObject(preparedStatement,
083: exprIndex[2], null);
084: getDataStoreMapping(3).setObject(preparedStatement,
085: exprIndex[3], null);
086: } else {
087: getDataStoreMapping(0).setInt(preparedStatement,
088: exprIndex[0], color.getRed());
089: getDataStoreMapping(1).setInt(preparedStatement,
090: exprIndex[1], color.getGreen());
091: getDataStoreMapping(2).setInt(preparedStatement,
092: exprIndex[2], color.getBlue());
093: getDataStoreMapping(3).setInt(preparedStatement,
094: exprIndex[3], color.getAlpha());
095: }
096: }
097:
098: /* (non-Javadoc)
099: * @see org.jpox.store.mapping.JavaTypeMapping#getObject(org.jpox.ObjectManager, java.lang.Object, int[])
100: */
101: public Object getObject(ObjectManager om, Object resultSet,
102: int[] exprIndex) {
103: try {
104: // Check for null entries
105: if (getDataStoreMapping(0).getObject(resultSet,
106: exprIndex[0]) == null) {
107: return null;
108: }
109: } catch (Exception e) {
110: // Do nothing
111: }
112:
113: int red = getDataStoreMapping(0)
114: .getInt(resultSet, exprIndex[0]);
115: int green = getDataStoreMapping(1).getInt(resultSet,
116: exprIndex[1]);
117: int blue = getDataStoreMapping(2).getInt(resultSet,
118: exprIndex[2]);
119: int alpha = getDataStoreMapping(3).getInt(resultSet,
120: exprIndex[3]);
121: return new Color(red, green, blue, alpha);
122: }
123:
124: // --------------------------------- JDOQL Query Methods -------------------------------------------
125:
126: /* (non-Javadoc)
127: * @see org.jpox.store.mapping.JavaTypeMapping#newLiteral(org.jpox.store.query.QueryStatement, java.lang.Object)
128: */
129: public ScalarExpression newLiteral(QueryExpression qs, Object value) {
130: return null;
131: }
132:
133: /* (non-Javadoc)
134: * @see org.jpox.store.mapping.JavaTypeMapping#newScalarExpression(org.jpox.store.query.QueryStatement, org.jpox.store.expression.TableExpression)
135: */
136: public ScalarExpression newScalarExpression(QueryExpression qs,
137: LogicSetExpression te) {
138: return null;
139: }
140: }
|