throws PersistenceException {
UpdateFlags flags = new UpdateFlags();
ClassMolder fieldClassMolder = _fieldMolder.getFieldClassMolder();
Object value = _fieldMolder.getValue(object, tx.getClassLoader());
Identity curIdentity = (Identity) field;
Identity newIdentity = null;
if (value != null) {
newIdentity = fieldClassMolder.getIdentity(tx, value);
flags.setNewField(newIdentity);
}
// | yip: don't delete the following comment,
// until it proved working by time. :-P
// if ids are the same or not canCreate
// if object is deleted
// warn
// if object are the same
// done
// not the same
// exception
// ids not the same or canCreate
// if depend
// if old is not null
// delete old
// removeRelation
// if canCreate
// create new
// not depend and autoStore
// if old is not null
// removeRelation
// if canCreate
// createObject
// not depend nor autoStore
// if old is not null
// removeRelation
// if new is not null
if (ClassMolderHelper.isEquals(curIdentity, newIdentity)) {
/*
* Let's deal with a situation where there's no dependent object (field == null),
* a 'new' dependent object has been set (value != null), but as we are using a key
* generator on this newly set object, calling fieldClassMolder.getIdentity() will
* return null (and hence newField == null). In this case, we still have to mark this
* new object for creation and instruct Castor to update the cache(s) as well.
*/
if ((field == null) && (value != null)
&& _fieldMolder.isDependent() && !tx.isRecorded(value)) {
if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
flags.setUpdatePersist(true);
}
flags.setUpdateCache(true);
tx.markCreate(fieldClassMolder, value, oid);
}
//TODO [WG]: can anybody please explain to me the meaning of the next two lines.
if (!_debug) { return flags; }
if (curIdentity == null) { return flags; } // do the next field if both are null
if ((value != null) && tx.isDeleted(value)) {
LOG.warn ("Deleted object found!");
if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
flags.setUpdatePersist(true);
}
flags.setUpdateCache(true);
_fieldMolder.setValue(object, null, tx.getClassLoader());
return flags;
}
if (tx.isAutoStore() || _fieldMolder.isDependent()) {
if (value != tx.fetch(fieldClassMolder, curIdentity, null)) {
throw new DuplicateIdentityException("");
}
}
} else {
if (_fieldMolder.isStored() /* && _fieldMolder.isCheckDirty() */) {
flags.setUpdatePersist(true);
}
flags.setUpdateCache(true);
if (_fieldMolder.isDependent()) {
if (curIdentity != null) {
Object reldel = tx.fetch(fieldClassMolder, curIdentity, null);
if (reldel != null) {
tx.delete(reldel);
}
}
if ((value != null) && !tx.isRecorded(value)) {
tx.markCreate(fieldClassMolder, value, oid);
}
} else if (tx.isAutoStore()) {
if (curIdentity != null) {
Object deref = tx.fetch(fieldClassMolder, curIdentity, null);
if (deref != null) {
fieldClassMolder.removeRelation(tx, deref, _classMolder, object);
}
}
if ((value != null) && !tx.isRecorded(value)) {
tx.markCreate(fieldClassMolder, value, null);
}
} else {
if (curIdentity != null) {
Object deref = tx.fetch(fieldClassMolder, curIdentity, null);
if (deref != null) {
fieldClassMolder.removeRelation(tx, deref, _classMolder, object);
}
}
// yip: user're pretty easily to run into cache
// integrity problem here, if user forgot to create
// "value" explicitly. We autoload the value for him.
if ((value != null) && !tx.isRecorded(value)) {
Identity fieldValue = fieldClassMolder.getIdentity(tx, value);
if (fieldValue != null) {
ProposedEntity temp = new ProposedEntity(fieldClassMolder);
tx.load(fieldValue, temp, null);
_fieldMolder.setValue(object, temp.getEntity(), tx.getClassLoader());
} else {