Search in sources :

Example 1 with IFieldValidator

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;
            }
        }
    }
}
Also used : IFieldValidator(org.jaffa.rules.fieldvalidators.IFieldValidator) ValidationException(org.jaffa.datatypes.ValidationException) FrameworkException(org.jaffa.exceptions.FrameworkException) MalformedURLException(java.net.MalformedURLException) FrameworkException(org.jaffa.exceptions.FrameworkException) FileNotFoundException(java.io.FileNotFoundException) ValidationException(org.jaffa.datatypes.ValidationException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1 ValidationException (org.jaffa.datatypes.ValidationException)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 IFieldValidator (org.jaffa.rules.fieldvalidators.IFieldValidator)1