001: /**********************************************************************
002: Copyright (c) 2002 Kelly Grizzle (TJDO) 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: 2003 Erik Bengtson - removed unused import
018: 2003 Andy Jefferson - coding standards
019: 2003 Andy Jefferson - updated setObject to use all input "params"
020: 2004 Andy Jefferson - fixes to allow full use of Long/String OIDs
021: ...
022: **********************************************************************/package org.jpox.store.mapping;
023:
024: import org.jpox.ClassLoaderResolver;
025: import org.jpox.ObjectManager;
026: import org.jpox.StateManager;
027: import org.jpox.api.ApiAdapter;
028: import org.jpox.identity.OID;
029: import org.jpox.identity.OIDFactory;
030: import org.jpox.store.exceptions.NotYetFlushedException;
031: import org.jpox.store.expression.LogicSetExpression;
032: import org.jpox.store.expression.ObjectExpression;
033: import org.jpox.store.expression.ObjectLiteral;
034: import org.jpox.store.expression.QueryExpression;
035: import org.jpox.store.expression.ScalarExpression;
036: import org.jpox.util.JPOXLogger;
037:
038: /**
039: * Mapping for OID type.
040: * TODO Remove this class and move PersistenceCapableMapping to subclass from MultiMapping.
041: *
042: * @version $Revision: 1.33 $
043: **/
044: public class OIDMapping extends SingleFieldMapping implements
045: SimpleDatastoreRepresentation {
046: /**
047: * Mutator for the OID in the datastore
048: * @param om The ObjectManager managing this object
049: * @param ps The Prepared Statement
050: * @param param Param numbers in the PreparedStatement for this object
051: * @param value The OID value to use
052: */
053: public void setObject(ObjectManager om, Object ps, int[] param,
054: Object value) {
055: if (value == null) {
056: getDataStoreMapping(0).setObject(ps, param[0], null);
057: } else {
058: ApiAdapter api = om.getApiAdapter();
059: OID oid;
060: if (api.isPersistable(value)) {
061: oid = (OID) api.getIdForObject(value);
062: if (oid == null) {
063: if (om.isInserting(value)) {
064: // Object is in the process of being inserted, but has no id yet so provide a null for now
065: // The "NotYetFlushedException" is caught by ParameterSetter and processed as an update being required.
066: getDataStoreMapping(0).setObject(ps, param[0],
067: null);
068: throw new NotYetFlushedException(value);
069: } else {
070: // Object is not persist, nor in the process of being made persistent
071: om.persistObjectInternal(value, null, null, -1,
072: StateManager.PC);
073: om.flushInternal(false);
074: }
075: }
076: oid = (OID) api.getIdForObject(value);
077: } else {
078: oid = (OID) value;
079: }
080:
081: try {
082: // Try as a Long
083: getDataStoreMapping(0).setObject(ps, param[0],
084: oid.getKeyValue());
085: } catch (Exception e) {
086: // Must be a String
087: getDataStoreMapping(0).setObject(ps, param[0],
088: oid.getKeyValue().toString());
089: }
090: }
091: }
092:
093: /**
094: * Accessor for the OID object from the result set
095: * @param om ObjectManager managing this object
096: * @param rs The ResultSet
097: * @param param Array of param numbers for this object
098: * @return The OID object
099: */
100: public Object getObject(ObjectManager om, Object rs, int[] param) {
101: Object value;
102: if (getNumberOfDatastoreFields() > 0) {
103: value = getDataStoreMapping(0).getObject(rs, param[0]);
104: } else {
105: // 1-1 bidirectional "mapped-by" relation, so use ID mappings of related class to retrieve the value
106: if (referenceMapping != null) //TODO why is it null for PC concrete classes?
107: {
108: return referenceMapping.getObject(om, rs, param);
109: }
110: Class fieldType = fmd.getType();
111: JavaTypeMapping referenceMapping = om.getStoreManager()
112: .getDatastoreClass(fieldType.getName(),
113: om.getClassLoaderResolver()).getIDMapping();
114: value = referenceMapping.getDataStoreMapping(0).getObject(
115: rs, param[0]);
116: }
117: if (value != null) {
118: value = OIDFactory.getInstance(om, getType(), value);
119: if (JPOXLogger.PERSISTENCE.isDebugEnabled()) {
120: JPOXLogger.PERSISTENCE.debug(LOCALISER.msg("041034",
121: value));
122: }
123: } else {
124: JPOXLogger.PERSISTENCE.warn(LOCALISER.msg("041035"));
125: }
126:
127: return value;
128: }
129:
130: public Object getSampleValue(ClassLoaderResolver clr) {
131: return "NO SAMPLE AVAILABLE";
132: }
133:
134: public ScalarExpression newLiteral(QueryExpression qs, Object value) {
135: ScalarExpression expr = new ObjectLiteral(qs, this , value,
136: getType());
137: return expr;
138: }
139:
140: public ScalarExpression newScalarExpression(QueryExpression qs,
141: LogicSetExpression te) {
142: ScalarExpression expr = new ObjectExpression(qs, this , te);
143: return expr;
144: }
145:
146: public Class getJavaType() {
147: return OID.class;
148: }
149: }
|