001: package org.apache.ojb.odmg.collections;
002:
003: /* Copyright 2003-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:
020: import org.apache.commons.lang.builder.ToStringBuilder;
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.RuntimeObject;
031: import org.apache.ojb.odmg.TransactionImpl;
032: import org.apache.ojb.odmg.TxManagerFactory;
033: import org.odmg.Transaction;
034:
035: /**
036: * Encapsulates an DList entry object.
037: *
038: * @version $Id: DListEntry.java,v 1.25.2.4 2005/12/21 22:29:50 tomdz Exp $
039: */
040: public class DListEntry implements Serializable, PersistenceBrokerAware {
041: private static final long serialVersionUID = 5251476492626009907L;
042: /*
043: * declare transient, because ManageableCollection entries need to be {@link java.io.Serializable}.
044: */
045: private transient Logger log;
046: protected transient Object realSubject;
047: protected PBKey pbKey;
048:
049: protected Integer id;
050: protected Integer dlistId;
051: protected Identity oid;
052: protected int position;
053:
054: /**
055: * Used to instantiate persistent DLists from DB by the kernel
056: * FOR INTERNAL USE ONLY
057: */
058: public DListEntry() {
059: /*
060: arminw:
061: When PB kernel fill DList with DListEntry, the DListEntry needs to know the current
062: used PBKey, because we need to lookup the real objects when user iterates the list,
063: thus we need the associated PBKey to find right PB/DB connection.
064: TODO: Find a better solution
065: */
066: // if(getTransaction() == null)
067: // {
068: // throw new TransactionNotInProgressException("Materialization of DCollection instances must be done with a tx");
069: // }
070: getPBKey();
071: }
072:
073: /**
074: * Standard way to instantiate new entries
075: */
076: public DListEntry(DListImpl theDList, Object theObject) {
077: this .dlistId = theDList.getId();
078: this .position = theDList.size();
079: this .realSubject = theObject;
080: this .pbKey = getPBKey();
081: }
082:
083: protected Logger getLog() {
084: if (log == null) {
085: log = LoggerFactory.getLogger(DListEntry.class);
086: }
087: return log;
088: }
089:
090: protected TransactionImpl getTransaction() {
091: return TxManagerFactory.instance().getTransaction();
092: }
093:
094: public PBKey getPBKey() {
095: if (pbKey == null) {
096: TransactionImpl tx = getTransaction();
097: if (tx != null && tx.isOpen()) {
098: pbKey = tx.getBroker().getPBKey();
099: }
100: }
101: return pbKey;
102: }
103:
104: protected void prepareForPersistency(PersistenceBroker broker) {
105: if (oid == null) {
106: if (realSubject == null) {
107: throw new OJBRuntimeException(
108: "Identity and real object are 'null' - Can not persist empty entry");
109: } else {
110: oid = broker.serviceIdentity().buildIdentity(
111: realSubject);
112: }
113: }
114: }
115:
116: protected void prepareRealSubject(PersistenceBroker broker) {
117: if (oid == null) {
118: throw new OJBRuntimeException(
119: "can not return real object, real object and Identity is null");
120: }
121: realSubject = broker.getObjectByIdentity(oid);
122: }
123:
124: public Object getRealSubject() {
125: if (realSubject != null) {
126: return realSubject;
127: } else {
128: TransactionImpl tx = getTransaction();
129: if (tx != null && tx.isOpen()) {
130: prepareRealSubject(tx.getBroker());
131: if (realSubject != null) {
132: RuntimeObject rt = new RuntimeObject(realSubject,
133: tx, false);
134: tx.lockAndRegister(rt, Transaction.READ, tx
135: .getRegistrationList());
136: }
137: } else {
138: PBKey aPbKey = getPBKey();
139: if (aPbKey != null) {
140: PBCapsule capsule = new PBCapsule(aPbKey, null);
141: try {
142: prepareRealSubject(capsule.getBroker());
143: } finally {
144: capsule.destroy();
145: }
146: } else {
147: getLog().warn(
148: "No tx, no PBKey - can't materialise object with Identity "
149: + getOid());
150: }
151: }
152: }
153: return realSubject;
154: }
155:
156: public void setRealSubject(Object realSubject) {
157: this .realSubject = realSubject;
158: }
159:
160: public int getPosition() {
161: return position;
162: }
163:
164: public void setPosition(int newPosition) {
165: position = newPosition;
166: }
167:
168: /**
169: * Gets the dlistId.
170: *
171: * @return Returns a int
172: */
173: public Integer getDlistId() {
174: return dlistId;
175: }
176:
177: /**
178: * Sets the dlistId.
179: *
180: * @param dlistId The dlistId to set
181: */
182: public void setDlistId(Integer dlistId) {
183: this .dlistId = dlistId;
184: }
185:
186: /**
187: * Gets the id.
188: *
189: * @return Returns a int
190: */
191: public Integer getId() {
192: return id;
193: }
194:
195: /**
196: * Sets the id.
197: *
198: * @param id The id to set
199: */
200: public void setId(Integer id) {
201: this .id = id;
202: }
203:
204: public Identity getOid() {
205: return oid;
206: }
207:
208: public void setOid(Identity oid) {
209: this .oid = oid;
210: }
211:
212: /**
213: * return String representation.
214: */
215: public String toString() {
216: ToStringBuilder buf = new ToStringBuilder(this );
217: buf.append("id", id);
218: buf.append("dListId", dlistId);
219: buf.append("position", position);
220: buf.append("identity", oid);
221: buf.append("realSubject", realSubject);
222: return buf.toString();
223: }
224:
225: //===================================================
226: // PersistenceBrokerAware interface methods
227: //===================================================
228: public void beforeInsert(PersistenceBroker broker)
229: throws PersistenceBrokerException {
230: // before insert we have to build the Identity objects of the persistent objects
231: // we can't do this ealier, because we can now expect that the persistent object
232: // was written to DB (we make sure in code that the persistent object was locked before
233: // this entry) and the generated Identity is valid
234: prepareForPersistency(broker);
235: }
236:
237: public void beforeUpdate(PersistenceBroker broker)
238: throws PersistenceBrokerException {
239: }
240:
241: public void beforeDelete(PersistenceBroker broker)
242: throws PersistenceBrokerException {
243: }
244:
245: public void afterLookup(PersistenceBroker broker)
246: throws PersistenceBrokerException {
247: }
248:
249: public void afterDelete(PersistenceBroker broker)
250: throws PersistenceBrokerException {
251: }
252:
253: public void afterInsert(PersistenceBroker broker)
254: throws PersistenceBrokerException {
255: }
256:
257: public void afterUpdate(PersistenceBroker broker)
258: throws PersistenceBrokerException {
259: }
260: }
|