001: package org.apache.ojb.odmg.collections;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.Serializable;
019: import java.util.Map.Entry;
020:
021: import org.apache.ojb.broker.Identity;
022: import org.apache.ojb.broker.OJBRuntimeException;
023: import org.apache.ojb.broker.PBKey;
024: import org.apache.ojb.broker.PersistenceBroker;
025: import org.apache.ojb.broker.PersistenceBrokerAware;
026: import org.apache.ojb.broker.PersistenceBrokerException;
027: import org.apache.ojb.broker.util.logging.Logger;
028: import org.apache.ojb.broker.util.logging.LoggerFactory;
029: import org.apache.ojb.odmg.PBCapsule;
030: import org.apache.ojb.odmg.TransactionExt;
031: import org.apache.ojb.odmg.TransactionImpl;
032: import org.apache.ojb.odmg.TxManagerFactory;
033:
034: /**
035: * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
036: * @version $Id: DMapEntry.java,v 1.18.2.2 2005/12/21 22:29:50 tomdz Exp $
037: */
038:
039: public class DMapEntry implements Entry, Serializable,
040: PersistenceBrokerAware {
041: private static final long serialVersionUID = 4382757889982004339L;
042: private transient Logger log = LoggerFactory
043: .getLogger(DMapEntry.class);
044:
045: private PBKey pbKey;
046:
047: private Integer id;
048: private Integer dmapId;
049: private Identity keyOid;
050: private Identity valueOid;
051:
052: /* declare transient because the object is not required to be serializable and we can reload it via the oid */
053: private transient Object keyRealSubject;
054: /* declare transient because the object is not required to be serializable and we can reload it via the oid */
055: private transient Object valueRealSubject;
056:
057: /**
058: * Used to materialize DMaps from the database.
059: */
060: public DMapEntry() {
061: // if(getTransaction() == null)
062: // {
063: // throw new TransactionNotInProgressException("Materialization of DCollection instances must be done with a tx");
064: // }
065: this .pbKey = getPBKey();
066: }
067:
068: /**
069: * DMapEntry constructor comment.
070: */
071: public DMapEntry(DMapImpl map, Object key, Object value) {
072: if (map != null) {
073: dmapId = map.getId();
074: }
075: keyRealSubject = key;
076: valueRealSubject = value;
077: getPBKey();
078: }
079:
080: protected Logger getLog() {
081: if (log == null) {
082: log = LoggerFactory.getLogger(DMapEntry.class);
083: }
084: return log;
085: }
086:
087: protected TransactionImpl getTransaction() {
088: return TxManagerFactory.instance().getTransaction();
089: }
090:
091: public PBKey getPBKey() {
092: if (pbKey == null) {
093: TransactionImpl tx = getTransaction();
094: if (tx != null && tx.isOpen()) {
095: pbKey = tx.getBroker().getPBKey();
096: }
097: }
098: return pbKey;
099: }
100:
101: protected void prepareForPersistency(PersistenceBroker broker) {
102: if (keyOid == null) {
103: if (keyRealSubject == null) {
104: throw new OJBRuntimeException(
105: "Key identity and real key object are 'null' - Can not persist empty entry");
106: } else {
107: keyOid = broker.serviceIdentity().buildIdentity(
108: keyRealSubject);
109: }
110: }
111: if (valueOid == null) {
112: if (valueRealSubject == null) {
113: throw new OJBRuntimeException(
114: "Key identity and real key object are 'null' - Can not persist empty entry");
115: } else {
116: valueOid = broker.serviceIdentity().buildIdentity(
117: valueRealSubject);
118: }
119: }
120: }
121:
122: protected void prepareKeyRealSubject(PersistenceBroker broker) {
123: if (keyOid == null) {
124: getLog()
125: .info(
126: "Cannot retrieve real key object because its id is not known");
127: } else {
128: keyRealSubject = broker.getObjectByIdentity(keyOid);
129: }
130: }
131:
132: protected void prepareValueRealSubject(PersistenceBroker broker) {
133: if (valueOid == null) {
134: getLog()
135: .info(
136: "Cannot retrieve real key object because its id is not known");
137: } else {
138: valueRealSubject = broker.getObjectByIdentity(valueOid);
139: }
140: }
141:
142: /**
143: * Returns the real key object.
144: */
145: public Object getRealKey() {
146: if (keyRealSubject != null) {
147: return keyRealSubject;
148: } else {
149: TransactionExt tx = getTransaction();
150:
151: if ((tx != null) && tx.isOpen()) {
152: prepareKeyRealSubject(tx.getBroker());
153: } else {
154: if (getPBKey() != null) {
155: PBCapsule capsule = new PBCapsule(getPBKey(), null);
156:
157: try {
158: prepareKeyRealSubject(capsule.getBroker());
159: } finally {
160: capsule.destroy();
161: }
162: } else {
163: getLog().warn(
164: "No tx, no PBKey - can't materialise key with Identity "
165: + getKeyOid());
166: }
167: }
168: }
169: return keyRealSubject;
170: }
171:
172: /* (non-Javadoc)
173: * @see java.util.Map.Entry#getKey()
174: */
175: public Object getKey() {
176: // we don't save the key object itself but only its identiy
177: // so we now might have to load the object from the db
178: if ((keyRealSubject == null)) {
179: return getRealKey();
180: } else {
181: return keyRealSubject;
182: }
183: }
184:
185: /**
186: * Returns the real value object.
187: */
188: public Object getRealValue() {
189: if (valueRealSubject != null) {
190: return valueRealSubject;
191: } else {
192: TransactionExt tx = getTransaction();
193:
194: if ((tx != null) && tx.isOpen()) {
195: prepareValueRealSubject(tx.getBroker());
196: } else {
197: if (getPBKey() != null) {
198: PBCapsule capsule = new PBCapsule(getPBKey(), null);
199:
200: try {
201: prepareValueRealSubject(capsule.getBroker());
202: } finally {
203: capsule.destroy();
204: }
205: } else {
206: getLog().warn(
207: "No tx, no PBKey - can't materialise value with Identity "
208: + getKeyOid());
209: }
210: }
211: }
212: return valueRealSubject;
213: }
214:
215: /* (non-Javadoc)
216: * @see java.util.Map.Entry#getValue()
217: */
218: public Object getValue() {
219: // we don't save the value object itself but only its identiy
220: // so we now might have to load the object from the db
221: if ((valueRealSubject == null)) {
222: return getRealValue();
223: } else {
224: return valueRealSubject;
225: }
226: }
227:
228: /*
229: * (non-Javadoc)
230: * @see java.util.Map.Entry#setValue(java.lang.Object)
231: */
232: public Object setValue(Object obj) {
233: Object old = valueRealSubject;
234:
235: valueRealSubject = obj;
236: return old;
237: }
238:
239: /**
240: * Gets the dmapId.
241: *
242: * @return Returns a int
243: */
244: public Integer getDmapId() {
245: return dmapId;
246: }
247:
248: /**
249: * Sets the dmapId.
250: *
251: * @param dmapId The dmapId to set
252: */
253: public void setDmapId(Integer dmapId) {
254: this .dmapId = dmapId;
255: }
256:
257: /**
258: * Gets the id.
259: *
260: * @return Returns a int
261: */
262: public Integer getId() {
263: return id;
264: }
265:
266: /**
267: * Sets the id.
268: *
269: * @param id The id to set
270: */
271: public void setId(Integer id) {
272: this .id = id;
273: }
274:
275: public Identity getKeyOid() {
276: return keyOid;
277: }
278:
279: public void setKeyOid(Identity keyOid) {
280: this .keyOid = keyOid;
281: }
282:
283: public Identity getValueOid() {
284: return valueOid;
285: }
286:
287: public void setValueOid(Identity valueOid) {
288: this .valueOid = valueOid;
289: }
290:
291: //===================================================
292: // PersistenceBrokerAware interface methods
293: //===================================================
294: public void beforeInsert(PersistenceBroker broker)
295: throws PersistenceBrokerException {
296: // before insert we have to build the Identity objects of the persistent objects
297: // we can't do this ealier, because we can now expect that the persistent object
298: // was written to DB (we make sure in code that the persistent object was locked before
299: // this entry) and the generated Identity is valid
300: prepareForPersistency(broker);
301: }
302:
303: public void beforeUpdate(PersistenceBroker broker)
304: throws PersistenceBrokerException {
305: }
306:
307: public void beforeDelete(PersistenceBroker broker)
308: throws PersistenceBrokerException {
309: }
310:
311: public void afterLookup(PersistenceBroker broker)
312: throws PersistenceBrokerException {
313: }
314:
315: public void afterDelete(PersistenceBroker broker)
316: throws PersistenceBrokerException {
317: }
318:
319: public void afterInsert(PersistenceBroker broker)
320: throws PersistenceBrokerException {
321: }
322:
323: public void afterUpdate(PersistenceBroker broker)
324: throws PersistenceBrokerException {
325: }
326: }
|