Search in sources :

Example 11 with RuleMetaData

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

the class CommentValidator method validateProperty.

/**
 * {@inheritDoc}
 */
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules) throws ApplicationException, FrameworkException {
    RuleMetaData rule = rules.get(0);
    if (log.isDebugEnabled()) {
        log.debug("Applying " + rule + " on " + targetPropertyValue);
    }
    Object initialValue = returnInitialValue(targetObject, targetPropertyName);
    String commentStyle = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
    Object value = null;
    if ("fifo".equals(commentStyle)) {
        value = addCommentWithStamp((String) initialValue, (String) targetPropertyValue, false);
    } else if ("lifo".equals(commentStyle)) {
        value = addCommentWithStamp((String) initialValue, (String) targetPropertyValue, true);
    } else {
        value = targetPropertyValue;
    }
    if (value != null && !value.equals(targetPropertyValue)) {
        try {
            BeanHelper.setField(targetObject, targetPropertyName, value);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Could not set the field: " + targetObject.getClass().getName() + "." + targetPropertyName, e);
            }
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("Changed value to '" + value + '\'');
        }
    }
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) FrameworkException(org.jaffa.exceptions.FrameworkException) RulesEngineException(org.jaffa.rules.RulesEngineException) ApplicationException(org.jaffa.exceptions.ApplicationException)

Example 12 with RuleMetaData

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

the class InListValidator method validateProperty.

/**
 * {@inheritDoc}
 */
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules) throws ApplicationException, FrameworkException {
    if (targetPropertyValue != null) {
        for (RuleMetaData rule : rules) {
            if (log.isDebugEnabled()) {
                log.debug("Applying " + rule + " on " + targetPropertyValue);
            }
            Map<String, String> inListValues = MetaDataIntrospector.getInListValues(rule);
            // Iterate through the valid Strings
            // convert the String to the appropriate datatype and then perform the comparison
            boolean isValueValid = false;
            for (String validString : inListValues.keySet()) {
                Object validValue = DataTypeMapper.instance().map(validString, targetPropertyValue.getClass());
                if (targetPropertyValue.equals(validValue)) {
                    isValueValid = true;
                    break;
                }
            }
            if (!isValueValid) {
                String listParam = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
                String[] listValues = listParam.split(",");
                String errorList = "";
                if (listValues != null) {
                    for (int i = 0; i < listValues.length; i++) {
                        if (i > 0) {
                            errorList = errorList + ", ";
                        }
                        if (listValues[i].indexOf("=") > 0) {
                            errorList = errorList + MessageHelper.replaceTokens(listValues[i].split("=")[1]);
                        } else {
                            errorList = errorList + MessageHelper.replaceTokens(listValues[i]);
                        }
                    }
                }
                if (log.isDebugEnabled()) {
                    log.debug("The value '" + targetPropertyValue + "' not found in the List '" + errorList + "'");
                }
                throw wrapException(InListException.class, targetObject, targetPropertyName, rule);
            }
        }
    }
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 13 with RuleMetaData

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

the class RuleValidatorFactory 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 org.jaffa.exceptions.ApplicationExceptions if any application exception occurs.
 * @throws org.jaffa.exceptions.FrameworkException    if any internal error occurs.
 */
protected Map<String, List<RuleMetaData>> getPropertyRuleMap(String targetClassName, 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, 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) ArrayList(java.util.ArrayList) List(java.util.List) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with RuleMetaData

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

the class RuleValidatorTest method testSetRuleMap.

/**
 * Bogus entries should be trimmed when setting the rule map.
 */
@Test
public void testSetRuleMap() {
    RuleValidator target = new TestValidator();
    Map<String, List<RuleMetaData>> ruleMap = new HashMap<>();
    List<RuleMetaData> valid = new ArrayList<>();
    RuleMetaData rule = new RuleMetaData();
    valid.add(rule);
    List<RuleMetaData> invalid = new ArrayList<>();
    ruleMap.put(null, valid);
    ruleMap.put("", valid);
    ruleMap.put("a", null);
    ruleMap.put("b", invalid);
    ruleMap.put("c", valid);
    target.setRuleMap(ruleMap);
    Map<String, List<RuleMetaData>> actual = target.getRuleMap();
    assertEquals(3, actual.size());
    assertEquals(rule, actual.get("c").get(0));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) Test(org.junit.Test)

Example 15 with RuleMetaData

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

the class CxfFunctionGuardInterceptorTest method updateRules.

/**
 * Update the rules list and set the name of the rule in the list - Test is the expected name
 *
 * @param name       Name of the rule for the test
 * @param method     Name of the method the rule applies to
 * @param repository Rule repository
 * @param ruleHelper Rule helper
 */
private void updateRules(String name, String method, MetaDataRepository repository, IRuleHelper ruleHelper) throws FrameworkException, ApplicationExceptions {
    Map<String, List<RuleMetaData>> rules = new HashMap<>();
    List<RuleMetaData> ruleMetaDatas = new ArrayList<>();
    RuleMetaData rule = new RuleMetaData();
    rule.addParameter("method", method);
    rule.addParameter("name", name);
    ruleMetaDatas.add(rule);
    rules.put(null, ruleMetaDatas);
    when(repository.getPropertyRuleMap(Mockito.anyString(), Mockito.anyString())).thenReturn(rules);
    when(ruleHelper.getApplicableRules(Mockito.anyString(), Mockito.anyObject(), Mockito.anyListOf(RuleMetaData.class), Mockito.anyBoolean())).thenReturn(ruleMetaDatas);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) 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