private void processDeleteRules(DataObject object, int oldState)
throws DeleteDenyException {
ObjEntity entity = dataContext.getEntityResolver().lookupObjEntity(object);
Iterator it = entity.getRelationships().iterator();
while (it.hasNext()) {
ObjRelationship relationship = (ObjRelationship) it.next();
boolean processFlattened = relationship.isFlattened()
&& relationship.isToDependentEntity();
// first check for no action... bail out if no flattened processing is needed
if (relationship.getDeleteRule() == DeleteRule.NO_ACTION && !processFlattened) {
continue;
}
List relatedObjects = Collections.EMPTY_LIST;
if (relationship.isToMany()) {
List toMany = (List) object.readNestedProperty(relationship.getName());
if (toMany.size() > 0) {
// Get a copy of the list so that deleting objects doesn't
// result in concurrent modification exceptions
relatedObjects = new ArrayList(toMany);
}
}
else {
Object relatedObject = object.readNestedProperty(relationship.getName());
if (relatedObject != null) {
relatedObjects = Collections.singletonList(relatedObject);
}
}
// no related object, bail out
if (relatedObjects.size() == 0) {
continue;
}
// process DENY rule first...
if (relationship.getDeleteRule() == DeleteRule.DENY) {
object.setPersistenceState(oldState);
String message = relatedObjects.size() == 1
? "1 related object"
: relatedObjects.size() + " related objects";
throw new DeleteDenyException(object, relationship.getName(), message);
}
// process flattened with dependent join tables...
// joins must be removed even if they are non-existent or ignored in the
// object graph
if (processFlattened) {
ObjectStore objectStore = dataContext.getObjectStore();
Iterator iterator = relatedObjects.iterator();
while (iterator.hasNext()) {
DataObject relatedObject = (DataObject) iterator.next();
objectStore.recordArcDeleted(
object,
relatedObject.getObjectId(),
relationship.getName());
}
}
// process remaining rules
switch (relationship.getDeleteRule()) {
case DeleteRule.NO_ACTION:
break;
case DeleteRule.NULLIFY:
ObjRelationship inverseRelationship = relationship
.getReverseRelationship();
if (inverseRelationship == null) {
// nothing we can do here
break;
}
if (inverseRelationship.isToMany()) {
Iterator iterator = relatedObjects.iterator();
while (iterator.hasNext()) {
DataObject relatedObject = (DataObject) iterator.next();
relatedObject.removeToManyTarget(inverseRelationship
.getName(), object, true);
}
}
else {
// Inverse is to-one - find all related objects and
// nullify the reverse relationship
Iterator iterator = relatedObjects.iterator();
while (iterator.hasNext()) {
DataObject relatedObject = (DataObject) iterator.next();
relatedObject.setToOneTarget(
inverseRelationship.getName(),
null,
true);
}
}