Search in sources :

Example 1 with RuleMetaData

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

the class CxfFunctionGuardInterceptor 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 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.
 */
private Map<String, List<RuleMetaData>> getPropertyRuleMap(String targetClassName, String ruleName) throws ApplicationExceptions, FrameworkException {
    if (targetClassName == null) {
        return null;
    }
    Map<String, List<RuleMetaData>> map = MetaDataRepository.instance().getPropertyRuleMap(targetClassName, ruleName);
    if (map == null) {
        return 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, null, me.getValue(), true);
        if (rules != null && rules.size() > 0) {
            if (newMap == null) {
                newMap = new LinkedHashMap<>();
            }
            newMap.put(me.getKey(), rules);
        }
    }
    return newMap;
}
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)

Example 2 with RuleMetaData

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

the class RealmRepository method getInheritableRules.

/**
 * Iterates through the input rules, returning only those that can be inherited by the realm for the targetClassName.
 * All rules will be returned if no realm is defined for the targetClassName.
 *
 * @param targetClassName the class which is inheriting the rules.
 * @param rules           the rules to be inherited.
 * @return inheritable rules.
 */
public List<RuleMetaData> getInheritableRules(String targetClassName, List<RuleMetaData> rules) {
    if (log.isDebugEnabled())
        log.debug("Checking inheritableRules for class '" + targetClassName + "' from " + rules);
    List<RuleMetaData> output = rules;
    if (rules != null && rules.size() > 0) {
        String targetRealmName = find(targetClassName);
        if (targetRealmName != null) {
            output = null;
            List<String> inheritanceRulesToInclude = getInheritanceRulesToInclude(targetRealmName);
            List<String> inheritanceRulesToExclude = getInheritanceRulesToExclude(targetRealmName);
            for (RuleMetaData rule : rules) {
                if (isInheritable(targetClassName, targetRealmName, inheritanceRulesToInclude, inheritanceRulesToExclude, rule)) {
                    if (output == null)
                        output = new LinkedList<RuleMetaData>();
                    output.add(rule);
                }
            }
        }
    }
    if (log.isDebugEnabled())
        log.debug("InheritableRules for class '" + targetClassName + "' are " + output);
    return output;
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 3 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.
 * The precedence for the rule is used to determine the rule(s) to be passed back.
 * @param className the target class.
 * @param rules the input list of rules.
 * @return the rules to be applied.
 * @param executionRealmCheck decides if executionRealm checks are to be performed.
 * @throws ApplicationExceptions if any application exception occurs.
 * @throws FrameworkException if any internal error occurs.
 */
public List<RuleMetaData> getApplicableRules(String className, 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, 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, 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, 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 4 with RuleMetaData

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

the class MetaDataReader method classMetaDataToDto.

/**
 * Convert a list of ClassMetaData objects into an array of ClassMetaDataDto
 * objects
 */
private static ClassMetaDataDto[] classMetaDataToDto(List<ClassMetaData> classMetaData, Boolean returnProperties, Boolean aggregate) {
    if (classMetaData == null)
        return null;
    List<ClassMetaDataDto> returnCmd = new LinkedList<ClassMetaDataDto>();
    for (ClassMetaData metaData : classMetaData) {
        ClassMetaDataDto dto = new ClassMetaDataDto();
        dto.setName(metaData.getName());
        dto.setSourceFileName(metaData.getSource());
        dto.setCondition(metaData.getCondition());
        dto.setLanguage(metaData.getLanguage());
        dto.setVariation(metaData.getVariation());
        dto.setExecutionRealm(metaData.getExecutionRealm());
        dto.setExtendsClass(metaData.getExtendsClass());
        // Load rules into dto
        List<RuleMetaDataDto> ruleMetaDataDtos = new LinkedList<RuleMetaDataDto>();
        List<RuleMetaData> rules = metaData.getRules();
        if (rules != null) {
            for (RuleMetaData rmd : rules) {
                RuleMetaDataDto ruleMetaDataDto = new RuleMetaDataDto();
                ruleMetaDataDto.setRuleName(rmd.getName());
                ruleMetaDataDto.setVariation(rmd.getVariation());
                ruleMetaDataDto.setVariationArray(rmd.getVariationArray());
                ruleMetaDataDto.setParameters(rmd.getParameters());
                ruleMetaDataDtos.add(ruleMetaDataDto);
            }
        }
        dto.setRules(ruleMetaDataDtos);
        // Load properties into dto
        if (returnProperties != null && returnProperties) {
            List<PropertyMetaDataDto> propertyMetaDataDtos = new LinkedList<PropertyMetaDataDto>();
            List<PropertyMetaData> props = metaData.getProperties();
            if (props != null) {
                for (PropertyMetaData pmd : props) {
                    PropertyMetaDataDto propertyMetaDataDto = new PropertyMetaDataDto();
                    propertyMetaDataDto.setPropertyName(pmd.getName());
                    propertyMetaDataDto.setCondition(pmd.getCondition());
                    propertyMetaDataDto.setLanguage(pmd.getLanguage());
                    propertyMetaDataDto.setVariation(pmd.getVariation());
                    propertyMetaDataDto.setExtendsClass(pmd.getExtendsClass());
                    propertyMetaDataDto.setExtendsProperty(pmd.getExtendsProperty());
                    // Load rules into dto
                    List<RuleMetaDataDto> propertyRuleMetaDataDtos = new LinkedList<RuleMetaDataDto>();
                    List<RuleMetaData> propertyRules = pmd.getRules(metaData.getName());
                    if (propertyRules != null) {
                        for (RuleMetaData rmd : propertyRules) {
                            RuleMetaDataDto ruleMetaDataDto = new RuleMetaDataDto();
                            ruleMetaDataDto.setRuleName(rmd.getName());
                            ruleMetaDataDto.setVariation(rmd.getVariation());
                            ruleMetaDataDto.setVariationArray(rmd.getVariationArray());
                            ruleMetaDataDto.setParameters(rmd.getParameters());
                            propertyRuleMetaDataDtos.add(ruleMetaDataDto);
                        }
                    }
                    propertyMetaDataDto.setRules(propertyRuleMetaDataDtos);
                    propertyMetaDataDtos.add(propertyMetaDataDto);
                }
            }
            dto.setProperties(propertyMetaDataDtos);
        }
        returnCmd.add(dto);
    }
    return (ClassMetaDataDto[]) returnCmd.toArray(new ClassMetaDataDto[returnCmd.size()]);
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) PropertyMetaData(org.jaffa.rules.meta.PropertyMetaData) LinkedList(java.util.LinkedList) ClassMetaData(org.jaffa.rules.meta.ClassMetaData)

Example 5 with RuleMetaData

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

the class MetaDataReader method getFlexClass.

/**
 *  Return information about all the classes that are configured to support flex fields
 */
public static List<FlexClassMetaData> getFlexClass() {
    List<FlexClassMetaData> flexClassMetaData = new LinkedList<FlexClassMetaData>();
    // Get an array of all the classes that support flex fields
    String[] classNames = MetaDataRepository.instance().getClassNamesByRuleName("flex-fields");
    String variation = VariationContext.getVariation();
    if (classNames != null) {
        for (String className : classNames) {
            if (log.isDebugEnabled()) {
                log.debug("Checking flex for: " + className);
            }
            RuleMetaData domainInfo = findRule(className, null, null, "domain-info");
            if (domainInfo != null) {
                Map<String, List<RuleMetaData>> ruleMap = MetaDataRepository.instance().getPropertyRuleMap(className, "flex-fields");
                List<RuleMetaData> rules = ruleMap != null ? ruleMap.get(null) : null;
                List<String> processedClasses = new LinkedList<String>();
                if (rules != null) {
                    for (RuleMetaData rule : rules) {
                        if (log.isDebugEnabled()) {
                            log.debug("Checking flex for class: " + className + " rule: " + rule.getName());
                        }
                        // Class flex-fields should only be defined in core aop folders
                        if (rule.getSource().toLowerCase().indexOf(AOP_CORE_PATH) > 0) {
                            if (processedClasses.indexOf(rule.getParameter("source")) >= 0) {
                                log.error("Flex-fields rules have been defined, in core, multiple times for the same source: " + rule.getParameter("source"));
                                continue;
                            }
                            processedClasses.add(rule.getParameter("source"));
                            // Get class meta data by flex class name
                            List<ClassMetaData> cmd = MetaDataRepository.instance().getClassMetaDataListByClassName(rule.getParameter("source"));
                            Boolean hasCustomerDefinition = false;
                            Boolean hasCoreDefinition = false;
                            String coreSourceFile = "";
                            if (cmd != null) {
                                for (ClassMetaData flexMetaData : cmd) {
                                    String flexSourcePath = flexMetaData.getSource();
                                    if (flexSourcePath.toLowerCase().indexOf(AOP_VARIATION_PATH + variation.toLowerCase() + "/") > 0) {
                                        hasCustomerDefinition = true;
                                    }
                                    if (flexSourcePath.toLowerCase().indexOf(AOP_CORE_PATH) > 0) {
                                        hasCoreDefinition = true;
                                        coreSourceFile = flexSourcePath;
                                    }
                                }
                            }
                            if (!hasCoreDefinition) {
                                log.error("Core AOP definition not found for:" + rule.getParameter("source") + ". This should include value for domain-info rule.");
                            } else if (!hasCustomerDefinition) {
                                // define stub for customer-variation
                                FlexClassMetaData fcmd = new FlexClassMetaData();
                                String flexClassName = rule.getParameter("source");
                                RuleMetaData flexDomainInfo = findRule(flexClassName, null, null, "domain-info");
                                if (flexDomainInfo == null) {
                                    log.error("No domain-info defined for: " + flexClassName);
                                    continue;
                                }
                                fcmd.setDomainClass(className);
                                RuleMetaData labelRule = findRule(className, null, null, "label");
                                if (labelRule != null) {
                                    fcmd.setDomainLabel(MessageHelper.replaceTokens(labelRule.getParameter(RuleMetaData.PARAMETER_VALUE)));
                                    fcmd.setDomainLabelToken(MessageHelper.removeTokenMarkers(labelRule.getParameter(RuleMetaData.PARAMETER_VALUE)));
                                }
                                String dummyPath = coreSourceFile.replace(AOP_CORE_PATH, (AOP_VARIATION_PATH + variation.toLowerCase() + "/"));
                                int lastIndex = dummyPath.lastIndexOf("/");
                                if (flexDomainInfo.getParameter("name") != null) {
                                    dummyPath = dummyPath.substring(0, (lastIndex + 1)) + flexDomainInfo.getParameter("name").replace("$", "") + "-aop.xml";
                                } else {
                                    log.error("Name not found for class:" + flexClassName);
                                    continue;
                                }
                                fcmd.setFlexSourceFile(dummyPath);
                                fcmd.setDomainName(domainInfo.getParameter("name"));
                                fcmd.setFlexClass(flexClassName);
                                fcmd.setFlexName(flexDomainInfo.getParameter("name"));
                                RuleMetaData flexLabelRule = findRule(flexClassName, null, null, "label");
                                if (flexLabelRule != null) {
                                    fcmd.setFlexLabel(MessageHelper.replaceTokens(flexLabelRule.getParameter(RuleMetaData.PARAMETER_VALUE)));
                                    fcmd.setFlexLabelToken(MessageHelper.removeTokenMarkers(flexLabelRule.getParameter(RuleMetaData.PARAMETER_VALUE)));
                                }
                                fcmd.setCondition(rule.getParameter("condition"));
                                flexClassMetaData.add(fcmd);
                            } else if (hasCustomerDefinition) {
                                for (ClassMetaData flexMetaData : cmd) {
                                    String flexSourcePath = flexMetaData.getSource();
                                    if (flexSourcePath.toLowerCase().indexOf(AOP_VARIATION_PATH + variation.toLowerCase() + "/") > 0) {
                                        Map<String, String> parameters = rule.getParameters();
                                        if (parameters != null) {
                                            FlexClassMetaData fcmd = new FlexClassMetaData();
                                            String flexClassName = rule.getParameter("source");
                                            RuleMetaData flexDomainInfo = findRule(flexClassName, null, null, "domain-info");
                                            fcmd.setDomainClass(className);
                                            RuleMetaData labelRule = findRule(className, null, null, "label");
                                            if (labelRule != null) {
                                                fcmd.setDomainLabel(MessageHelper.replaceTokens(labelRule.getParameter(RuleMetaData.PARAMETER_VALUE)));
                                                fcmd.setDomainLabelToken(MessageHelper.removeTokenMarkers(labelRule.getParameter(RuleMetaData.PARAMETER_VALUE)));
                                            }
                                            fcmd.setFlexSourceFile(flexSourcePath);
                                            fcmd.setDomainName(domainInfo.getParameter("name"));
                                            RuleMetaData flexLabelRule = findRule(flexClassName, null, null, "label");
                                            if (flexLabelRule != null) {
                                                fcmd.setFlexLabel(MessageHelper.replaceTokens(flexLabelRule.getParameter(RuleMetaData.PARAMETER_VALUE)));
                                                fcmd.setFlexLabelToken(MessageHelper.removeTokenMarkers(flexLabelRule.getParameter(RuleMetaData.PARAMETER_VALUE)));
                                            }
                                            fcmd.setFlexClass(flexClassName);
                                            fcmd.setFlexName(flexDomainInfo.getParameter("name"));
                                            fcmd.setCondition(rule.getParameter("condition"));
                                            flexClassMetaData.add(fcmd);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return flexClassMetaData;
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) ClassMetaData(org.jaffa.rules.meta.ClassMetaData)

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