*
* @since 1.1
*/
protected void validateForSave(ValidationResult validationResult) {
ObjEntity objEntity = getDataContext().getEntityResolver().lookupObjEntity(this);
if (objEntity == null) {
throw new CayenneRuntimeException(
"No ObjEntity mapping found for DataObject " + getClass().getName());
}
DataNode node = getDataContext().getParentDataDomain().lookupDataNode(
objEntity.getDataMap());
if (node == null) {
throw new CayenneRuntimeException("No DataNode found for objEntity: "
+ objEntity.getName());
}
ExtendedTypeMap types = node.getAdapter().getExtendedTypes();
// validate mandatory attributes
// handling a special case - meaningful mandatory FK... defer failures until
// relationship validation is done... This is just a temporary solution, as
// handling meaningful keys within the object lifecycle requires something more,
// namely read/write methods for relationships and direct values should be
// synchronous with each other..
Map failedDbAttributes = null;
Iterator attributes = objEntity.getAttributes().iterator();
while (attributes.hasNext()) {
ObjAttribute objAttribute = (ObjAttribute) attributes.next();
DbAttribute dbAttribute = objAttribute.getDbAttribute();
Object value = this.readPropertyDirectly(objAttribute.getName());
if (dbAttribute.isMandatory()) {
ValidationFailure failure = BeanValidationFailure.validateNotNull(
this,
objAttribute.getName(),
value);
if (failure != null) {
if (failedDbAttributes == null) {
failedDbAttributes = new HashMap();
}
failedDbAttributes.put(dbAttribute.getName(), failure);
continue;
}
}
if (value != null) {
// TODO: should we pass null values for validation as well?
// if so, class can be obtained from ObjAttribute...
types.getRegisteredType(value.getClass()).validateProperty(
this,
objAttribute.getName(),
value,
dbAttribute,
validationResult);
}
}
// validate mandatory relationships
Iterator relationships = objEntity.getRelationships().iterator();
while (relationships.hasNext()) {
ObjRelationship relationship = (ObjRelationship) relationships.next();
if (relationship.isSourceIndependentFromTargetChange()) {
continue;