Search in sources :

Example 21 with RuleMetaData

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

the class CandidateKeyValidator method getKeyValueMap.

/**
 * Obtain a Map containing key fields and corresponding values.
 */
private Map<String, Object> getKeyValueMap(String targetClassName, IPersistent targetObject) throws ApplicationExceptions, FrameworkException {
    // Search for the first available class-level primary-key rule
    String[] keys = null;
    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);
            keys = rule.getParameter(RuleMetaData.PARAMETER_VALUE).split(",");
        }
    }
    // Search for corresponding DomainMeta class, if required
    if (keys == null || keys.length == 0) {
        try {
            FieldMetaData[] keyFields = PersistentHelper.getKeyFields(targetObject.getClass().getName());
            if (keyFields != null) {
                keys = new String[keyFields.length];
                for (int i = 0; i < keyFields.length; i++) keys[i] = keyFields[i].getName();
            }
        } catch (Exception e) {
        // do nothing
        }
    }
    // Create the keyValueMap
    if (keys != null && keys.length > 0) {
        Map<String, Object> keyValueMap = new LinkedHashMap<String, Object>();
        for (String key : keys) {
            Object value = null;
            try {
                value = BeanHelper.getField(targetObject, key);
            } catch (NoSuchMethodException e) {
                if (log.isDebugEnabled()) {
                    log.debug("Could not get the field: " + targetObject.getClass().getName() + "." + key, e);
                }
                throw new JaffaRulesFrameworkException(targetObject.getClass().getName() + "." + key, null, e);
            }
            keyValueMap.put(key, value);
        }
        return keyValueMap;
    } else {
        return null;
    }
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) FrameworkException(org.jaffa.exceptions.FrameworkException) DuplicateCandidateKeyException(org.jaffa.exceptions.DuplicateCandidateKeyException) ApplicationException(org.jaffa.exceptions.ApplicationException) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) FieldMetaData(org.jaffa.metadata.FieldMetaData)

Example 22 with RuleMetaData

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

the class GenericForeignKeyValidator method validateProperty.

/**
 * {@inheritDoc}
 */
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
    if (targetPropertyValue != null) {
        for (RuleMetaData rule : rules) {
            if (log.isDebugEnabled()) {
                log.debug("Applying " + rule + " on " + targetPropertyValue);
            }
            String tableName = rule.getParameter(PARAMETER_TABLE_NAME);
            String fieldName = rule.getParameter(PARAMETER_FIELD_NAME);
            String domainClassName = rule.getParameter(PARAMETER_DOMAIN_CLASS_NAME);
            String fieldNameForTable = rule.getParameter(PARAMETER_FIELD_NAME_FOR_TABLE);
            String fieldNameForField = rule.getParameter(PARAMETER_FIELD_NAME_FOR_FIELD);
            String fieldNameForValue = rule.getParameter(PARAMETER_FIELD_NAME_FOR_VALUE);
            // Transform the value to the data type of the 'LegalValue' field of the Generic Foreign Key class.
            Class valueClass = determineValueClass(domainClassName, fieldNameForValue);
            Object value = DataTypeMapper.instance().map(targetPropertyValue, valueClass);
            Criteria c = new Criteria();
            c.setTable(domainClassName);
            c.addCriteria(fieldNameForTable, tableName);
            c.addCriteria(fieldNameForField, fieldName);
            c.addCriteria(fieldNameForValue, value);
            Collection col = uow.query(c);
            if (!col.iterator().hasNext()) {
                // Invalid value. Display the list of valid values in the error message
                StringBuilder validValues = new StringBuilder();
                c = new Criteria();
                c.setTable(domainClassName);
                c.addCriteria(fieldNameForTable, tableName);
                c.addCriteria(fieldNameForField, fieldName);
                c.addOrderBy(fieldNameForValue);
                for (Object o : uow.query(c)) {
                    try {
                        Object legalValue = BeanHelper.getField(o, fieldNameForValue);
                        if (validValues.length() > 0)
                            validValues.append(',');
                        validValues.append(legalValue);
                    } catch (Exception e) {
                    // do nothing
                    }
                }
                if (log.isDebugEnabled()) {
                    log.debug("Generic ForeignKey validation failed for the value '" + value + "' against the table/field - " + tableName + '/' + fieldName + ". Valid values are " + validValues.toString());
                }
                String label = getPropertyLabel(targetObject, targetPropertyName);
                Object[] arguments = getErrorArgumentArray(targetObject, rule);
                if (arguments == null) {
                    arguments = new Object[] { tableName, fieldName, validValues.toString() };
                }
                throw wrapException(new InvalidGenericForeignKeyException(label, arguments), targetObject, rule);
            }
        }
    }
}
Also used : InvalidGenericForeignKeyException(org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException) Collection(java.util.Collection) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) Criteria(org.jaffa.persistence.Criteria) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) FrameworkException(org.jaffa.exceptions.FrameworkException) InvalidGenericForeignKeyException(org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException) ApplicationException(org.jaffa.exceptions.ApplicationException)

Example 23 with RuleMetaData

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

the class KeyValidator method validate.

/**
 * {@inheritDoc}
 */
