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);
}
}
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);
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
}
}
Aggregations