1 | |
package org.apache.ojb.odmg.collections; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.io.Serializable; |
19 | |
|
20 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
21 | |
import org.apache.ojb.broker.Identity; |
22 | |
import org.apache.ojb.broker.OJBRuntimeException; |
23 | |
import org.apache.ojb.broker.PBKey; |
24 | |
import org.apache.ojb.broker.PersistenceBroker; |
25 | |
import org.apache.ojb.broker.PersistenceBrokerAware; |
26 | |
import org.apache.ojb.broker.PersistenceBrokerException; |
27 | |
import org.apache.ojb.broker.util.logging.Logger; |
28 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
29 | |
import org.apache.ojb.odmg.PBCapsule; |
30 | |
import org.apache.ojb.odmg.RuntimeObject; |
31 | |
import org.apache.ojb.odmg.TransactionImpl; |
32 | |
import org.apache.ojb.odmg.TxManagerFactory; |
33 | |
import org.odmg.Transaction; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class DListEntry implements Serializable, PersistenceBrokerAware |
41 | |
{ |
42 | |
private static final long serialVersionUID = 5251476492626009907L; |
43 | |
|
44 | |
|
45 | |
|
46 | |
private transient Logger log; |
47 | |
protected transient Object realSubject; |
48 | |
protected PBKey pbKey; |
49 | |
|
50 | |
protected Integer id; |
51 | |
protected Integer dlistId; |
52 | |
protected Identity oid; |
53 | |
protected int position; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public DListEntry() |
60 | |
{ |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
getPBKey(); |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public DListEntry(DListImpl theDList, Object theObject) |
79 | |
{ |
80 | |
this.dlistId = theDList.getId(); |
81 | |
this.position = theDList.size(); |
82 | |
this.realSubject = theObject; |
83 | |
this.pbKey = getPBKey(); |
84 | |
} |
85 | |
|
86 | |
protected Logger getLog() |
87 | |
{ |
88 | |
if(log == null) |
89 | |
{ |
90 | |
log = LoggerFactory.getLogger(DListEntry.class); |
91 | |
} |
92 | |
return log; |
93 | |
} |
94 | |
|
95 | |
protected TransactionImpl getTransaction() |
96 | |
{ |
97 | |
return TxManagerFactory.instance().getTransaction(); |
98 | |
} |
99 | |
|
100 | |
public PBKey getPBKey() |
101 | |
{ |
102 | |
if(pbKey == null) |
103 | |
{ |
104 | |
TransactionImpl tx = getTransaction(); |
105 | |
if(tx != null && tx.isOpen()) |
106 | |
{ |
107 | |
pbKey = tx.getBroker().getPBKey(); |
108 | |
} |
109 | |
} |
110 | |
return pbKey; |
111 | |
} |
112 | |
|
113 | |
protected void prepareForPersistency(PersistenceBroker broker) |
114 | |
{ |
115 | |
if(oid == null) |
116 | |
{ |
117 | |
if(realSubject == null) |
118 | |
{ |
119 | |
throw new OJBRuntimeException("Identity and real object are 'null' - Can not persist empty entry"); |
120 | |
} |
121 | |
else |
122 | |
{ |
123 | |
oid = broker.serviceIdentity().buildIdentity(realSubject); |
124 | |
} |
125 | |
} |
126 | |
} |
127 | |
|
128 | |
protected void prepareRealSubject(PersistenceBroker broker) |
129 | |
{ |
130 | |
if(oid == null) |
131 | |
{ |
132 | |
throw new OJBRuntimeException("can not return real object, real object and Identity is null"); |
133 | |
} |
134 | |
realSubject = broker.getObjectByIdentity(oid); |
135 | |
} |
136 | |
|
137 | |
public Object getRealSubject() |
138 | |
{ |
139 | |
if(realSubject != null) |
140 | |
{ |
141 | |
return realSubject; |
142 | |
} |
143 | |
else |
144 | |
{ |
145 | |
TransactionImpl tx = getTransaction(); |
146 | |
if(tx != null && tx.isOpen()) |
147 | |
{ |
148 | |
prepareRealSubject(tx.getBroker()); |
149 | |
if(realSubject != null) |
150 | |
{ |
151 | |
RuntimeObject rt = new RuntimeObject(realSubject, tx, false); |
152 | |
tx.lockAndRegister(rt, Transaction.READ, tx.getRegistrationList()); |
153 | |
} |
154 | |
} |
155 | |
else |
156 | |
{ |
157 | |
PBKey aPbKey = getPBKey(); |
158 | |
if(aPbKey != null) |
159 | |
{ |
160 | |
PBCapsule capsule = new PBCapsule(aPbKey, null); |
161 | |
try |
162 | |
{ |
163 | |
prepareRealSubject(capsule.getBroker()); |
164 | |
} |
165 | |
finally |
166 | |
{ |
167 | |
capsule.destroy(); |
168 | |
} |
169 | |
} |
170 | |
else |
171 | |
{ |
172 | |
getLog().warn("No tx, no PBKey - can't materialise object with Identity " + getOid()); |
173 | |
} |
174 | |
} |
175 | |
} |
176 | |
return realSubject; |
177 | |
} |
178 | |
|
179 | |
public void setRealSubject(Object realSubject) |
180 | |
{ |
181 | |
this.realSubject = realSubject; |
182 | |
} |
183 | |
|
184 | |
public int getPosition() |
185 | |
{ |
186 | |
return position; |
187 | |
} |
188 | |
|
189 | |
public void setPosition(int newPosition) |
190 | |
{ |
191 | |
position = newPosition; |
192 | |
} |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public Integer getDlistId() |
200 | |
{ |
201 | |
return dlistId; |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public void setDlistId(Integer dlistId) |
210 | |
{ |
211 | |
this.dlistId = dlistId; |
212 | |
} |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public Integer getId() |
220 | |
{ |
221 | |
return id; |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public void setId(Integer id) |
230 | |
{ |
231 | |
this.id = id; |
232 | |
} |
233 | |
|
234 | |
public Identity getOid() |
235 | |
{ |
236 | |
return oid; |
237 | |
} |
238 | |
|
239 | |
public void setOid(Identity oid) |
240 | |
{ |
241 | |
this.oid = oid; |
242 | |
} |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
public String toString() |
248 | |
{ |
249 | |
ToStringBuilder buf = new ToStringBuilder(this); |
250 | |
buf.append("id", id); |
251 | |
buf.append("dListId", dlistId); |
252 | |
buf.append("position", position); |
253 | |
buf.append("identity", oid); |
254 | |
buf.append("realSubject", realSubject); |
255 | |
return buf.toString(); |
256 | |
} |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException |
263 | |
{ |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
prepareForPersistency(broker); |
269 | |
} |
270 | |
|
271 | |
public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException{} |
272 | |
public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException{} |
273 | |
public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException{} |
274 | |
public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException{} |
275 | |
public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException{} |
276 | |
public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException{} |
277 | |
} |