/ Published in: Java
Audit Trail
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package net.javabeat.spring.audit; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.annotation.Resource; import org.apache.commons.beanutils.BeanMap; import org.apache.commons.beanutils.PropertyUtilsBean; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.hibernate.SessionFactory; import org.hibernate.metadata.ClassMetadata; import org.infinispan.commons.api.BasicCache; import org.springframework.aop.framework.Advised; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Component; import net.javabeat.spring.cache.LocalCacheManager; import net.javabeat.spring.model.AuditTable; import net.javabeat.spring.service.AuditFlowService; import net.javabeat.spring.service.AuditTableService; import net.javabeat.spring.util.JsonUtils; @Aspect @Configurable @EnableAspectJAutoProxy public class AuditTrailAspect { @Autowired private AuditTableService auditTableService; @Resource private SessionFactory sessionFactory; @Autowired private AuditFlowService auditFlowService; @Before("execution(public * net.javabeat.spring.dao..*.genericUpdate(..)) && args(bean)") try { Advised advised = (Advised) joinPoint.getThis(); Class<?> cls = advised.getTargetSource().getTargetClass(); ClassMetadata metadata = sessionFactory.getClassMetadata(bean.getClass()); JsonUtils utils = new JsonUtils(); caches.put(key, oldValue); // TODO Auto-generated catch block e.printStackTrace(); } } @AfterReturning("execution(public * net.javabeat.spring.dao..*.genericUpdate(..)) && args(bean)") try { Advised advised = (Advised) joinPoint.getThis(); Class<?> cls = advised.getTargetSource().getTargetClass(); JsonUtils utils = new JsonUtils(); ClassMetadata metadata = sessionFactory.getClassMetadata(bean.getClass()); key = bean.toString() + typeid; ObjectMapper mapper = new ObjectMapper(); ChangeSetRecord changeSetRecord = new ChangeSetRecord (); changeSetRecord = compareObjects(oldValue, bean); if (changeSetRecord != null){ AuditTable auditRecord = new AuditTable(); auditRecord.setEntityname(bean.getClass().getSimpleName()); auditRecord.setAction("Update"); auditRecord.setChangeset(mapper.writeValueAsString( changeSetRecord ) ); auditTableService.createAudit(auditRecord); } // TODO Auto-generated catch block e.printStackTrace(); } } List<String> current = get(key); if (current == null) { current = new ArrayList<String>(); super.put(key, current); } current.add(index); } } public ChangeSetRecord compareObjects(Object oldObject, Object newObject) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { BeanMap map = new BeanMap(oldObject); PropertyUtilsBean propUtils = new PropertyUtilsBean(); ChangeSetRecord changeSetRecord = new ChangeSetRecord(); if (property1.equals(property2)) { } else { System.out.println("> " + propertyName + " is different (oldValue=\"" + property1 + "\", newValue=\"" + property2 + "\")"); changeSetRecord.put(propertyName, (property1 == null ? "" : property1.toString())); changeSetRecord.put(propertyName, (property2 == null ? "" : property2.toString())); } } return changeSetRecord; } }