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.ClassLoaderResolver;
021: import org.jpox.StateManager;
022: import org.jpox.metadata.AbstractMemberMetaData;
023: import org.jpox.state.StateManagerFactory;
024: import org.jpox.store.DatastoreAdapter;
025: import org.jpox.store.DatastoreContainerObject;
026:
027: /**
028: * Mapping for a PC object embedded within another PC object (1-1 relation).
029: * Provides mapping for a single Java type (the PC type) to multiple datastore columns.
030: * Allows for nested embedded fields.
031: * Implements MappingCallbacks since if we are embedding a MappingCallbacks field (e.g a BLOB on Oracle)
032: * then we need in turn to call the underlying MappingCallbacks methods.
033: *
034: * @version $Revision: 1.35 $
035: **/
036: public class EmbeddedPCMapping extends EmbeddedMapping implements
037: MappingCallbacks {
038: /**
039: * Initialize this JavaTypeMapping with the given DatastoreAdapter for
040: * the given FieldMetaData.
041: *
042: * @param dba The Datastore Adapter that this Mapping should use.
043: * @param fmd FieldMetaData for the field to be mapped (if any)
044: * @param container The datastore container storing this mapping (if any)
045: * @param clr the ClassLoaderResolver
046: */
047: public void initialize(DatastoreAdapter dba,
048: AbstractMemberMetaData fmd,
049: DatastoreContainerObject container, ClassLoaderResolver clr) {
050: initialize(dba, fmd, container, clr, fmd.getEmbeddedMetaData(),
051: fmd.getTypeName(), StateManager.EMBEDDED_PC);
052: }
053:
054: /**
055: * MappingCallback called when the owning object is being fetched.
056: * @param sm StateManager of the owning object
057: */
058: public void postFetch(StateManager sm) {
059: if (fmd.getAbsoluteFieldNumber() < 0) {
060: return;
061: }
062:
063: // Find the SM for the embedded PC object
064: StateManager this SM = getStateManagerForEmbeddedObject(sm);
065: if (this SM == null) {
066: return;
067: }
068:
069: for (int i = 0; i < getNumberOfJavaTypeMappings(); i++) {
070: JavaTypeMapping m = getJavaTypeMapping(i);
071: if (m instanceof MappingCallbacks) {
072: ((MappingCallbacks) m).postFetch(this SM);
073: }
074: }
075: }
076:
077: /**
078: * MappingCallback called when the owning object has just being inserted.
079: * @param sm StateManager of the owning object
080: */
081: public void postInsert(StateManager sm) {
082: if (fmd.getAbsoluteFieldNumber() < 0) {
083: return;
084: }
085:
086: // Find the SM for the embedded PC object
087: StateManager this SM = getStateManagerForEmbeddedObject(sm);
088: if (this SM == null) {
089: return;
090: }
091:
092: // Call postInsert on any MappingCallbacks components
093: for (int i = 0; i < getNumberOfJavaTypeMappings(); i++) {
094: JavaTypeMapping m = getJavaTypeMapping(i);
095: if (m instanceof MappingCallbacks) {
096: ((MappingCallbacks) m).postInsert(this SM);
097: }
098: }
099: }
100:
101: /**
102: * MappingCallback called when the owning object has just being udpated.
103: * @param sm StateManager of the owning object
104: */
105: public void postUpdate(StateManager sm) {
106: if (fmd.getAbsoluteFieldNumber() < 0) {
107: return;
108: }
109:
110: // Find the SM for the embedded PC object
111: StateManager this SM = getStateManagerForEmbeddedObject(sm);
112: if (this SM == null) {
113: return;
114: }
115:
116: // Call postUpdate on any MappingCallbacks components
117: for (int i = 0; i < getNumberOfJavaTypeMappings(); i++) {
118: JavaTypeMapping m = getJavaTypeMapping(i);
119: if (m instanceof MappingCallbacks) {
120: ((MappingCallbacks) m).postUpdate(this SM);
121: }
122: }
123: }
124:
125: /**
126: * MappingCallback called when the owning object is about to be deleted.
127: * @param sm StateManager of the owning object
128: */
129: public void preDelete(StateManager sm) {
130: if (fmd.getAbsoluteFieldNumber() < 0) {
131: return;
132: }
133:
134: // Find the SM for the embedded PC object
135: StateManager this SM = getStateManagerForEmbeddedObject(sm);
136: if (this SM == null) {
137: return;
138: }
139:
140: // Call preDelete on any MappingCallbacks components
141: for (int i = 0; i < getNumberOfJavaTypeMappings(); i++) {
142: JavaTypeMapping m = getJavaTypeMapping(i);
143: if (m instanceof MappingCallbacks) {
144: ((MappingCallbacks) m).preDelete(this SM);
145: }
146: }
147: }
148:
149: /**
150: * Accessor for the StateManager of the embedded PC object when provided with the owner object.
151: * @param ownerSM StateManager of the owner
152: * @return StateManager of the embedded object
153: */
154: private StateManager getStateManagerForEmbeddedObject(
155: StateManager ownerSM) {
156: Object value = ownerSM.provideField(fmd
157: .getAbsoluteFieldNumber()); // Owner (non-embedded) PC
158: if (value == null) {
159: return null;
160: }
161:
162: StateManager this SM = ownerSM.getObjectManager()
163: .findStateManager(value);
164: if (this SM == null) {
165: // Assign a StateManager to manage our embedded object
166: this SM = StateManagerFactory.newStateManagerForEmbedded(
167: ownerSM.getObjectManager(), value, false);
168: thisSM.addEmbeddedOwner(ownerSM, fmd
169: .getAbsoluteFieldNumber());
170: thisSM.setPcObjectType(objectType);
171: }
172:
173: return thisSM;
174: }
175: }
|