KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > collections > DMapEntry


1 package org.apache.ojb.odmg.collections;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.Serializable JavaDoc;
19 import java.util.Map.Entry;
20
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.TransactionExt;
31 import org.apache.ojb.odmg.TransactionImpl;
32 import org.apache.ojb.odmg.TxManagerFactory;
33
34 /**
35  * @author <a HREF="mailto:thma@apache.org">Thomas Mahler<a>
36  * @version $Id: DMapEntry.java,v 1.18.2.2 2005/12/21 22:29:50 tomdz Exp $
37  */

38
39 public class DMapEntry implements Entry, Serializable JavaDoc, PersistenceBrokerAware
40 {
41     private static final long serialVersionUID = 4382757889982004339L;
42     private transient Logger log = LoggerFactory.getLogger(DMapEntry.class);
43
44     private PBKey pbKey;
45
46     private Integer JavaDoc id;
47     private Integer JavaDoc dmapId;
48     private Identity keyOid;
49     private Identity valueOid;
50
51     /* declare transient because the object is not required to be serializable and we can reload it via the oid */
52     private transient Object JavaDoc keyRealSubject;
53     /* declare transient because the object is not required to be serializable and we can reload it via the oid */
54     private transient Object JavaDoc valueRealSubject;
55
56     /**
57      * Used to materialize DMaps from the database.
58      */

59     public DMapEntry()
60     {
61 // if(getTransaction() == null)
62
// {
63
// throw new TransactionNotInProgressException("Materialization of DCollection instances must be done with a tx");
64
// }
65
this.pbKey = getPBKey();
66     }
67
68     /**
69      * DMapEntry constructor comment.
70      */

71     public DMapEntry(DMapImpl map, Object JavaDoc key, Object JavaDoc value)
72     {
73         if(map != null)
74         {
75             dmapId = map.getId();
76         }
77         keyRealSubject = key;
78         valueRealSubject = value;
79         getPBKey();
80     }
81
82     protected Logger getLog()
83     {
84         if(log == null)
85         {
86             log = LoggerFactory.getLogger(DMapEntry.class);
87         }
88         return log;
89     }
90
91     protected TransactionImpl getTransaction()
92     {
93         return TxManagerFactory.instance().getTransaction();
94     }
95
96     public PBKey getPBKey()
97     {
98         if(pbKey == null)
99         {
100             TransactionImpl tx = getTransaction();
101             if(tx != null && tx.isOpen())
102             {
103                 pbKey = tx.getBroker().getPBKey();
104             }
105         }
106         return pbKey;
107     }
108
109     protected void prepareForPersistency(PersistenceBroker broker)
110     {
111         if(keyOid == null)
112         {
113             if(keyRealSubject == null)
114             {
115                 throw new OJBRuntimeException("Key identity and real key object are 'null' - Can not persist empty entry");
116             }
117             else
118             {
119                 keyOid = broker.serviceIdentity().buildIdentity(keyRealSubject);
120             }
121         }
122         if(valueOid == null)
123         {
124             if(valueRealSubject == null)
125             {
126                 throw new OJBRuntimeException("Key identity and real key object are 'null' - Can not persist empty entry");
127             }
128             else
129             {
130                 valueOid = broker.serviceIdentity().buildIdentity(valueRealSubject);
131             }
132         }
133     }
134
135     protected void prepareKeyRealSubject(PersistenceBroker broker)
136     {
137         if(keyOid == null)
138         {
139             getLog().info("Cannot retrieve real key object because its id is not known");
140         }
141         else
142         {
143             keyRealSubject = broker.getObjectByIdentity(keyOid);
144         }
145     }
146
147     protected void prepareValueRealSubject(PersistenceBroker broker)
148     {
149         if(valueOid == null)
150         {
151             getLog().info("Cannot retrieve real key object because its id is not known");
152         }
153         else
154         {
155             valueRealSubject = broker.getObjectByIdentity(valueOid);
156         }
157     }
158
159     /**
160      * Returns the real key object.
161      */

162     public Object JavaDoc getRealKey()
163     {
164         if(keyRealSubject != null)
165         {
166             return keyRealSubject;
167         }
168         else
169         {
170             TransactionExt tx = getTransaction();
171
172             if((tx != null) && tx.isOpen())
173             {
174                 prepareKeyRealSubject(tx.getBroker());
175             }
176             else
177             {
178                 if(getPBKey() != null)
179                 {
180                     PBCapsule capsule = new PBCapsule(getPBKey(), null);
181
182                     try
183                     {
184                         prepareKeyRealSubject(capsule.getBroker());
185                     }
186                     finally
187                     {
188                         capsule.destroy();
189                     }
190                 }
191                 else
192                 {
193                     getLog().warn("No tx, no PBKey - can't materialise key with Identity " + getKeyOid());
194                 }
195             }
196         }
197         return keyRealSubject;
198     }
199
200     /* (non-Javadoc)
201      * @see java.util.Map.Entry#getKey()
202      */

203     public Object JavaDoc getKey()
204     {
205         // we don't save the key object itself but only its identiy
206
// so we now might have to load the object from the db
207
if((keyRealSubject == null))
208         {
209             return getRealKey();
210         }
211         else
212         {
213             return keyRealSubject;
214         }
215     }
216
217     /**
218      * Returns the real value object.
219      */

220     public Object JavaDoc getRealValue()
221     {
222         if(valueRealSubject != null)
223         {
224             return valueRealSubject;
225         }
226         else
227         {
228             TransactionExt tx = getTransaction();
229
230             if((tx != null) && tx.isOpen())
231             {
232                 prepareValueRealSubject(tx.getBroker());
233             }
234             else
235             {
236                 if(getPBKey() != null)
237                 {
238                     PBCapsule capsule = new PBCapsule(getPBKey(), null);
239
240                     try
241                     {
242                         prepareValueRealSubject(capsule.getBroker());
243                     }
244                     finally
245                     {
246                         capsule.destroy();
247                     }
248                 }
249                 else
250                 {
251                     getLog().warn("No tx, no PBKey - can't materialise value with Identity " + getKeyOid());
252                 }
253             }
254         }
255         return valueRealSubject;
256     }
257
258     /* (non-Javadoc)
259      * @see java.util.Map.Entry#getValue()
260      */

261     public Object JavaDoc getValue()
262     {
263         // we don't save the value object itself but only its identiy
264
// so we now might have to load the object from the db
265
if((valueRealSubject == null))
266         {
267             return getRealValue();
268         }
269         else
270         {
271             return valueRealSubject;
272         }
273     }
274
275     /*
276      * (non-Javadoc)
277      * @see java.util.Map.Entry#setValue(java.lang.Object)
278      */

279     public Object JavaDoc setValue(Object JavaDoc obj)
280     {
281         Object JavaDoc old = valueRealSubject;
282
283         valueRealSubject = obj;
284         return old;
285     }
286
287     /**
288      * Gets the dmapId.
289      *
290      * @return Returns a int
291      */

292     public Integer JavaDoc getDmapId()
293     {
294         return dmapId;
295     }
296
297     /**
298      * Sets the dmapId.
299      *
300      * @param dmapId The dmapId to set
301      */

302     public void setDmapId(Integer JavaDoc dmapId)
303     {
304         this.dmapId = dmapId;
305     }
306
307     /**
308      * Gets the id.
309      *
310      * @return Returns a int
311      */

312     public Integer JavaDoc getId()
313     {
314         return id;
315     }
316
317     /**
318      * Sets the id.
319      *
320      * @param id The id to set
321      */

322     public void setId(Integer JavaDoc id)
323     {
324         this.id = id;
325     }
326
327     public Identity getKeyOid()
328     {
329         return keyOid;
330     }
331
332     public void setKeyOid(Identity keyOid)
333     {
334         this.keyOid = keyOid;
335     }
336
337     public Identity getValueOid()
338     {
339         return valueOid;
340     }
341
342     public void setValueOid(Identity valueOid)
343     {
344         this.valueOid = valueOid;
345     }
346
347     //===================================================
348
// PersistenceBrokerAware interface methods
349
//===================================================
350
public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException
351     {
352         // before insert we have to build the Identity objects of the persistent objects
353
// we can't do this ealier, because we can now expect that the persistent object
354
// was written to DB (we make sure in code that the persistent object was locked before
355
// this entry) and the generated Identity is valid
356
prepareForPersistency(broker);
357     }
358
359     public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException{}
360     public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException{}
361     public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException{}
362     public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException{}
363     public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException{}
364     public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException{}
365 }
Popular Tags