use of org.apache.cayenne.validation.BeanValidationFailure 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);
}
}
}
use of org.apache.cayenne.validation.BeanValidationFailure in project cayenne by apache.
the class CayenneDataObjectValidationIT method testValidateForSaveAttributeTooLong.
@Test
public void testValidateForSaveAttributeTooLong() throws Exception {
Artist artist = context.newObject(Artist.class);
DbEntity entity = context.getEntityResolver().getObjEntity(artist).getDbEntity();
int len = entity.getAttribute("ARTIST_NAME").getMaxLength();
StringBuffer buf = new StringBuffer(len);
for (int i = 0; i < len + 1; i++) {
buf.append("c");
}
artist.setArtistName(buf.toString());
ValidationResult result = new ValidationResult();
artist.validateForSave(result);
assertTrue(result.hasFailures());
assertTrue(result.hasFailures(artist));
List<ValidationFailure> failures = result.getFailures();
assertEquals(1, failures.size());
BeanValidationFailure failure = (BeanValidationFailure) failures.get(0);
assertEquals(Artist.ARTIST_NAME.getName(), failure.getProperty());
// fix the problem and see if it goes away
artist.setArtistName("aa");
result = new ValidationResult();
artist.validateForSave(result);
assertFalse(result.hasFailures());
}
use of org.apache.cayenne.validation.BeanValidationFailure in project cayenne by apache.
the class CayenneDataObjectValidationIT method testValidateForSaveMandatoryAttributeMissing.
@Test
public void testValidateForSaveMandatoryAttributeMissing() throws Exception {
Artist artist = context.newObject(Artist.class);
ValidationResult result = new ValidationResult();
artist.validateForSave(result);
assertTrue("Validation of 'artistName' should've failed.", result.hasFailures());
assertTrue(result.hasFailures(artist));
List<ValidationFailure> failures = result.getFailures();
assertEquals(1, failures.size());
BeanValidationFailure failure = (BeanValidationFailure) failures.get(0);
assertEquals(Artist.ARTIST_NAME.getName(), failure.getProperty());
// fix the problem and see if it goes away
artist.setArtistName("aa");
result = new ValidationResult();
artist.validateForSave(result);
assertFalse(result.hasFailures());
}
use of org.apache.cayenne.validation.BeanValidationFailure in project cayenne by apache.
the class GeneratorController method validateRelationship.
protected ValidationFailure validateRelationship(ObjRelationship relationship, boolean clientValidation) {
String name = relationship.getSourceEntity().getName();
ValidationFailure emptyName = BeanValidationFailure.validateNotEmpty(name, "relationship.name", relationship.getName());
if (emptyName != null) {
return emptyName;
}
ValidationFailure badName = CodeValidationUtil.validateJavaIdentifier(name, "relationship.name", relationship.getName());
if (badName != null) {
return badName;
}
if (!relationship.isToMany()) {
ObjEntity targetEntity = relationship.getTargetEntity();
if (clientValidation && targetEntity != null) {
targetEntity = targetEntity.getClientEntity();
}
if (targetEntity == null) {
return new BeanValidationFailure(name, "relationship.targetEntity", "No target entity");
} else if (!targetEntity.isGeneric()) {
ValidationFailure emptyClass = BeanValidationFailure.validateNotEmpty(name, "relationship.targetEntity.className", targetEntity.getClassName());
if (emptyClass != null) {
return emptyClass;
}
ValidationFailure badClass = BeanValidationFailure.validateJavaClassName(name, "relationship.targetEntity.className", targetEntity.getClassName());
if (badClass != null) {
return badClass;
}
}
}
return null;
}
use of org.apache.cayenne.validation.BeanValidationFailure in project cayenne by apache.
the class CayenneDataObjectValidationIT method testValidateForSaveMandatoryToOneMissing.
@Test
public void testValidateForSaveMandatoryToOneMissing() throws Exception {
Exhibit exhibit = context.newObject(Exhibit.class);
exhibit.setOpeningDate(new Date());
exhibit.setClosingDate(new Date());
ValidationResult result = new ValidationResult();
exhibit.validateForSave(result);
assertTrue("Validation of 'toGallery' should've failed.", result.hasFailures());
assertTrue(result.hasFailures(exhibit));
List<ValidationFailure> failures = result.getFailures();
assertEquals(1, failures.size());
BeanValidationFailure failure = (BeanValidationFailure) failures.get(0);
assertEquals(Exhibit.TO_GALLERY.getName(), failure.getProperty());
// fix the problem and see if it goes away
Gallery gallery = context.newObject(Gallery.class);
exhibit.setToGallery(gallery);
result = new ValidationResult();
exhibit.validateForSave(result);
assertFalse("No failures expected: " + result, result.hasFailures());
}
Aggregations