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