use of org.jaffa.soa.rules.PendingEventException in project jaffa-framework by jaffa-projects.
the class AuditLogger method audit.
private void audit(IPersistent object, ChangeType changeType) throws ApplicationExceptions, FrameworkException {
IObjectRuleIntrospector introspector = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(object);
Properties auditInfo = introspector.getAuditInfo();
if (auditInfo != null) {
// find auditInfo for the various properties
Map<String, Properties> auditInfoMap = introspector.getAuditInfoForProperties();
// obtain information for flex fields
FlexBean flexBean = null;
IObjectRuleIntrospector flexIntrospector = null;
Map<String, Properties> auditInfoOfFlexFields = null;
if (object instanceof IFlexFields) {
flexBean = ((IFlexFields) object).getFlexBean();
if (flexBean != null) {
flexIntrospector = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(flexBean.getDynaClass().getName(), flexBean);
auditInfoOfFlexFields = flexIntrospector.getAuditInfoForProperties();
}
}
// determine if auditing is required
boolean doAudit = changeType == ChangeType.INSERT || changeType == ChangeType.DELETE;
if (changeType == ChangeType.UPDATE) {
// In update-mode, auditing is required only if one of the audit fields has changed
for (String propertyName : auditInfoMap.keySet()) {
if (object.isModified(propertyName) || object.isModified(StringHelper.getUpper1(propertyName))) {
doAudit = true;
break;
}
}
// check if flex fields have changed
if (!doAudit && auditInfoOfFlexFields != null) {
for (String propertyName : auditInfoOfFlexFields.keySet()) {
if (flexBean.hasChanged(propertyName)) {
doAudit = true;
break;
}
}
}
}
if (doAudit) {
// Raise PendingEventException if reason is required, but is missing in the MDC
Boolean reasonRequired = Parser.parseBoolean(auditInfo.getProperty("reason-required"));
if (reasonRequired != null && reasonRequired && MDC.get(AuditTransactionMeta.REASON) == null) {
if (log.isDebugEnabled())
log.debug("Throwing PendingEventException. 'Reason' needs to be provided in the MDC, since the reason-required attribute has been set to true in the audit rule for " + object.getClass());
throw new ApplicationExceptions(new PendingEventException("ReasonRequiredForAuditing"));
}
// Create AuditTransaction
createAuditTransaction();
// Create AuditTransactionObject
String objectId = createAuditTransactionObject(auditInfo.getProperty("name"), changeType);
// Create AuditTransactionField instances
createAuditTransactionFields(objectId, object, auditInfoMap, flexBean, auditInfoOfFlexFields);
} else {
if (log.isDebugEnabled())
log.debug("Audit logging not required since none of the auditable fields have changed in " + object);
}
} else {
if (log.isDebugEnabled())
log.debug("Audit logging not enabled for " + object.getClass());
}
}
Aggregations