use of org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException in project jaffa-framework by jaffa-projects.
the class GenericForeignKeyFieldValidator method validate.
/**
* The RulesEngine will invoke this method to perform the field validation.
* @throws ValidationException if any validation rule fails.
* @throws FrameworkException if any framework error occurs.
*/
public void validate() throws ValidationException, FrameworkException {
if (getValue() != null) {
UOW uow = getUow();
boolean localUow = (uow == null);
try {
if (localUow)
uow = new UOW();
Criteria c = new Criteria();
c.setTable(getDomainClassName());
c.addCriteria(getFieldNameForTable(), getTableName());
c.addCriteria(getFieldNameForField(), getFieldName());
c.addCriteria(getFieldNameForValue(), getValue().toString());
Collection col = uow.query(c);
if (col.size() == 0) {
// Invalid value. Display the list of valid values in the error message
StringBuffer validValues = new StringBuffer();
c = new Criteria();
c.setTable(getDomainClassName());
c.addCriteria(getFieldNameForTable(), getTableName());
c.addCriteria(getFieldNameForField(), getFieldName());
c.addOrderBy(getFieldNameForValue(), Criteria.ORDER_BY_ASC);
for (Iterator i = uow.query(c).iterator(); i.hasNext(); ) {
try {
Object value = BeanHelper.getField(i.next(), getFieldNameForValue());
if (validValues.length() > 0)
validValues.append(',');
validValues.append(value);
} catch (Exception e) {
// do nothing
}
}
String str = "Generic ForeignKey validation failed for the value '" + getValue() + "' against the table/field - " + getTableName() + '/' + getFieldName() + ". Valid values are " + validValues.toString();
log.error(str);
throw new InvalidGenericForeignKeyException(getLabelToken(), new Object[] { getTableName(), getFieldName(), validValues.toString() });
}
} finally {
if (localUow && uow != null)
uow.rollback();
}
}
}
use of org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException in project jaffa-framework by jaffa-projects.
the class GenericForeignKeyValidator method validateProperty.
/**
* {@inheritDoc}
*/
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
if (targetPropertyValue != null) {
for (RuleMetaData rule : rules) {
if (log.isDebugEnabled()) {
log.debug("Applying " + rule + " on " + targetPropertyValue);
}
String tableName = rule.getParameter(PARAMETER_TABLE_NAME);
String fieldName = rule.getParameter(PARAMETER_FIELD_NAME);
String domainClassName = rule.getParameter(PARAMETER_DOMAIN_CLASS_NAME);
String fieldNameForTable = rule.getParameter(PARAMETER_FIELD_NAME_FOR_TABLE);
String fieldNameForField = rule.getParameter(PARAMETER_FIELD_NAME_FOR_FIELD);
String fieldNameForValue = rule.getParameter(PARAMETER_FIELD_NAME_FOR_VALUE);
// Transform the value to the data type of the 'LegalValue' field of the Generic Foreign Key class.
Class valueClass = determineValueClass(domainClassName, fieldNameForValue);
Object value = DataTypeMapper.instance().map(targetPropertyValue, valueClass);
Criteria c = new Criteria();
c.setTable(domainClassName);
c.addCriteria(fieldNameForTable, tableName);
c.addCriteria(fieldNameForField, fieldName);
c.addCriteria(fieldNameForValue, value);
Collection col = uow.query(c);
if (!col.iterator().hasNext()) {
// Invalid value. Display the list of valid values in the error message
StringBuilder validValues = new StringBuilder();
c = new Criteria();
c.setTable(domainClassName);
c.addCriteria(fieldNameForTable, tableName);
c.addCriteria(fieldNameForField, fieldName);
c.addOrderBy(fieldNameForValue);
for (Object o : uow.query(c)) {
try {
Object legalValue = BeanHelper.getField(o, fieldNameForValue);
if (validValues.length() > 0)
validValues.append(',');
validValues.append(legalValue);
} catch (Exception e) {
// do nothing
}
}
if (log.isDebugEnabled()) {
log.debug("Generic ForeignKey validation failed for the value '" + value + "' against the table/field - " + tableName + '/' + fieldName + ". Valid values are " + validValues.toString());
}
String label = getPropertyLabel(targetObject, targetPropertyName);
Object[] arguments = getErrorArgumentArray(targetObject, rule);
if (arguments == null) {
arguments = new Object[] { tableName, fieldName, validValues.toString() };
}
throw wrapException(new InvalidGenericForeignKeyException(label, arguments), targetObject, rule);
}
}
}
}
Aggregations