Search in sources :

Example 1 with Validating

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;
}
Also used : ValidationException(org.apache.cayenne.validation.ValidationException) Validating(org.apache.cayenne.Validating) Persistent(org.apache.cayenne.Persistent) ValidationResult(org.apache.cayenne.validation.ValidationResult)

Aggregations

Persistent (org.apache.cayenne.Persistent)1 Validating (org.apache.cayenne.Validating)1 ValidationException (org.apache.cayenne.validation.ValidationException)1 ValidationResult (org.apache.cayenne.validation.ValidationResult)1