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