Search in sources :

Example 26 with RuleMetaData

use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.

the class PrimaryKeyValidator method validateProperty.

@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
    // No need to perform validations for an IPersistent instance that exists in the database
    if (targetObject instanceof IPersistent && ((IPersistent) targetObject).isDatabaseOccurence()) {
        if (log.isDebugEnabled()) {
            log.debug(getName() + " check not performed since the target object already exists in the database");
        }
        return;
    }
    RuleMetaData rule = rules.get(0);
    if (log.isDebugEnabled()) {
        log.debug("Applying " + rule + " on " + targetObject);
    }
    Criteria criteria = null;
    String pk = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
    String[] pkFields = pk.split(",");
    for (String pkField : pkFields) {
        Object pkValue = null;
        try {
            pkValue = BeanHelper.getField(targetObject, pkField);
        } catch (NoSuchMethodException e) {
            if (log.isDebugEnabled()) {
                log.debug("No Such Method Exception: " + targetObject.getClass().getName() + "." + pkField, e);
            }
            return;
        }
        if (pkValue == null) {
            if (log.isDebugEnabled()) {
                log.debug(getName() + " check not performed since the key field " + pkField + " is null");
            }
            return;
        } else {
            // Add to criteria
            if (criteria == null) {
                criteria = new Criteria();
            }
            criteria.addCriteria(pkField, pkValue);
        }
    }
    if (criteria != null) {
        criteria.setTable(getActualClassName(targetObject.getClass()));
        if (uow.query(criteria).iterator().hasNext()) {
            if (log.isDebugEnabled()) {
                log.debug("Primary key '" + pk + "' is not unique for the object " + targetObject);
            }
            String objectLabel = getObjectLabel(getActualClassName(targetObject.getClass()), targetObject);
            throw wrapException(new DuplicateKeyException(objectLabel), targetObject, rule);
        }
    }
}
Also used : IPersistent(org.jaffa.persistence.IPersistent) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) Criteria(org.jaffa.persistence.Criteria) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException)

Example 27 with RuleMetaData

use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.

the class RuleValidatorFactory method createValidator.

/**
 * Creates a validator.
 *
 * @param className    the name of the class to create a validator for.
 * @param metaDataList the class meta data to create a validator from.
 * @return a validator.
 */
private Validator<?> createValidator(String className, List<ClassMetaData> metaDataList) {
    // create a validator to collect all rule validators for this class
    BatchValidator<?> resultValidator = new BatchValidator();
    // associated with the class
    for (ClassMetaData classMetaData : metaDataList) {
        // Retrieve class-level rules
        List<RuleMetaData> allRulesForClass = classMetaData.getClassRules();
        if (allRulesForClass != null) {
            for (RuleMetaData ruleMetaData : allRulesForClass) {
                String ruleName = ruleMetaData.getName();
                try {
                    // get the map of rules that apply to the class in new condition
                    Map<String, List<RuleMetaData>> ruleMap = getPropertyRuleMap(className, ruleName);
                    // look up the validator from the context
                    RuleValidator foundValidator = (RuleValidator) lookupValidator(ruleName);
                    if (ruleMap != null && !ruleMap.isEmpty() && foundValidator != null) {
                        // add the property map to the validator which was found in the context
                        foundValidator.setRuleMap(ruleMap);
                        // save the validator to the batch validator
                        if (!resultValidator.getValidatorSet().contains(foundValidator))
                            resultValidator.getValidatorSet().add(foundValidator);
                    }
                } catch (Exception e) {
                    logger.error("Error occurred creating validator for rule: " + ruleName + " while getting property rule map");
                }
            }
        }
        List<PropertyMetaData> propertyMetaDataList = classMetaData.getProperties();
        if (propertyMetaDataList != null) {
            // iterate list of properties for this class to determine rules associated with it.
            for (PropertyMetaData propertyMetaData : propertyMetaDataList) {
                List<RuleMetaData> allRulesForProperty = propertyMetaData.getRules(className);
                if (allRulesForProperty != null) {
                    // constructed validator
                    for (RuleMetaData ruleMetaData : allRulesForProperty) {
                        String ruleName = ruleMetaData.getName();
                        try {
                            // get the map of rules that apply to the class in new condition
                            Map<String, List<RuleMetaData>> ruleMap = getPropertyRuleMap(className, ruleName);
                            // look up the validator from the context
                            RuleValidator foundValidator = (RuleValidator) lookupValidator(ruleName);
                            if (ruleMap != null && !ruleMap.isEmpty() && foundValidator != null) {
                                // add the property map to the validator which was found in the context
                                foundValidator.setRuleMap(ruleMap);
                                // save the validator to the batch validator
                                if (!resultValidator.getValidatorSet().contains(foundValidator)) {
                                    resultValidator.getValidatorSet().add(foundValidator);
                                } else {
                                    // if a validator of the same type exists in the batch, extract the rule meta
                                    // / data and append to the existing property map
                                    appendPropertyRule(resultValidator, foundValidator, ruleMap);
                                }
                            }
                        } catch (Exception e) {
                            logger.error("Error occurred creating validator for rule: " + ruleName + " while getting property rule map");
                        }
                    }
                }
            }
        }
    }
    return resultValidator;
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) ArrayList(java.util.ArrayList) List(java.util.List) PropertyMetaData(org.jaffa.rules.meta.PropertyMetaData) ClassMetaData(org.jaffa.rules.meta.ClassMetaData) FrameworkException(org.jaffa.exceptions.FrameworkException) BeansException(org.springframework.beans.BeansException)

