*/
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()) {
Identity removedId = (Identity) removedItor.next();
Object reldel = tx.fetch(fieldClassMolder, removedId, null);
if (reldel != null) {
if (_fieldMolder.isDependent()) {
tx.delete(reldel);
} else {
fieldClassMolder.removeRelation(tx, reldel,
this._classMolder, object);
}
// } else {
// // should i notify user that the object does not exist?
// // user can't delete dependent object himself. So, must
// // error.
}
}
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 addedValue = addedItor.next();
if (_fieldMolder.isDependent()) {
if (!tx.isRecorded(addedValue)) {
tx.markCreate(fieldClassMolder, addedValue, oid);
// } else {
// // should i notify user that the object does not exist?
// // user can't create dependent object himself. So, must
// // be
// // an error.
}
} else if (tx.isAutoStore()) {
if (!tx.isRecorded(addedValue)) {
tx.markCreate(fieldClassMolder, addedValue, null);
}
}
}
// it would be good if we also compare the new field element with
// the element in the transaction, when debug is set true
} 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);
// if ( fieldMolder.isDependent() ) {
Iterator itor = deleted.iterator();
while (itor.hasNext()) {
flags.setUpdateCache(true);
Object toBeDeleted = lazy.find(itor.next());
if (toBeDeleted != null && tx.isPersistent(toBeDeleted)) {
if (_fieldMolder.isDependent()) {
tx.delete(toBeDeleted);
} else {
fieldClassMolder.removeRelation(tx, toBeDeleted,
this._classMolder, object);
}
// } else {
// // what to do if it happens?
}