001: /**********************************************************************
002: Copyright (c) 2004 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: 2005 Andy Jefferson - added capability to have String or long type discriminators
018: ...
019: **********************************************************************/package org.jpox.store.mapping;
020:
021: import org.jpox.ClassLoaderResolver;
022: import org.jpox.ClassNameConstants;
023: import org.jpox.ObjectManager;
024: import org.jpox.metadata.ColumnMetaData;
025: import org.jpox.metadata.DiscriminatorMetaData;
026: import org.jpox.store.DatastoreContainerObject;
027: import org.jpox.store.DatastoreField;
028: import org.jpox.store.DatastoreAdapter;
029: import org.jpox.store.DatastoreIdentifier;
030: import org.jpox.store.IdentifierFactory;
031: import org.jpox.store.expression.QueryExpression;
032: import org.jpox.store.expression.ScalarExpression;
033: import org.jpox.store.expression.LogicSetExpression;
034:
035: /**
036: * Mapping for a discriminator column in a table used in inheritance.
037: * The discriminator column is, by default, a String type, typically VARCHAR.
038: * It can however be "long" based if the user specifies INTEGER, BIGINT, or NUMERIC as the jdbc-type.
039: * In the latter case we make the necessary conversions between value types in this mapping class.
040: *
041: * This class is for internal use only. It should not be used in user mappings nor extended.
042: *
043: * @version $Revision: 1.34 $
044: */
045: public final class DiscriminatorMapping extends SingleFieldMapping {
046: private final JavaTypeMapping delegate;
047:
048: /**
049: * Constructor.
050: * @param dba Datastore Adapter
051: * @param datastoreContainer Datastore table
052: * @param delegate The JavaTypeMapping to delegate storage
053: */
054: public DiscriminatorMapping(DatastoreAdapter dba,
055: DatastoreContainerObject datastoreContainer,
056: JavaTypeMapping delegate) {
057: initialize(dba, delegate.getType());
058: this .delegate = delegate;
059:
060: DiscriminatorMetaData dismd = datastoreContainer
061: .getDiscriminatorMetaData();
062: IdentifierFactory idFactory = datastoreContainer
063: .getStoreManager().getIdentifierFactory();
064: DatastoreIdentifier id = null;
065: if (dismd.getColumnMetaData() == null) {
066: // No column name so generate a default
067: id = idFactory.newDiscriminatorFieldIdentifier();
068: ColumnMetaData colmd = new ColumnMetaData(dismd, id
069: .getIdentifier());
070: dismd.setColumnMetaData(colmd);
071: } else {
072: // Column name defined
073: ColumnMetaData colmd = dismd.getColumnMetaData();
074: id = idFactory.newDatastoreFieldIdentifier(colmd.getName());
075: }
076:
077: DatastoreField column = datastoreContainer.addDatastoreField(
078: getType(), id, this , dismd.getColumnMetaData());
079: datastoreContainer.getStoreManager().getMappingManager()
080: .createDatastoreMapping(delegate,
081: datastoreContainer.getStoreManager(), column,
082: getType());
083: }
084:
085: /**
086: * Accessor for the type represented here, returning the class itself
087: * @return This class.
088: */
089: public Class getJavaType() {
090: return DiscriminatorMapping.class;
091: }
092:
093: /**
094: * Accessor for a sample value for this type.
095: * @return Sample value
096: */
097: public Object getSampleValue(ClassLoaderResolver clr) {
098: return delegate.getSampleValue(clr);
099: }
100:
101: /**
102: * Accessor for a new literal for this mapping.
103: * @param qs The QueryStatement
104: * @param value The value of the object
105: * @return The new literal
106: */
107: public ScalarExpression newLiteral(QueryExpression qs, Object value) {
108: Object valueObj = value;
109: if (value instanceof java.lang.String
110: && (getType().equals(ClassNameConstants.LONG) || getType()
111: .equals(ClassNameConstants.JAVA_LANG_LONG))) {
112: valueObj = new Long((String) value);
113: }
114: return delegate.newLiteral(qs, valueObj);
115: }
116:
117: /**
118: * Accessor for a new scalar expression including this mapping.
119: * @param qs The QueryStatement
120: * @param te The table Expression
121: * @return The new scalar expression
122: */
123: public ScalarExpression newScalarExpression(QueryExpression qs,
124: LogicSetExpression te) {
125: return delegate.newScalarExpression(qs, te);
126: }
127:
128: /**
129: * Mutator for the object in this column
130: * @param om The Object Manager
131: * @param preparedStatement The statement
132: * @param exprIndex The indexes
133: * @param value The value to set it to
134: */
135: public void setObject(ObjectManager om, Object preparedStatement,
136: int[] exprIndex, Object value) {
137: Object valueObj = value;
138: if (value instanceof java.lang.String) {
139: if (getType().equals(ClassNameConstants.LONG)
140: || getType().equals(
141: ClassNameConstants.JAVA_LANG_LONG)) {
142: valueObj = new Long((String) value);
143: }
144: }
145: delegate.setObject(om, preparedStatement, exprIndex, valueObj);
146: }
147:
148: /**
149: * Accessor for the object in this column
150: * @param om The ObjectManager
151: * @param resultSet The ResultSet to get the value from
152: * @param exprIndex The indexes
153: * @return The object
154: */
155: public Object getObject(ObjectManager om, Object resultSet,
156: int[] exprIndex) {
157: Object value = delegate.getObject(om, resultSet, exprIndex);
158: Object valueObj = value;
159: if (value instanceof java.lang.String) {
160: if (getType().equals(ClassNameConstants.LONG)
161: || getType().equals(
162: ClassNameConstants.JAVA_LANG_LONG)) {
163: valueObj = new Long((String) value);
164: }
165: }
166: return valueObj;
167: }
168:
169: /**
170: * Accessor for the number of datastore fields.
171: * @return Number of datastore fields
172: */
173: public int getNumberOfDatastoreFields() {
174: return delegate.getNumberOfDatastoreFields();
175: }
176:
177: /**
178: * Accessor for a datastore mapping
179: * @param index Index of the mapping
180: * @return The datastore mapping.
181: */
182: public DatastoreMapping getDataStoreMapping(int index) {
183: return delegate.getDataStoreMapping(index);
184: }
185:
186: /**
187: * Mutator to add a datastore mapping
188: * @param datastoreMapping Datastore mapping
189: */
190: public void addDataStoreMapping(DatastoreMapping datastoreMapping) {
191: delegate.addDataStoreMapping(datastoreMapping);
192: }
193: }
|