Example 28 with RuleMetaData

use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.

the class RuleValidatorTest method testConditionalValidation.

/**
 * When a condition is specified on a rule, property validation should only be called if the condition is true.
 */
@Test
public void testConditionalValidation() throws FrameworkException, ApplicationExceptions, ApplicationException {
    TestValidator target = new TestValidator();
    IRuleEvaluator ruleEvaluator = mock(IRuleEvaluator.class);
    target.setRuleEvaluator(ruleEvaluator);
    Map<String, List<RuleMetaData>> ruleMap = new HashMap<>();
    List<RuleMetaData> rules = new ArrayList<>();
    RuleMetaData trueCondition = new RuleMetaData();
    rules.add(trueCondition);
    RuleMetaData falseCondition = new RuleMetaData();
    rules.add(falseCondition);
    ruleMap.put("field1", rules);
    target.setRuleMap(ruleMap);
    TestModel obj = new TestModel();
    obj.setField1("value");
    when(ruleEvaluator.checkCondition(obj, trueCondition)).thenReturn(true);
    target.validate(obj);
    assertEquals(1, target.validatedRules.size());
    assertEquals(trueCondition, target.validatedRules.get(0));
}
Also used : IRuleEvaluator(org.jaffa.rules.rulemeta.IRuleEvaluator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Example 29 with RuleMetaData

use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.

the class SOAEventBaseHandler method raiseSOAEvent.

/**
 * Check if the method being invoked on the handler is the trigger method defined in the rule meta data then raise the event
 *
 * @param invocationMethod method being invoked
 * @param target           the target object
 * @param uow              unit of work for the life cycle event handler
 * @throws ApplicationExceptions
 * @throws FrameworkException
 */
protected void raiseSOAEvent(String invocationMethod, Object target, UOW uow, Object[] args) throws ApplicationExceptions, FrameworkException {
    if (log.isDebugEnabled()) {
        log.debug("Handle Event : " + invocationMethod + "  for " + " (Target=" + shortClassName(target) + ")");
    }
    if (invocationMethod.equals(ruleMetaData.getParameter(RuleMetaData.PARAMETER_TRIGGER))) {
        // Need to check the condition
        List<RuleMetaData> rules = new ArrayList<>();
        rules.add(ruleMetaData);
        IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleMetaData.getName());
        // Check if the rule is applicable for the target object (evaluates rule condition)
        List<RuleMetaData> applicableRules = ruleHelper.getApplicableRules(target.getClass().getName(), target, rules, true);
        // raise event for each applicableRule
        if (applicableRules != null) {
            for (RuleMetaData rule : applicableRules) {
                raiseSOAEvent(uow, target, rule, args);
            }
        }
    }
}
Also used : IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) ArrayList(java.util.ArrayList) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 30 with RuleMetaData

use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.

the class CxfFunctionGuardInterceptor method checkAccess.

/**
 * Checks if a caller has access to a method being invoked
 *
 * @param method Method being invoked
 * @throws AccessControlException Thrown when the caller doesn't have access
 */
private void checkAccess(Method method) throws AccessControlException, ApplicationExceptions, FrameworkException {
    String targetClassName = method.getDeclaringClass().getName();
    // Get the rule map
    Map<String, List<RuleMetaData>> ruleMap = getPropertyRuleMap(targetClassName, "function-guard");
    if (ruleMap == null) {
        return;
    }
    // Get the rules
    List<RuleMetaData> rules = ruleMap.get(null);
    if (rules == null) {
        return;
    }
    // Check access for any rules that match
    for (RuleMetaData rule : rules) {
        if (match(method, rule)) {
            checkAccess(method, targetClassName, rule);
        }
    }
}
Also used : List(java.util.List) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Aggregations

RuleMetaData (org.jaffa.rules.meta.RuleMetaData)30 List (java.util.List)10 ArrayList (java.util.ArrayList)9 FrameworkException (org.jaffa.exceptions.FrameworkException)8 ApplicationException (org.jaffa.exceptions.ApplicationException)7 JaffaRulesFrameworkException (org.jaffa.rules.JaffaRulesFrameworkException)7 IRuleHelper (org.jaffa.rules.rulemeta.IRuleHelper)6 Map (java.util.Map)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Criteria (org.jaffa.persistence.Criteria)3 ClassMetaData (org.jaffa.rules.meta.ClassMetaData)3 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 ListIterator (java.util.ListIterator)2 FlexBean (org.jaffa.flexfields.FlexBean)2 IPersistent (org.jaffa.persistence.IPersistent)2 RulesEngineException (org.jaffa.rules.RulesEngineException)2 PropertyMetaData (org.jaffa.rules.meta.PropertyMetaData)2