use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class CommentValidator 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 (log.isDebugEnabled()) {
log.debug("Applying " + rule + " on " + targetPropertyValue);
}
Object initialValue = returnInitialValue(targetObject, targetPropertyName);
String commentStyle = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
Object value = null;
if ("fifo".equals(commentStyle)) {
value = addCommentWithStamp((String) initialValue, (String) targetPropertyValue, false);
} else if ("lifo".equals(commentStyle)) {
value = addCommentWithStamp((String) initialValue, (String) targetPropertyValue, true);
} else {
value = targetPropertyValue;
}
if (value != null && !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 + '\'');
}
}
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class InListValidator 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);
}
Map<String, String> inListValues = MetaDataIntrospector.getInListValues(rule);
// Iterate through the valid Strings
// convert the String to the appropriate datatype and then perform the comparison
boolean isValueValid = false;
for (String validString : inListValues.keySet()) {
Object validValue = DataTypeMapper.instance().map(validString, targetPropertyValue.getClass());
if (targetPropertyValue.equals(validValue)) {
isValueValid = true;
break;
}
}
if (!isValueValid) {
String listParam = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
String[] listValues = listParam.split(",");
String errorList = "";
if (listValues != null) {
for (int i = 0; i < listValues.length; i++) {
if (i > 0) {
errorList = errorList + ", ";
}
if (listValues[i].indexOf("=") > 0) {
errorList = errorList + MessageHelper.replaceTokens(listValues[i].split("=")[1]);
} else {
errorList = errorList + MessageHelper.replaceTokens(listValues[i]);
}
}
}
if (log.isDebugEnabled()) {
log.debug("The value '" + targetPropertyValue + "' not found in the List '" + errorList + "'");
}
throw wrapException(InListException.class, targetObject, targetPropertyName, rule);
}
}
}
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class RuleValidatorFactory 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 org.jaffa.exceptions.ApplicationExceptions if any application exception occurs.
* @throws org.jaffa.exceptions.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 RuleValidatorTest method testSetRuleMap.
/**
* Bogus entries should be trimmed when setting the rule map.
*/
@Test
public void testSetRuleMap() {
RuleValidator target = new TestValidator();
Map<String, List<RuleMetaData>> ruleMap = new HashMap<>();
List<RuleMetaData> valid = new ArrayList<>();
RuleMetaData rule = new RuleMetaData();
valid.add(rule);
List<RuleMetaData> invalid = new ArrayList<>();
ruleMap.put(null, valid);
ruleMap.put("", valid);
ruleMap.put("a", null);
ruleMap.put("b", invalid);
ruleMap.put("c", valid);
target.setRuleMap(ruleMap);
Map<String, List<RuleMetaData>> actual = target.getRuleMap();
assertEquals(3, actual.size());
assertEquals(rule, actual.get("c").get(0));
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class CxfFunctionGuardInterceptorTest method updateRules.
/**
* Update the rules list and set the name of the rule in the list - Test is the expected name
*
* @param name Name of the rule for the test
* @param method Name of the method the rule applies to
* @param repository Rule repository
* @param ruleHelper Rule helper
*/
private void updateRules(String name, String method, MetaDataRepository repository, IRuleHelper ruleHelper) throws FrameworkException, ApplicationExceptions {
Map<String, List<RuleMetaData>> rules = new HashMap<>();
List<RuleMetaData> ruleMetaDatas = new ArrayList<>();
RuleMetaData rule = new RuleMetaData();
rule.addParameter("method", method);
rule.addParameter("name", name);
ruleMetaDatas.add(rule);
rules.put(null, ruleMetaDatas);
when(repository.getPropertyRuleMap(Mockito.anyString(), Mockito.anyString())).thenReturn(rules);
when(ruleHelper.getApplicableRules(Mockito.anyString(), Mockito.anyObject(), Mockito.anyListOf(RuleMetaData.class), Mockito.anyBoolean())).thenReturn(ruleMetaDatas);
}
Aggregations