Search in sources :

Example 1 with InvalidRuleParameterException

use of org.jaffa.rules.rulemeta.InvalidRuleParameterException in project jaffa-framework by jaffa-projects.

the class ForeignKeyValidator method findMethod.

/**
 * An InvalidRuleParameterException will be thrown if the domainObject does not have a 'public static boolean exists(UOW uow, Object key)' method.
 * A reference to the method is maintained in a field.
 */
private Method findMethod(String[] fkFields, RuleMetaData rule) {
    // Get a handle on the 'public static boolean exists(UOW uow, Object key1...)' method.
    String domainClassName = rule.getParameter(RuleMetaData.PARAMETER_DOMAIN_OBJECT);
    try {
        Class domainClass = Class.forName(domainClassName);
        Method[] methods = domainClass.getMethods();
        Method m = null;
        if (methods != null) {
            for (Method method : methods) {
                m = method;
                if (m.getName().equals("exists") && m.getParameterTypes() != null && m.getParameterTypes().length == (fkFields.length + 1) && UOW.class.isAssignableFrom(m.getParameterTypes()[0]) && m.getReturnType() == Boolean.TYPE && Modifier.isStatic(m.getModifiers())) {
                    break;
                } else {
                    m = null;
                }
            }
        }
        if (m == null) {
            log.error("The 'public static boolean exists(UOW uow, Object key)' method not found on the domainObject '" + domainClassName + "'");
            throw new InvalidRuleParameterException(StringHelper.stringArrayToString(fkFields), getName(), "domainObject", domainClassName);
        }
        return m;
    } catch (ClassNotFoundException e) {
        log.error("The domainObject '" + domainClassName + "' class not found", e);
        throw new InvalidRuleParameterException(StringHelper.stringArrayToString(fkFields), getName(), "domainObject", domainClassName);
    }
}
Also used : InvalidRuleParameterException(org.jaffa.rules.rulemeta.InvalidRuleParameterException) Method(java.lang.reflect.Method) UOW(org.jaffa.persistence.UOW)

Example 2 with InvalidRuleParameterException

use of org.jaffa.rules.rulemeta.InvalidRuleParameterException 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)

Aggregations

InvalidRuleParameterException (org.jaffa.rules.rulemeta.InvalidRuleParameterException)2 Method (java.lang.reflect.Method)1 Currency (org.jaffa.datatypes.Currency)1 UOW (org.jaffa.persistence.UOW)1 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)1