Search in sources :

Example 6 with RuleMetaData

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

the class RuleActor method getObjectLabel.

/**
 * A convenience method to return the object label to be used in error messages.
 * It'll check for the label in the following order
 * - Lookup a 'label' rule for the input class
 * - Return the class name
 *
 * @param targetClassName The target Class.
 * @param targetObject    The target Object.
 * @return label for the class.
 */
protected String getObjectLabel(String targetClassName, Object targetObject) {
    String label = null;
    try {
        // Search for the first available class-level label rule
        Map<String, List<RuleMetaData>> pkMap = getPropertyRuleMap(targetClassName, targetObject, "label");
        if (pkMap != null && pkMap.containsKey(null)) {
            List<RuleMetaData> rules = pkMap.get(null);
            if (rules != null && rules.size() > 0) {
                RuleMetaData rule = rules.get(0);
                label = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
            }
        }
    } catch (Exception e) {
    // do nothing
    }
    // See if this can be resolved with a persistence meta class
    if (label == null) {
        try {
            label = PersistentHelper.getLabelToken(targetClassName);
        } catch (Exception e) {
        // do nothing
        }
    }
    // Finally default to the simple class name
    if (label == null) {
        label = StringHelper.getShortClassName(targetClassName);
    }
    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 7 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 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, 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) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 8 with RuleMetaData

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

the class RuleActor method getPropertyLabel.

/**
 * A convenience method to return the property label to be used in error messages.
 * It'll check for the label in the following order
 * - Lookup a 'label' rule for this property
 * - Return the property name
 *
 * @param targetObject       The target Object.
 * @param targetPropertyName The property for which the label is to be found.
 * @return label for the property.
 */
protected String getPropertyLabel(Object targetObject, String targetPropertyName) {
    String label = null;
    String targetClassName = getActualClassName(targetObject.getClass());
    try {
        if (targetObject instanceof FlexBean) {
            if (((FlexBean) targetObject).getDynaClass().getDynaProperty(targetPropertyName) == null && ((FlexBean) targetObject).getPersistentObject() != null) {
                targetObject = ((FlexBean) targetObject).getPersistentObject();
                targetClassName = targetObject.getClass().getName();
            }
        } else if (targetObject instanceof IFlexFields) {
            if (((IFlexFields) targetObject).getFlexBean() != null && ((IFlexFields) targetObject).getFlexBean().getDynaClass().getDynaProperty(targetPropertyName) != null) {
                targetObject = ((IFlexFields) targetObject).getFlexBean();
                targetClassName = ((FlexBean) targetObject).getDynaClass().getName();
            }
        }
        // Search for the first available property-level label rule
        Map<String, List<RuleMetaData>> pkMap = getPropertyRuleMap(targetClassName, targetObject, "label");
        if (pkMap != null && pkMap.containsKey(targetPropertyName)) {
            List<RuleMetaData> rules = pkMap.get(targetPropertyName);
            if (rules != null && rules.size() > 0) {
                RuleMetaData rule = rules.get(0);
                label = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
            }
        }
    } catch (Exception e) {
    // do nothing
    }
    if (label == null) {
        label = targetPropertyName;
    }
    return label;
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) IFlexFields(org.jaffa.flexfields.IFlexFields) FlexBean(org.jaffa.flexfields.FlexBean) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) FrameworkException(org.jaffa.exceptions.FrameworkException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApplicationException(org.jaffa.exceptions.ApplicationException)

Example 9 with RuleMetaData

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

the class CandidateKeyValidator method validateProperty.

/**
 * {@inheritDoc}
 */
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
    String targetClassName = getActualClassName(targetObject.getClass());
    IPersistent persistentTargetObject = null;
    try {
        // Use the associated persistentObject for a FlexBean instance
        if (targetObject instanceof FlexBean && ((FlexBean) targetObject).getPersistentObject() != null) {
            persistentTargetObject = ((FlexBean) targetObject).getPersistentObject();
            targetClassName = persistentTargetObject.getClass().getName();
        }
        // This rules makes sense on IPersistent instances only. Bail out on all other instances
        if (!(targetObject instanceof IPersistent)) {
            return;
        } else if (persistentTargetObject == null) {
            persistentTargetObject = (IPersistent) targetObject;
        }
        // Obtain a Map containing key fields and corresponding values
        Map<String, Object> keyValueMap = getKeyValueMap(targetClassName, (IPersistent) targetObject);
        // Create a criteria such that the current object is not included in the search for candidate-key violations
        AtomicCriteria criteriaToDiscardCurrentObject = createCriteriaToDiscardCurrentObject(persistentTargetObject, keyValueMap);
        // Invoke each rule
        for (RuleMetaData rule : rules) {
            invoke(targetClassName, persistentTargetObject, uow, rule, criteriaToDiscardCurrentObject, keyValueMap);
        }
    } catch (ApplicationExceptions e) {
        if (log.isDebugEnabled()) {
            log.debug("Error in validateProperty method for object: " + targetClassName, e);
        }
        throw new JaffaRulesFrameworkException("Error in validateProperty method for object: " + targetClassName, null, e);
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) IPersistent(org.jaffa.persistence.IPersistent) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) FlexBean(org.jaffa.flexfields.FlexBean) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException)

Example 10 with RuleMetaData

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

the class CaseTypeValidator 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 (targetPropertyValue != null && targetPropertyValue instanceof String) {
        if (log.isDebugEnabled()) {
            log.debug("Applying " + rule + " on " + targetPropertyValue);
        }
        String caseType = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
        Object value = "upper".equals(caseType) ? ((String) targetPropertyValue).toUpperCase() : ("lower".equals(caseType) ? ((String) targetPropertyValue).toLowerCase() : targetPropertyValue);
        if (!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 + '\'');
            }
        }
    } else {
        if (targetPropertyValue != null && log.isDebugEnabled()) {
            log.debug(targetPropertyName + " not a String and hence " + rule + " cannot be applied");
        }
    }
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) FrameworkException(org.jaffa.exceptions.FrameworkException) RulesEngineException(org.jaffa.rules.RulesEngineException) ApplicationException(org.jaffa.exceptions.ApplicationException)

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