Search in sources :

Example 1 with ValidationFailure

use of org.apache.cayenne.validation.ValidationFailure in project cayenne by apache.

the class BaseDataObject method validateForSave.

/**
 * Performs property validation of the object, appending any validation
 * failures to the provided validationResult object. This method is invoked
 * from "validateFor.." before committing a NEW or MODIFIED object to the
 * database. Validation includes checking for null values and value sizes.
 * CayenneDataObject subclasses may override this method, calling super.
 *
 * @since 1.1
 */
protected void validateForSave(ValidationResult validationResult) {
    ObjEntity objEntity = getObjectContext().getEntityResolver().getObjEntity(this);
    if (objEntity == null) {
        throw new CayenneRuntimeException("No ObjEntity mapping found for DataObject %s", getClass().getName());
    }
    // validate mandatory attributes
    Map<String, ValidationFailure> failedDbAttributes = null;
    for (ObjAttribute next : objEntity.getAttributes()) {
        // TODO: andrus, 2/20/2007 - handle embedded attribute
        if (next instanceof EmbeddedAttribute) {
            continue;
        }
        DbAttribute dbAttribute = next.getDbAttribute();
        if (dbAttribute == null) {
            throw new CayenneRuntimeException("ObjAttribute '%s" + "' does not have a corresponding DbAttribute", next.getName());
        }
        // pk may still be generated
        if (dbAttribute.isPrimaryKey()) {
            continue;
        }
        Object value = this.readPropertyDirectly(next.getName());
        if (dbAttribute.isMandatory()) {
            ValidationFailure failure = BeanValidationFailure.validateNotNull(this, next.getName(), value);
            if (failure != null) {
                if (failedDbAttributes == null) {
                    failedDbAttributes = new HashMap<>();
                }
                failedDbAttributes.put(dbAttribute.getName(), failure);
                continue;
            }
        }
        // validate length
        if (value != null && dbAttribute.getMaxLength() > 0) {
            if (value.getClass().isArray()) {
                int len = Array.getLength(value);
                if (len > dbAttribute.getMaxLength()) {
                    String message = "\"" + next.getName() + "\" exceeds maximum allowed length (" + dbAttribute.getMaxLength() + " bytes): " + len;
                    validationResult.addFailure(new BeanValidationFailure(this, next.getName(), message));
                }
            } else if (value instanceof CharSequence) {
                int len = ((CharSequence) value).length();
                if (len > dbAttribute.getMaxLength()) {
                    String message = "\"" + next.getName() + "\" exceeds maximum allowed length (" + dbAttribute.getMaxLength() + " chars): " + len;
                    validationResult.addFailure(new BeanValidationFailure(this, next.getName(), message));
                }
            }
        }
    }
    // validate mandatory relationships
    for (final ObjRelationship relationship : objEntity.getRelationships()) {
        List<DbRelationship> dbRels = relationship.getDbRelationships();
        if (dbRels.isEmpty()) {
            continue;
        }
        // see ObjRelationship.recalculateReadOnlyValue() for more info
        if (relationship.isSourceIndependentFromTargetChange()) {
            continue;
        }
        // if db relationship is not based on a PK and is based on mandatory
        // attributes, see if we have a target object set
        // relationship will be validated only if all db path has mandatory
        // db relationships
        boolean validate = true;
        for (DbRelationship dbRelationship : dbRels) {
            for (DbJoin join : dbRelationship.getJoins()) {
                DbAttribute source = join.getSource();
                if (source.isMandatory()) {
                    // clear attribute failures...
                    if (failedDbAttributes != null && !failedDbAttributes.isEmpty()) {
                        failedDbAttributes.remove(source.getName());
                    }
                } else {
                    // do not validate if the relation is based on
                    // multiple keys with some that can be nullable.
                    validate = false;
                }
            }
        }
        if (validate) {
            Object value = this.readPropertyDirectly(relationship.getName());
            ValidationFailure failure = BeanValidationFailure.validateNotNull(this, relationship.getName(), value);
            if (failure != null) {
                validationResult.addFailure(failure);
            }
        }
    }
    // deal with previously found attribute failures...
    if (failedDbAttributes != null && !failedDbAttributes.isEmpty()) {
        for (ValidationFailure failure : failedDbAttributes.values()) {
            validationResult.addFailure(failure);
        }
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) DbAttribute(org.apache.cayenne.map.DbAttribute) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) ObjEntity(org.apache.cayenne.map.ObjEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin)

