use of org.apache.cayenne.Validating in project cayenne by apache.
the class ObjectStoreGraphDiff method validateAndCheckNoop.
/**
* Requires external synchronization on ObjectStore.
*/
boolean validateAndCheckNoop() {
if (getChangesByObjectId().isEmpty()) {
return true;
}
boolean noop = true;
// build a new collection for validation as validation methods may
// result in
// ObjectStore modifications
Collection<Validating> objectsToValidate = null;
for (final ObjectDiff diff : getChangesByObjectId().values()) {
if (!diff.isNoop()) {
noop = false;
if (diff.getObject() instanceof Validating) {
if (objectsToValidate == null) {
objectsToValidate = new ArrayList<>();
}
objectsToValidate.add((Validating) diff.getObject());
}
}
}
if (objectsToValidate != null) {
ValidationResult result = new ValidationResult();
for (Validating object : objectsToValidate) {
switch(((Persistent) object).getPersistenceState()) {
case PersistenceState.NEW:
object.validateForInsert(result);
break;
case PersistenceState.MODIFIED:
object.validateForUpdate(result);
break;
case PersistenceState.DELETED:
object.validateForDelete(result);
break;
}
}
if (result.hasFailures()) {
throw new ValidationException(result);
}
}
return noop;
}
Aggregations