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 org.jpox.ObjectManager;
021: import org.jpox.ObjectManagerHelper;
022: import org.jpox.StateManager;
023: import org.jpox.api.ApiAdapter;
024: import org.jpox.state.StateManagerFactory;
025:
026: /**
027: * Mapping for a serialised reference (Interface/Object) field.
028: * Extends SerialisedMapping since that provides the basic serialisation mechanism,
029: * adding on the addition of StateManagers to the serialised object whenever it is required
030: * (since the object is a PersistenceCapable).
031: *
032: * @version $Revision: 1.13 $
033: */
034: public class SerialisedReferenceMapping extends SerialisedMapping {
035: /**
036: * Method to populate parameter positions in a PreparedStatement with this object
037: * @param om The Object Manager
038: * @param preparedStatement The Prepared Statement
039: * @param exprIndex The parameter positions to populate
040: * @param value The value of the PC to use in populating the parameter positions
041: */
042: public void setObject(ObjectManager om, Object preparedStatement,
043: int[] exprIndex, Object value) {
044: if (fmd != null) {
045: setObject(om, preparedStatement, exprIndex, value, null,
046: fmd.getAbsoluteFieldNumber());
047: } else {
048: super .setObject(om, preparedStatement, exprIndex, value);
049: }
050: }
051:
052: /**
053: * Method to populate parameter positions in a PreparedStatement with this object
054: * @param om The Object Manager
055: * @param preparedStatement The Prepared Statement
056: * @param exprIndex The parameter positions to populate
057: * @param value The value of the PC to use in populating the parameter positions
058: * @param ownerSM State Manager for the owning object
059: * @param fieldNumber field number of this object in the owning object
060: */
061: public void setObject(ObjectManager om, Object preparedStatement,
062: int[] exprIndex, Object value, StateManager ownerSM,
063: int fieldNumber) {
064: ApiAdapter api = om.getApiAdapter();
065: if (api.isPersistable(value)) {
066: // Assign a StateManager to the serialised object if none present
067: StateManager embSM = om.findStateManager(value);
068: if (embSM == null
069: || ObjectManagerHelper.getObjectManager(value) == null) {
070: embSM = StateManagerFactory.newStateManagerForEmbedded(
071: om, value, false);
072: embSM.addEmbeddedOwner(ownerSM, fieldNumber);
073: }
074: }
075:
076: StateManager sm = null;
077: if (api.isPersistable(value)) {
078: // Find SM for serialised PC object
079: sm = om.findStateManager(value);
080: }
081:
082: if (sm != null) {
083: sm.setStoringPC();
084: }
085: getDataStoreMapping(0).setObject(preparedStatement,
086: exprIndex[0], value);
087: if (sm != null) {
088: sm.unsetStoringPC();
089: }
090: }
091:
092: /**
093: * Method to extract the value of the PersistenceCapable from a ResultSet.
094: * @param om The Object Manager
095: * @param resultSet The ResultSet
096: * @param exprIndex The parameter positions in the result set to use.
097: * @return The (deserialised) PersistenceCapable object
098: */
099: public Object getObject(ObjectManager om, Object resultSet,
100: int[] exprIndex) {
101: if (fmd != null) {
102: return getObject(om, resultSet, exprIndex, null, fmd
103: .getAbsoluteFieldNumber());
104: } else {
105: return super .getObject(om, resultSet, exprIndex);
106: }
107: }
108:
109: /**
110: * Method to extract the value of the PersistenceCapable from a ResultSet.
111: * @param om The Object Manager
112: * @param resultSet The ResultSet
113: * @param exprIndex The parameter positions in the result set to use.
114: * @param ownerSM The owning object
115: * @param fieldNumber Absolute number of field in owner object
116: * @return The (deserialised) PersistenceCapable object
117: */
118: public Object getObject(ObjectManager om, Object resultSet,
119: int[] exprIndex, StateManager ownerSM, int fieldNumber) {
120: Object obj = getDataStoreMapping(0).getObject(resultSet,
121: exprIndex[0]);
122: ApiAdapter api = om.getApiAdapter();
123: if (api.isPersistable(obj)) {
124: // Assign a StateManager to the serialised object if none present
125: StateManager embSM = om.findStateManager(obj);
126: if (embSM == null
127: || ObjectManagerHelper.getObjectManager(obj) == null) {
128: embSM = StateManagerFactory.newStateManagerForEmbedded(
129: om, obj, false);
130: embSM.addEmbeddedOwner(ownerSM, fieldNumber); // Feed dirty flags to the owning object
131: }
132: }
133: return obj;
134: }
135: }
|