Search in sources :

Example 1 with ClassMetaData

use of org.jaffa.rules.meta.ClassMetaData 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 2 with ClassMetaData

use of org.jaffa.rules.meta.ClassMetaData 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)

Example 3 with ClassMetaData

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

the class TestConfig method fakeModelMeta.

@Bean
public ClassMetaData fakeModelMeta() {
    ClassMetaData data = classMetaData(FakeModel.class);
    data.property("field1").label("[Field1]").mandatory();
    data.property("field2").label("[Field2]").initialize("123").mandatory();
    data.setExecutionRealm("business");
    data.setVariation("DEF");
    data.register();
    return data;
}
Also used : ClassMetaData(org.jaffa.rules.meta.ClassMetaData) Bean(org.springframework.context.annotation.Bean)

Example 4 with ClassMetaData

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

the class PersistentConfigValidatorTest method setup.

/**
 * Common setup
 *
 * @throws Exception
 */
@Before
public void setup() throws Exception {
    // create app context, do not need to include PersistentContext directly
    AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(JaffaRulesConfig.class, TestModelPersistentConfig.class);
    assertNotNull(appContext);
    new StaticContext().setApplicationContext(appContext);
    // get validator factory bean from combined app context
    ValidatorFactory target = appContext.getBean(ValidatorFactory.class);
    assertNotNull(target);
    Realm realm = new Realm("realm");
    realm.setSource("Test");
    realm.regex(TestModelPersistent.class.getName());
    realm.register();
    ClassMetaData classMetaData = new ClassMetaData();
    classMetaData.setName(TestModelPersistent.class.getName());
    classMetaData.setVariation("DEF");
    classMetaData.setExecutionRealm(realm.getName());
    classMetaData.register();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ValidatorFactory(org.jaffa.rules.fieldvalidators.ValidatorFactory) Realm(org.jaffa.rules.realm.Realm) ClassMetaData(org.jaffa.rules.meta.ClassMetaData) StaticContext(org.jaffa.beans.factory.config.StaticContext) Before(org.junit.Before)

Example 5 with ClassMetaData

use of org.jaffa.rules.meta.ClassMetaData 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)

Aggregations

ClassMetaData (org.jaffa.rules.meta.ClassMetaData)7 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 PropertyMetaData (org.jaffa.rules.meta.PropertyMetaData)2 Realm (org.jaffa.rules.realm.Realm)2 Before (org.junit.Before)2 Bean (org.springframework.context.annotation.Bean)2 ArrayList (java.util.ArrayList)1 StaticContext (org.jaffa.beans.factory.config.StaticContext)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 TestConfig (org.jaffa.rules.TestConfig)1 TestModel (org.jaffa.rules.TestModel)1 ValidatorFactory (org.jaffa.rules.fieldvalidators.ValidatorFactory)1 BeansException (org.springframework.beans.BeansException)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1