Example 2 with ValidationFailure

use of org.apache.cayenne.validation.ValidationFailure in project cayenne by apache.

the class ValidatorDialog method showFailedObject.

private void showFailedObject() {
    if (problemsTable.getSelectedRow() >= 0) {
        ValidationFailure obj = (ValidationFailure) problemsTable.getModel().getValueAt(problemsTable.getSelectedRow(), 0);
        ValidationDisplayHandler.getErrorMsg(obj).displayField(getMediator(), super.getParentEditor());
    }
}
Also used : ValidationFailure(org.apache.cayenne.validation.ValidationFailure)

Example 3 with ValidationFailure

use of org.apache.cayenne.validation.ValidationFailure in project cayenne by apache.

the class GeneratorController method validateEntity.

protected ValidationFailure validateEntity(ObjEntity entity) {
    String name = entity.getName();
    if (entity.isGeneric()) {
        return new SimpleValidationFailure(name, "Generic class");
    }
    ValidationFailure emptyClass = BeanValidationFailure.validateNotEmpty(name, "className", entity.getClassName());
    if (emptyClass != null) {
        return emptyClass;
    }
    ValidationFailure badClass = BeanValidationFailure.validateJavaClassName(name, "className", entity.getClassName());
    if (badClass != null) {
        return badClass;
    }
    if (entity.getSuperClassName() != null) {
        ValidationFailure badSuperClass = BeanValidationFailure.validateJavaClassName(name, "superClassName", entity.getSuperClassName());
        if (badSuperClass != null) {
            return badSuperClass;
        }
    }
    return null;
}
Also used : SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure)

Example 4 with ValidationFailure

use of org.apache.cayenne.validation.ValidationFailure in project cayenne by apache.

the class GeneratorController method validateEmbeddable.

protected ValidationFailure validateEmbeddable(Embeddable embeddable) {
    String name = embeddable.getClassName();
    ValidationFailure emptyClass = BeanValidationFailure.validateNotEmpty(name, "className", embeddable.getClassName());
    if (emptyClass != null) {
        return emptyClass;
    }
    ValidationFailure badClass = BeanValidationFailure.validateJavaClassName(name, "className", embeddable.getClassName());
    if (badClass != null) {
        return badClass;
    }
    return null;
}
Also used : SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure)

Example 5 with ValidationFailure

use of org.apache.cayenne.validation.ValidationFailure in project cayenne by apache.

the class GeneratorController method validateEmbeddableAttribute.

private ValidationFailure validateEmbeddableAttribute(EmbeddableAttribute attribute) {
    String name = attribute.getEmbeddable().getClassName();
    ValidationFailure emptyName = BeanValidationFailure.validateNotEmpty(name, "attribute.name", attribute.getName());
    if (emptyName != null) {
        return emptyName;
    }
    ValidationFailure badName = CodeValidationUtil.validateJavaIdentifier(name, "attribute.name", attribute.getName());
    if (badName != null) {
        return badName;
    }
    ValidationFailure emptyType = BeanValidationFailure.validateNotEmpty(name, "attribute.type", attribute.getType());
    if (emptyType != null) {
        return emptyType;
    }
    ValidationFailure badType = BeanValidationFailure.validateJavaClassName(name, "attribute.type", attribute.getType());
    if (badType != null) {
        return badType;
    }
    return null;
}
Also used : SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure)

Aggregations

ValidationFailure (org.apache.cayenne.validation.ValidationFailure)17 BeanValidationFailure (org.apache.cayenne.validation.BeanValidationFailure)11 SimpleValidationFailure (org.apache.cayenne.validation.SimpleValidationFailure)7 ValidationResult (org.apache.cayenne.validation.ValidationResult)6 ObjEntity (org.apache.cayenne.map.ObjEntity)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 DbAttribute (org.apache.cayenne.map.DbAttribute)2 DbEntity (org.apache.cayenne.map.DbEntity)2 DbRelationship (org.apache.cayenne.map.DbRelationship)2 ProjectValidator (org.apache.cayenne.project.validation.ProjectValidator)2 Artist (org.apache.cayenne.testdo.testmap.Artist)2 File (java.io.File)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 MergerContext (org.apache.cayenne.dbsync.merge.context.MergerContext)1 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)1 ObjectNameGenerator (org.apache.cayenne.dbsync.naming.ObjectNameGenerator)1 ModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.ModelMergeDelegate)1