* java.lang.Object)
*/
public UpdateFlags preStore(final TransactionContext tx, final OID oid,
final Object object, final int timeout, final Object field)
throws PersistenceException {
UpdateFlags flags = new UpdateFlags();
ClassMolder fieldClassMolder = _fieldMolder.getFieldClassMolder();
Object value = _fieldMolder.getValue(object, tx.getClassLoader());
ArrayList orgFields = (ArrayList) field;
if (!(value instanceof Lazy)) {
Collection removed = ClassMolderHelper.getRemovedIdsList(tx,
orgFields, value, fieldClassMolder);
Iterator removedItor = removed.iterator();
if (removedItor.hasNext()) {
if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
flags.setUpdatePersist(true);
}
flags.setUpdateCache(true);
}
while (removedItor.hasNext()) {
// must be loaded thur transaction, so that the related object
// is properly locked and updated before we delete it.
Identity identity = (Identity) removedItor.next();
if (!tx.isDeletedByOID(new OID(fieldClassMolder, identity))) {
ProposedEntity proposedValue = new ProposedEntity(fieldClassMolder);
Object reldel = tx.load(identity, proposedValue, null);
if (reldel != null && tx.isPersistent(reldel)) {
tx.writeLock(reldel, tx.getLockTimeout());
_fieldMolder.getRelationLoader().deleteRelation(
tx.getConnection(oid.getMolder().getLockEngine()),
oid.getIdentity(), identity);
fieldClassMolder.removeRelation(tx, reldel, _classMolder, object);
}
}
}
Collection added = ClassMolderHelper.getAddedValuesList(tx,
orgFields, value, fieldClassMolder);
Iterator addedItor = added.iterator();
if (addedItor.hasNext()) {
if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
flags.setUpdatePersist(true);
}
flags.setUpdateCache(true);
}
while (addedItor.hasNext()) {
Object addedField = addedItor.next();
tx.markModified(addedField, false/* updatePersist */,
true/* updateCache */);
if (tx.isPersistent(addedField)) {
_fieldMolder.getRelationLoader().createRelation(
tx.getConnection(oid.getMolder().getLockEngine()),
oid.getIdentity(),
fieldClassMolder.getIdentity(tx, addedField));
} else {
if (tx.isAutoStore()) {
if (!tx.isDeleted(addedField)) {
tx.markCreate(fieldClassMolder, addedField, null);
}
}
}
}
} else {
RelationCollection lazy = (RelationCollection) value;
// this RelationCollection has to clean up its state at the end of
// the
// transaction
tx.addTxSynchronizable(lazy);
ArrayList deleted = lazy.getDeleted();
if (!deleted.isEmpty()) {
if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
flags.setUpdatePersist(true);
}
flags.setUpdateCache(true);
Iterator itor = deleted.iterator();
while (itor.hasNext()) {
flags.setUpdateCache(true);
Identity deletedId = (Identity) itor.next();
Object toBeDeleted = lazy.find(deletedId);
if (toBeDeleted != null) {
if (tx.isPersistent(toBeDeleted)) {
tx.writeLock(toBeDeleted, 0);
_fieldMolder.getRelationLoader().deleteRelation(
tx.getConnection(oid.getMolder().getLockEngine()),
oid.getIdentity(), deletedId);
fieldClassMolder.removeRelation(tx, toBeDeleted,
this._classMolder, object);
}
// } else {
// // what to do if it happens?
}
}
}
ArrayList added = lazy.getAdded();
if (!added.isEmpty()) {
if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
flags.setUpdatePersist(true);
}
flags.setUpdateCache(true);
Iterator itor = added.iterator();
while (itor.hasNext()) {
Identity addedId = (Identity) itor.next();
Object toBeAdded = lazy.find(addedId);