Search in sources :

Example 16 with RuleMetaData

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

the class AbstractRuleHelper method getApplicableRules.

/**
 * Selects the rules to be applied from the input.
 * Performs the variation check. An input rule will be ignored if its variation attribute does not match the variation in the thread context.
 * Performs the execution-realm check. An input rule will be ignored if the execution-realm of the input class does not match the supportedExecutionRealms for the rule.
 * Performs the condition check. An input rule having a condition will be ignored, if the evaluation of its condition script returns a false or if the Object is null (provided the condition does not contain the word 'bean').
 * The precedence for the rule is used to determine the rule(s) to be passed back.
 * @param className the target class.
 * @param obj the instance on which the rules are to be applied.
 * @param rules the input list of rules.
 * @param executionRealmCheck decides if executionRealm checks are to be performed.
 * @return the rules to be applied.
 * @throws ApplicationExceptions if any application exception occurs.
 * @throws FrameworkException if any internal error occurs.
 */
public List<RuleMetaData> getApplicableRules(String className, Object obj, List<RuleMetaData> rules, boolean executionRealmCheck) throws ApplicationExceptions, FrameworkException {
    if (rules != null && rules.size() > 0) {
        // Assume that the input rules are of the same type. Pick the Rule info based on the very first input rule
        Rule ruleInfo = findRuleInfo(rules.get(0));
        List<RuleMetaData> output = null;
        if (ruleInfo.isPrecedenceLast()) {
            // Return the first valid rule starting from the end of the list
            for (ListIterator<RuleMetaData> litr = rules.listIterator(rules.size()); litr.hasPrevious(); ) {
                RuleMetaData rule = litr.previous();
                if (check(className, obj, rule, executionRealmCheck, ruleInfo)) {
                    if (output == null)
                        output = new ArrayList<RuleMetaData>(1);
                    output.add(rule);
                    break;
                }
            }
        } else if (ruleInfo.isPrecedenceFirst()) {
            // Return the first valid rule starting from the beginning of the list
            for (Iterator<RuleMetaData> itr = rules.iterator(); itr.hasNext(); ) {
                RuleMetaData rule = itr.next();
                if (check(className, obj, rule, executionRealmCheck, ruleInfo)) {
                    if (output == null)
                        output = new ArrayList<RuleMetaData>();
                    output.add(rule);
                    break;
                }
            }
        } else {
            // Return all valid rules
            for (Iterator<RuleMetaData> itr = rules.iterator(); itr.hasNext(); ) {
                RuleMetaData rule = itr.next();
                if (check(className, obj, rule, executionRealmCheck, ruleInfo)) {
                    if (output == null)
                        output = new LinkedList<RuleMetaData>();
                    output.add(rule);
                }
            }
        }
        return output;
    }
    return rules;
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 17 with RuleMetaData

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

the class ScriptLifecycleHandler method invokeScript.

/**
 * Checks if rule is still applicable based on bean values and invokes script
 *
 * @param invocationMethod name of the called method on the handler
 * @throws ApplicationExceptions
 * @throws FrameworkException
 */
private void invokeScript(String invocationMethod) throws ApplicationExceptions, FrameworkException {
    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(targetBean.getClass().getName(), targetBean, rules, true);
        // invoke script for each applicableRule
        if (applicableRules != null) {
            for (RuleMetaData rule : applicableRules) {
                invoke(rule);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 18 with RuleMetaData

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

the class DataSecurity method getRules.

/**
 * Gets the data security rules for a class
 *
 * @param targetClassName Name of the class the rule applies to
 * @param targetObject    Object the rules applies to
 * @param ruleName        Name of the rule
 * @return List of rules that apply
 */
private static List<RuleMetaData> getRules(String targetClassName, Object targetObject, String ruleName) throws ApplicationExceptions, FrameworkException {
    if (targetClassName != null) {
        // NOTE: We can probably go up the inheritance hierarchy of the targetObject to determine the appropriate rules
        Map<String, List<RuleMetaData>> map = MetaDataRepository.instance().getPropertyRuleMap(targetClassName, ruleName);
        if (map != null) {
            IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleName);
            Map<String, List<RuleMetaData>> newMap = null;
            for (Map.Entry<String, List<RuleMetaData>> me : map.entrySet()) {
                List<RuleMetaData> rules = ruleHelper.getApplicableRules(targetClassName, targetObject, me.getValue(), true);
                if (rules != null && rules.size() > 0) {
                    if (newMap == null)
                        newMap = new LinkedHashMap<String, List<RuleMetaData>>();
                    newMap.put(me.getKey(), rules);
                }
            }
            map = newMap;
            return map.get(null);
        }
    }
    return Collections.EMPTY_LIST;
}
Also used : IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) List(java.util.List) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with RuleMetaData

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

the class RuleActor method getPrimaryKeyLabel.

/**
 * A convenience method to return the label of the key fields for the input domain class, to be used in error messages.
 * For a composite key, the labels will be comma-separated.
 * It'll check for the label in the following order
 * - First looks up the 'primary-key' rule for the input class. A null will be returned if the key is not defined.
 * - Lookup a 'label' rule for each key field. The field name will be returned if the label is not defined.
 *
 * @param targetClassName The target Class.
 * @param targetObject    The target Object.
 * @return label of the key fields for the input domain class.
 */
protected String getPrimaryKeyLabel(String targetClassName, Object targetObject) {
    String label = null;
    try {
        String[] primaryKey = null;
        // Search for the first available class-level primary-key rule
        Map<String, List<RuleMetaData>> pkMap = getPropertyRuleMap(targetClassName, targetObject, "primary-key");
        if (pkMap != null && pkMap.containsKey(null)) {
            List<RuleMetaData> rules = pkMap.get(null);
            if (rules != null && rules.size() > 0) {
                RuleMetaData rule = rules.get(0);
                String value = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
                if (value != null) {
                    primaryKey = value.split(",");
                }
            }
        }
        if (primaryKey != null) {
            StringBuilder primaryKeyLabel = new StringBuilder();
            for (int i = 0; i < primaryKey.length; i++) {
                if (i > 0) {
                    primaryKeyLabel.append(',');
                }
                primaryKeyLabel.append(getPropertyLabel(targetObject, primaryKey[i]));
            }
            label = primaryKeyLabel.toString();
        }
    } catch (Exception e) {
    // do nothing
    }
    return label;
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) FrameworkException(org.jaffa.exceptions.FrameworkException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApplicationException(org.jaffa.exceptions.ApplicationException)

Example 20 with RuleMetaData

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

the class RuleActor method getPropertyRuleMap.

/**
 * Returns a Map containing a List of RuleMetaData instances per propertyName for the className+ruleName combination.
 * The class-level RuleMetaData instances defined for the className+ruleName combination will be added to the Map with propertyName null.
 * The className is obtained from the targetClass.
 *
 * @param targetClassName The target Class.
 * @param targetObject    The target Object.
 * @param ruleName        the rule to search for.
 * @return a Map containing a List of RuleMetaData instances per propertyName for the className+ruleName combination.
 * @throws ApplicationExceptions if any application exception occurs.
 * @throws FrameworkException    if any internal error occurs.
 */
protected Map<String, List<RuleMetaData>> getPropertyRuleMap(String targetClassName, Object targetObject, String ruleName) throws ApplicationExceptions, FrameworkException {
    if (targetClassName != null) {
        Map<String, List<RuleMetaData>> map = MetaDataRepository.instance().getPropertyRuleMap(targetClassName, ruleName);
        if (map != null) {
            IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleName);
            Map<String, List<RuleMetaData>> newMap = null;
            for (Map.Entry<String, List<RuleMetaData>> me : map.entrySet()) {
                List<RuleMetaData> rules = ruleHelper.getApplicableRules(targetClassName, targetObject, me.getValue(), true);
                if (rules != null && rules.size() > 0) {
                    if (newMap == null) {
                        newMap = new LinkedHashMap<>();
                    }
                    newMap.put(me.getKey(), rules);
                }
            }
            map = newMap;
        }
        return map;
    } else {
        return null;
    }
}
Also used : IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) 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