use of org.jaffa.rules.fieldvalidators.IFieldValidator in project jaffa-framework by jaffa-projects.
the class RulesEngine method invokeRules.
/**
* This will invoke all the rules for a field.
*/
private static void invokeRules(Object fieldValue, UOW uow, boolean doOnlyMandatory, String labelToken, FieldMetaData fieldMetaData) throws ValidationException, FrameworkException {
RuleMetaData[] rules = fieldMetaData.getRules();
for (int i = 0; i < rules.length; i++) {
RuleMetaData rule = rules[i];
IFieldValidator validator = null;
try {
FieldValidatorMetaData fieldValidatorMetaData = ValidatorMetaDataService.getFieldValidatorMetaData(rule.getName());
if (!doOnlyMandatory || fieldValidatorMetaData.getMandatory()) {
// Create an instance of the validator
validator = (IFieldValidator) Class.forName(fieldValidatorMetaData.getClassName()).newInstance();
// Perform the initialization
validator.init();
// Set some values
validator.setValue(fieldValue);
validator.setLabelToken(labelToken);
validator.setUow(uow);
// Set the Parameters, if specified in the validators.xml file
if (fieldValidatorMetaData.getParameters() != null) {
for (Iterator itr = fieldValidatorMetaData.getParameters().entrySet().iterator(); itr.hasNext(); ) {
Map.Entry me = (Map.Entry) itr.next();
BeanHelper.setField(validator, (String) me.getKey(), (String) me.getValue());
}
}
// Set the Parameters provided in the rules.xml file
for (Iterator itr = rule.getParameters().entrySet().iterator(); itr.hasNext(); ) {
Map.Entry me = (Map.Entry) itr.next();
BeanHelper.setField(validator, (String) me.getKey(), (String) me.getValue());
}
// Now perform validation
if (log.isDebugEnabled())
log.debug("Invoking the rule " + rule.getName() + " for field " + fieldMetaData.getName());
validator.validate();
}
} catch (ValidationException e) {
throw e;
} catch (FrameworkException e) {
throw e;
} catch (Exception e) {
String str = "Error thrown while trying to invoke the rules for the field '" + labelToken + "', having the value '" + fieldValue + "', using the fieldMetaData - " + fieldMetaData;
log.error(str, e);
throw new RulesEngineException(RulesEngineException.RULE_INVOCATION_FAILED, new Object[] { labelToken, fieldValue }, e);
} finally {
if (validator != null) {
validator.cleanup();
validator = null;
}
}
}
}
Aggregations