@Override
public void validate(T targetObject) throws ApplicationException, FrameworkException {
    UOW uow = getUOW(targetObject);
    if (uow == null) {
        if (log.isDebugEnabled()) {
            log.debug(getName() + " check not performed since UOW is not available on the targetObject " + targetObject);
        }
        return;
    }
    // No need to perform validations for an object that has not been modified.
    if (isModified(targetObject)) {
        for (Map.Entry<String, List<RuleMetaData>> entry : getRuleMap().entrySet()) {
            String targetPropertyName = entry.getKey();
            List<RuleMetaData> rules = entry.getValue();
            // No need to perform validations for a property that has not been modified.
            if (isModified(targetObject, targetPropertyName)) {
                List<RuleMetaData> applicableRules = trimInapplicableRules(targetObject, rules);
                if (applicableRules != null && !applicableRules.isEmpty()) {
                    validateProperty(targetObject, targetPropertyName, applicableRules, uow);
                }
            }
        }
    }
}
Also used : List(java.util.List) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) UOW(org.jaffa.persistence.UOW) Map(java.util.Map)

Example 24 with RuleMetaData

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

the class MaxLengthValidator 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);
            }
            if (rule.getName().equalsIgnoreCase(getName())) {
                // Determine the intSize and fracSize
                Integer intSize = null;
                Integer fracSize = null;
                try {
                    String[] sizes = rule.getParameter(RuleMetaData.PARAMETER_VALUE).split(",");
                    if (sizes.length >= 1 && sizes[0] != null && sizes[0].length() > 0) {
                        intSize = new Integer(sizes[0]);
                    }
                    if (sizes.length >= 2 && sizes[1] != null && sizes[1].length() > 0) {
                        fracSize = new Integer(sizes[1]);
                    }
                } catch (NumberFormatException e) {
                    throw new InvalidRuleParameterException(targetPropertyName, getName(), "value", rule.getParameter(RuleMetaData.PARAMETER_VALUE));
                }
                if (intSize != null || fracSize != null) {
                    if (targetPropertyValue instanceof String) {
                        if (intSize != null && ((String) targetPropertyValue).length() > intSize.intValue()) {
                            throw logErrorCreateException(targetObject, targetPropertyName, targetPropertyValue, rule);
                        }
                    } else if (targetPropertyValue instanceof Number) {
                        if (!checkLength((Number) targetPropertyValue, intSize, fracSize)) {
                            throw logErrorCreateException(targetObject, targetPropertyName, targetPropertyValue, rule);
                        }
                    } else if (targetPropertyValue instanceof Currency) {
                        if (!checkLength(((Currency) targetPropertyValue).getValue(), intSize, fracSize)) {
                            throw logErrorCreateException(targetObject, targetPropertyName, targetPropertyValue, rule);
                        }
                    } else {
                        if (log.isDebugEnabled())
                            log.debug("This rule does not support instances of " + targetPropertyValue.getClass().getName());
                    }
                }
            }
        }
    }
}
Also used : InvalidRuleParameterException(org.jaffa.rules.rulemeta.InvalidRuleParameterException) Currency(org.jaffa.datatypes.Currency) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 25 with RuleMetaData

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

the class PartialForeignKeyValidator method validateProperty.

/**
 * {@inheritDoc}
 */
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
    if (targetPropertyValue != null) {
        for (RuleMetaData rule : rules) {
            if (log.isDebugEnabled()) {
                log.debug("Applying " + rule + " on " + targetPropertyValue);
            }
            String domainName = rule.getParameter(PARAMETER_DOMAIN_NAME);
            String propName = rule.getParameter(PARAMETER_PROPERTY_NAME);
            if (propName == null) {
                propName = targetPropertyName;
            }
            propName = StringHelper.getUpper1(propName);
            Criteria c = new Criteria();
            c.setTable(domainName);
            c.addCriteria(propName, targetPropertyValue);
            if (!uow.query(c).iterator().hasNext()) {
                String className;
                try {
                    className = Class.forName(domainName).getName();
                } catch (ClassNotFoundException e) {
                    throw new JaffaRulesFrameworkException(e.getMessage(), null, e);
                }
                // Invalid value. Display the list of valid values in the error message
                // It is valid for multiple row to be returned so we don't check for that here
                String domainLabel = getObjectLabel(className, null);
                String fkLabel = getPropertyLabel(targetObject, targetPropertyName);
                String primaryKeyLabel = getPrimaryKeyLabel(className, null);
                if (primaryKeyLabel == null) {
                    primaryKeyLabel = fkLabel.toString();
                }
                if (log.isDebugEnabled()) {
                    log.debug("PartialForeignKey validation for the value '" + targetPropertyValue + "' of '" + targetPropertyName + "' failed against the domainObject '" + domainName + '\'');
                }
                String errorCode = getErrorCode(primaryKeyLabel, rule);
                Object[] arguments = getErrorArgumentArray(targetObject, rule);
                if (arguments == null) {
                    arguments = new Object[] { domainLabel, primaryKeyLabel };
                }
                throw wrapException(new InvalidForeignKeyException(errorCode, arguments), targetObject, rule);
            }
        }
    }
}
Also used : InvalidForeignKeyException(org.jaffa.datatypes.exceptions.InvalidForeignKeyException) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) Criteria(org.jaffa.persistence.Criteria) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException)

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