Search in sources :

Example 6 with JaffaRulesFrameworkException

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

the class GenericForeignKeyValidator method determineValueClass.

/**
 * This will determine the class of the 'fieldNameForValue' field of the generic foreign key class.
 * This should typically be a String.class.
 *
 * @param domainClassName   the full name of the domain class.
 * @param fieldNameForValue the field of the generic foreign key class.
 */
private Class<?> determineValueClass(String domainClassName, String fieldNameForValue) throws FrameworkException {
    try {
        Class<?> domainClass = Class.forName(domainClassName);
        Method method = domainClass.getMethod(BeanHelper.getReaderName(fieldNameForValue), (Class<?>[]) null);
        return method.getReturnType();
    } catch (Exception e) {
        throw new JaffaRulesFrameworkException(e.getMessage(), null, e);
    }
}
Also used : Method(java.lang.reflect.Method) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) FrameworkException(org.jaffa.exceptions.FrameworkException) InvalidGenericForeignKeyException(org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException) ApplicationException(org.jaffa.exceptions.ApplicationException) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException)

Example 7 with JaffaRulesFrameworkException

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

Example 8 with JaffaRulesFrameworkException

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

the class AbstractLoader method load.

/**
 * Imports XML.
 * @param uri the URI of the source file.
 * @throws JaffaRulesFrameworkException if any internal error occurs.
 */
public void load(String uri) throws JaffaRulesFrameworkException {
    try {
        if (log.isDebugEnabled())
            log.debug("Loading URI " + uri);
        // Create an XML Parser
        DocumentBuilder parser = createParser();
        // Parse the file and build a Document tree to represent its content
        Document document = parser.parse(uri);
        // Load the metadata elements within the Document
        load(document, uri);
    } catch (Exception e) {
        log.error("Error in parsing the XML from " + uri, e);
        throw new JaffaRulesFrameworkException(JaffaRulesFrameworkException.PARSE_ERROR, new Object[] { uri }, e);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException)

Example 9 with JaffaRulesFrameworkException

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

the class AbstractLoader method load.

/**
 * Imports XML by parsing the metadata elements in the input character stream.
 * @param characterStream the character stream containing metadata elements.
 * @param source a unique name for identifying the character stream.
 * @throws JaffaRulesFrameworkException if any internal error occurs.
 */
public void load(Reader characterStream, String source) throws JaffaRulesFrameworkException {
    try {
        if (log.isDebugEnabled())
            log.debug("Loading from character stream identified by the source " + source);
        // Create an XML Parser
        DocumentBuilder parser = createParser();
        // Parse the file and build a Document tree to represent its content
        Document document = parser.parse(new InputSource(characterStream));
        // Load the metadata elements within the Document
        load(document, source);
    } catch (Exception e) {
        log.error("Error in parsing the XML from " + source, e);
        throw new JaffaRulesFrameworkException(JaffaRulesFrameworkException.PARSE_ERROR, new Object[] { source }, e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException)

Example 10 with JaffaRulesFrameworkException

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

the class FieldInitializer method initialize.

/**
 * Apply the initialize rule
 *
 * @param object       the object to be initialized
 * @param propertyName the name of the property to be initialized
 * @param rule         the rule used for initialization
 * @throws Exception if any error occurs
 */
private void initialize(Object object, String propertyName, RuleMetaData rule) throws JaffaRulesFrameworkException {
    // This is important, else the current value for the flex field will be unintentionally overwritten with the initial value.
    if (object instanceof FlexBean) {
        FlexBean flexBean = (FlexBean) object;
        if (flexBean.getPersistentObject() != null && flexBean.getPersistentObject().isDatabaseOccurence()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Rule " + rule + " on property" + propertyName + " of " + object + " cannot be applied since the targetObject is a FlexBean associated with an in-database persistentObject");
            }
            return;
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Applying " + rule + " on property" + propertyName + " of " + object);
    }
    String value = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
    String member = rule.getParameter(RuleMetaData.PARAMETER_MEMBER);
    Boolean expression = Parser.parseBoolean(rule.getParameter(RuleMetaData.PARAMETER_EXPRESSION));
    // evaluate expression if it exists
    Object evaluatedValue = value;
    if (expression != null && expression) {
        String language = rule.getParameter(RuleMetaData.PARAMETER_LANGUAGE);
        try {
            evaluatedValue = ScriptHelper.instance(language).evaluate(null, value, object, rule.getSource(), rule.getLine() != null ? rule.getLine() : 0, 0);
        } catch (Exception exception) {
            throw new JaffaRulesFrameworkException("Error evaluating expression during initialize of object: " + object.getClass().getName(), null, exception);
        }
        if (evaluatedValue != null && !(evaluatedValue instanceof String)) {
            evaluatedValue = DataTypeMapper.instance().map(evaluatedValue, String.class);
        }
    }
    if (member == null) {
        try {
            BeanHelper.setField(object, propertyName, (String) evaluatedValue);
        } catch (Exception exception) {
            // The rules engine should be passive and ignore the exceptions that a setter might throw
            if (logger.isDebugEnabled()) {
                logger.debug("Set property '" + propertyName + " = " + evaluatedValue + "' has failed", exception);
            }
        }
    } else {
        try {
            Field field = null;
            Class clazz = object.getClass();
            while (field == null) {
                try {
                    field = clazz.getDeclaredField(member);
                } catch (NoSuchFieldException e) {
                    if (clazz.getSuperclass() == null)
                        break;
                    clazz = clazz.getSuperclass();
                }
            }
            if (field == null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Member field " + member + " not found on class " + object.getClass().getName());
                }
            } else {
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                field.set(object, DataTypeMapper.instance().map(evaluatedValue, field.getType()));
            }
        } catch (Exception exception) {
            // The rules engine should be passive and ignore the exceptions that a setter might throw
            if (logger.isDebugEnabled()) {
                logger.debug("Set member '" + member + " = " + evaluatedValue + "' has failed", exception);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) FlexBean(org.jaffa.flexfields.FlexBean) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) FrameworkException(org.jaffa.exceptions.FrameworkException) ApplicationException(org.jaffa.exceptions.ApplicationException) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException)

Aggregations

JaffaRulesFrameworkException (org.jaffa.rules.JaffaRulesFrameworkException)10 FrameworkException (org.jaffa.exceptions.FrameworkException)6 ApplicationException (org.jaffa.exceptions.ApplicationException)5 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)3 Document (org.w3c.dom.Document)3 Method (java.lang.reflect.Method)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 InvalidForeignKeyException (org.jaffa.datatypes.exceptions.InvalidForeignKeyException)2 DuplicateCandidateKeyException (org.jaffa.exceptions.DuplicateCandidateKeyException)2 FlexBean (org.jaffa.flexfields.FlexBean)2 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)2 Criteria (org.jaffa.persistence.Criteria)2 File (java.io.File)1 Field (java.lang.reflect.Field)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1