use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class PrimaryKeyValidator method validateProperty.
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
// No need to perform validations for an IPersistent instance that exists in the database
if (targetObject instanceof IPersistent && ((IPersistent) targetObject).isDatabaseOccurence()) {
if (log.isDebugEnabled()) {
log.debug(getName() + " check not performed since the target object already exists in the database");
}
return;
}
RuleMetaData rule = rules.get(0);
if (log.isDebugEnabled()) {
log.debug("Applying " + rule + " on " + targetObject);
}
Criteria criteria = null;
String pk = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
String[] pkFields = pk.split(",");
for (String pkField : pkFields) {
Object pkValue = null;
try {
pkValue = BeanHelper.getField(targetObject, pkField);
} catch (NoSuchMethodException e) {
if (log.isDebugEnabled()) {
log.debug("No Such Method Exception: " + targetObject.getClass().getName() + "." + pkField, e);
}
return;
}
if (pkValue == null) {
if (log.isDebugEnabled()) {
log.debug(getName() + " check not performed since the key field " + pkField + " is null");
}
return;
} else {
// Add to criteria
if (criteria == null) {
criteria = new Criteria();
}
criteria.addCriteria(pkField, pkValue);
}
}
if (criteria != null) {
criteria.setTable(getActualClassName(targetObject.getClass()));
if (uow.query(criteria).iterator().hasNext()) {
if (log.isDebugEnabled()) {
log.debug("Primary key '" + pk + "' is not unique for the object " + targetObject);
}
String objectLabel = getObjectLabel(getActualClassName(targetObject.getClass()), targetObject);
throw wrapException(new DuplicateKeyException(objectLabel), targetObject, rule);
}
}
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class RuleValidatorFactory method createValidator.
/**
* Creates a validator.
*
* @param className the name of the class to create a validator for.
* @param metaDataList the class meta data to create a validator from.
* @return a validator.
*/
private Validator<?> createValidator(String className, List<ClassMetaData> metaDataList) {
// create a validator to collect all rule validators for this class
BatchValidator<?> resultValidator = new BatchValidator();
// associated with the class
for (ClassMetaData classMetaData : metaDataList) {
// Retrieve class-level rules
List<RuleMetaData> allRulesForClass = classMetaData.getClassRules();
if (allRulesForClass != null) {
for (RuleMetaData ruleMetaData : allRulesForClass) {
String ruleName = ruleMetaData.getName();
try {
// get the map of rules that apply to the class in new condition
Map<String, List<RuleMetaData>> ruleMap = getPropertyRuleMap(className, ruleName);
// look up the validator from the context
RuleValidator foundValidator = (RuleValidator) lookupValidator(ruleName);
if (ruleMap != null && !ruleMap.isEmpty() && foundValidator != null) {
// add the property map to the validator which was found in the context
foundValidator.setRuleMap(ruleMap);
// save the validator to the batch validator
if (!resultValidator.getValidatorSet().contains(foundValidator))
resultValidator.getValidatorSet().add(foundValidator);
}
} catch (Exception e) {
logger.error("Error occurred creating validator for rule: " + ruleName + " while getting property rule map");
}
}
}
List<PropertyMetaData> propertyMetaDataList = classMetaData.getProperties();
if (propertyMetaDataList != null) {
// iterate list of properties for this class to determine rules associated with it.
for (PropertyMetaData propertyMetaData : propertyMetaDataList) {
List<RuleMetaData> allRulesForProperty = propertyMetaData.getRules(className);
if (allRulesForProperty != null) {
// constructed validator
for (RuleMetaData ruleMetaData : allRulesForProperty) {
String ruleName = ruleMetaData.getName();
try {
// get the map of rules that apply to the class in new condition
Map<String, List<RuleMetaData>> ruleMap = getPropertyRuleMap(className, ruleName);
// look up the validator from the context
RuleValidator foundValidator = (RuleValidator) lookupValidator(ruleName);
if (ruleMap != null && !ruleMap.isEmpty() && foundValidator != null) {
// add the property map to the validator which was found in the context
foundValidator.setRuleMap(ruleMap);
// save the validator to the batch validator
if (!resultValidator.getValidatorSet().contains(foundValidator)) {
resultValidator.getValidatorSet().add(foundValidator);
} else {
// if a validator of the same type exists in the batch, extract the rule meta
// / data and append to the existing property map
appendPropertyRule(resultValidator, foundValidator, ruleMap);
}
}
} catch (Exception e) {
logger.error("Error occurred creating validator for rule: " + ruleName + " while getting property rule map");
}
}
}
}
}
}
return resultValidator;
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class RuleValidatorTest method testConditionalValidation.
/**
* When a condition is specified on a rule, property validation should only be called if the condition is true.
*/
@Test
public void testConditionalValidation() throws FrameworkException, ApplicationExceptions, ApplicationException {
TestValidator target = new TestValidator();
IRuleEvaluator ruleEvaluator = mock(IRuleEvaluator.class);
target.setRuleEvaluator(ruleEvaluator);
Map<String, List<RuleMetaData>> ruleMap = new HashMap<>();
List<RuleMetaData> rules = new ArrayList<>();
RuleMetaData trueCondition = new RuleMetaData();
rules.add(trueCondition);
RuleMetaData falseCondition = new RuleMetaData();
rules.add(falseCondition);
ruleMap.put("field1", rules);
target.setRuleMap(ruleMap);
TestModel obj = new TestModel();
obj.setField1("value");
when(ruleEvaluator.checkCondition(obj, trueCondition)).thenReturn(true);
target.validate(obj);
assertEquals(1, target.validatedRules.size());
assertEquals(trueCondition, target.validatedRules.get(0));
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class SOAEventBaseHandler method raiseSOAEvent.
/**
* Check if the method being invoked on the handler is the trigger method defined in the rule meta data then raise the event
*
* @param invocationMethod method being invoked
* @param target the target object
* @param uow unit of work for the life cycle event handler
* @throws ApplicationExceptions
* @throws FrameworkException
*/
protected void raiseSOAEvent(String invocationMethod, Object target, UOW uow, Object[] args) throws ApplicationExceptions, FrameworkException {
if (log.isDebugEnabled()) {
log.debug("Handle Event : " + invocationMethod + " for " + " (Target=" + shortClassName(target) + ")");
}
if (invocationMethod.equals(ruleMetaData.getParameter(RuleMetaData.PARAMETER_TRIGGER))) {
// Need to check the condition
List<RuleMetaData> rules = new ArrayList<>();
rules.add(ruleMetaData);
IRuleHelper ruleHelper = RuleHelperFactory.instance(ruleMetaData.getName());
// Check if the rule is applicable for the target object (evaluates rule condition)
List<RuleMetaData> applicableRules = ruleHelper.getApplicableRules(target.getClass().getName(), target, rules, true);
// raise event for each applicableRule
if (applicableRules != null) {
for (RuleMetaData rule : applicableRules) {
raiseSOAEvent(uow, target, rule, args);
}
}
}
}
use of org.jaffa.rules.meta.RuleMetaData in project jaffa-framework by jaffa-projects.
the class CxfFunctionGuardInterceptor method checkAccess.
/**
* Checks if a caller has access to a method being invoked
*
* @param method Method being invoked
* @throws AccessControlException Thrown when the caller doesn't have access
*/
private void checkAccess(Method method) throws AccessControlException, ApplicationExceptions, FrameworkException {
String targetClassName = method.getDeclaringClass().getName();
// Get the rule map
Map<String, List<RuleMetaData>> ruleMap = getPropertyRuleMap(targetClassName, "function-guard");
if (ruleMap == null) {
return;
}
// Get the rules
List<RuleMetaData> rules = ruleMap.get(null);
if (rules == null) {
return;
}
// Check access for any rules that match
for (RuleMetaData rule : rules) {
if (match(method, rule)) {
checkAccess(method, targetClassName, rule);
}
}
}
Aggregations