use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class RuleActor method getObjectLabel.
/**
* A convenience method to return the object label to be used in error messages.
* It'll check for the label in the following order
* - Lookup a 'label' rule for the input class
* - Return the class name
*
* @param targetClassName The target Class.
* @param targetObject The target Object.
* @return label for the class.
*/
protected String getObjectLabel(String targetClassName, Object targetObject) {
String label = null;
try {
// Search for the first available class-level label rule
Map<String, List<RuleMetaData>> pkMap = getPropertyRuleMap(targetClassName, targetObject, "label");
if (pkMap != null && pkMap.containsKey(null)) {
List<RuleMetaData> rules = pkMap.get(null);
if (rules != null && rules.size() > 0) {
RuleMetaData rule = rules.get(0);
label = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
}
}
} catch (Exception e) {
// do nothing
}
// See if this can be resolved with a persistence meta class
if (label == null) {
try {
label = PersistentHelper.getLabelToken(targetClassName);
} catch (Exception e) {
// do nothing
}
}
// Finally default to the simple class name
if (label == null) {
label = StringHelper.getShortClassName(targetClassName);
}
return label;
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class RuleActor method getPropertyRuleMap.
/**
* Returns a Map containing a List of RuleMetaData instances per propertyName for the className+ruleName combination.
* The class-level RuleMetaData instances defined for the className+ruleName combination will be added to the Map with propertyName null.
* The className is obtained from the targetClass.
*
* @param targetClassName The target Class.
* @param ruleName the rule to search for.
* @return a Map containing a List of RuleMetaData instances per propertyName for the className+ruleName combination.
* @throws ApplicationExceptions if any application exception occurs.
* @throws FrameworkException if any internal error occurs.
*/
protected Map<String, List<RuleMetaData>> getPropertyRuleMap(String targetClassName, String ruleName) throws ApplicationExceptions, FrameworkException {
if (targetClassName != null) {
Map<String, List<RuleMetaData>> map = MetaDataRepository.instance().getPropertyRuleMap(targetClassName, ruleName);
if (map != null) {
IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleName);
Map<String, List<RuleMetaData>> newMap = null;
for (Map.Entry<String, List<RuleMetaData>> me : map.entrySet()) {
List<RuleMetaData> rules = ruleHelper.getApplicableRules(targetClassName, me.getValue(), true);
if (rules != null && rules.size() > 0) {
if (newMap == null) {
newMap = new LinkedHashMap<>();
}
newMap.put(me.getKey(), rules);
}
}
map = newMap;
}
return map;
} else {
return null;
}
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class RuleActor method getPropertyLabel.
/**
* A convenience method to return the property label to be used in error messages.
* It'll check for the label in the following order
* - Lookup a 'label' rule for this property
* - Return the property name
*
* @param targetObject The target Object.
* @param targetPropertyName The property for which the label is to be found.
* @return label for the property.
*/
protected String getPropertyLabel(Object targetObject, String targetPropertyName) {
String label = null;
String targetClassName = getActualClassName(targetObject.getClass());
try {
if (targetObject instanceof FlexBean) {
if (((FlexBean) targetObject).getDynaClass().getDynaProperty(targetPropertyName) == null && ((FlexBean) targetObject).getPersistentObject() != null) {
targetObject = ((FlexBean) targetObject).getPersistentObject();
targetClassName = targetObject.getClass().getName();
}
} else if (targetObject instanceof IFlexFields) {
if (((IFlexFields) targetObject).getFlexBean() != null && ((IFlexFields) targetObject).getFlexBean().getDynaClass().getDynaProperty(targetPropertyName) != null) {
targetObject = ((IFlexFields) targetObject).getFlexBean();
targetClassName = ((FlexBean) targetObject).getDynaClass().getName();
}
}
// Search for the first available property-level label rule
Map<String, List<RuleMetaData>> pkMap = getPropertyRuleMap(targetClassName, targetObject, "label");
if (pkMap != null && pkMap.containsKey(targetPropertyName)) {
List<RuleMetaData> rules = pkMap.get(targetPropertyName);
if (rules != null && rules.size() > 0) {
RuleMetaData rule = rules.get(0);
label = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
}
}
} catch (Exception e) {
// do nothing
}
if (label == null) {
label = targetPropertyName;
}
return label;
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class CandidateKeyValidator method validateProperty.
/**
* {@inheritDoc}
*/
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
String targetClassName = getActualClassName(targetObject.getClass());
IPersistent persistentTargetObject = null;
try {
// Use the associated persistentObject for a FlexBean instance
if (targetObject instanceof FlexBean && ((FlexBean) targetObject).getPersistentObject() != null) {
persistentTargetObject = ((FlexBean) targetObject).getPersistentObject();
targetClassName = persistentTargetObject.getClass().getName();
}
// This rules makes sense on IPersistent instances only. Bail out on all other instances
if (!(targetObject instanceof IPersistent)) {
return;
} else if (persistentTargetObject == null) {
persistentTargetObject = (IPersistent) targetObject;
}
// Obtain a Map containing key fields and corresponding values
Map<String, Object> keyValueMap = getKeyValueMap(targetClassName, (IPersistent) targetObject);
// Create a criteria such that the current object is not included in the search for candidate-key violations
AtomicCriteria criteriaToDiscardCurrentObject = createCriteriaToDiscardCurrentObject(persistentTargetObject, keyValueMap);
// Invoke each rule
for (RuleMetaData rule : rules) {
invoke(targetClassName, persistentTargetObject, uow, rule, criteriaToDiscardCurrentObject, keyValueMap);
}
} catch (ApplicationExceptions e) {
if (log.isDebugEnabled()) {
log.debug("Error in validateProperty method for object: " + targetClassName, e);
}
throw new JaffaRulesFrameworkException("Error in validateProperty method for object: " + targetClassName, null, e);
}
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class CaseTypeValidator method validateProperty.
/**
* {@inheritDoc}
*/
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules) throws ApplicationException, FrameworkException {
RuleMetaData rule = rules.get(0);
if (targetPropertyValue != null && targetPropertyValue instanceof String) {
if (log.isDebugEnabled()) {
log.debug("Applying " + rule + " on " + targetPropertyValue);
}
String caseType = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
Object value = "upper".equals(caseType) ? ((String) targetPropertyValue).toUpperCase() : ("lower".equals(caseType) ? ((String) targetPropertyValue).toLowerCase() : targetPropertyValue);
if (!value.equals(targetPropertyValue)) {
try {
BeanHelper.setField(targetObject, targetPropertyName, value);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Could not set the field: " + targetObject.getClass().getName() + "." + targetPropertyName, e);
}
return;
}
if (log.isDebugEnabled()) {
log.debug("Changed value to '" + value + '\'');
}
}
} else {
if (targetPropertyValue != null && log.isDebugEnabled()) {
log.debug(targetPropertyName + " not a String and hence " + rule + " cannot be applied");
}
}
}
Aggregations