| 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 | |
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 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public class DMapEntry implements Entry, Serializable, 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 id; |
| 47 | |
private Integer dmapId; |
| 48 | |
private Identity keyOid; |
| 49 | |
private Identity valueOid; |
| 50 | |
|
| 51 | |
|
| 52 | |
private transient Object keyRealSubject; |
| 53 | |
|
| 54 | |
private transient Object valueRealSubject; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public DMapEntry() |
| 60 | |
{ |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
this.pbKey = getPBKey(); |
| 66 | |
} |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public DMapEntry(DMapImpl map, Object key, Object 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 | |
|
| 161 | |
|
| 162 | |
public Object 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 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
public Object getKey() |
| 204 | |
{ |
| 205 | |
|
| 206 | |
|
| 207 | |
if((keyRealSubject == null)) |
| 208 | |
{ |
| 209 | |
return getRealKey(); |
| 210 | |
} |
| 211 | |
else |
| 212 | |
{ |
| 213 | |
return keyRealSubject; |
| 214 | |
} |
| 215 | |
} |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
public Object 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 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
public Object getValue() |
| 262 | |
{ |
| 263 | |
|
| 264 | |
|
| 265 | |
if((valueRealSubject == null)) |
| 266 | |
{ |
| 267 | |
return getRealValue(); |
| 268 | |
} |
| 269 | |
else |
| 270 | |
{ |
| 271 | |
return valueRealSubject; |
| 272 | |
} |
| 273 | |
} |
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
public Object setValue(Object obj) |
| 280 | |
{ |
| 281 | |
Object old = valueRealSubject; |
| 282 | |
|
| 283 | |
valueRealSubject = obj; |
| 284 | |
return old; |
| 285 | |
} |
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
public Integer getDmapId() |
| 293 | |
{ |
| 294 | |
return dmapId; |
| 295 | |
} |
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
public void setDmapId(Integer dmapId) |
| 303 | |
{ |
| 304 | |
this.dmapId = dmapId; |
| 305 | |
} |
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
public Integer getId() |
| 313 | |
{ |
| 314 | |
return id; |
| 315 | |
} |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
public void setId(Integer 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 | |
|
| 349 | |
|
| 350 | |
public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException |
| 351 | |
{ |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 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 | |
} |