Package org.joget.apps.app.service

Source Code of org.joget.apps.app.service.AuditTrailInterceptor

package org.joget.apps.app.service;

import org.joget.commons.spring.model.Auditable;
import org.joget.commons.util.LogUtil;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.joget.workflow.util.WorkflowUtil;

/**
* AOP interceptor to perform audit trail logging.
*/
public class AuditTrailInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Object returnObject = null;
        String methodName = methodInvocation.getMethod().getName();
        if (methodName.startsWith("get")
                || methodName.startsWith("is")
                || methodName.startsWith("internal")) {
            returnObject = methodInvocation.proceed();
        } else {
            try {
                returnObject = methodInvocation.proceed();
            } finally {
                LogUtil.debug(getClass().getName(), "INTERCEPTED: " + methodName);

                Object[] args = methodInvocation.getArguments();
                Class[] param = methodInvocation.getMethod().getParameterTypes();

                boolean auditableObjExists = false;

                String message = "";
                int i = 0;
                for (Class clazz : param) {
                    if (args[i] instanceof Auditable) {
                        Auditable obj = (Auditable) args[i];
                        message += clazz.getName() + "@" + obj.getAuditTrailId() + ";";
                        auditableObjExists = true;
                    }

                    i++;
                }

                if (!auditableObjExists) {
                    //get first argument (usually is ID)
                    Object obj = args[0];
                    if (obj != null) {
                        message = args[0].toString();
                    } else {
                        message = "";
                    }
                }

                //remove trailing ';'
                if (auditableObjExists && message.length() > 0) {
                    message = message.substring(0, message.length() - 1);
                }

                // add audit trail record
                WorkflowUtil.addAuditTrail(methodInvocation.getThis().getClass().getSimpleName(), methodName, message);
            }
        }
        return returnObject;
    }
}
TOP

Related Classes of org.joget.apps.app.service.AuditTrailInterceptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.