Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

how implement global AuditLogInterceptor using Spring 3 and contextual sessions?

$
0
0
Hello I need advice on implementing global AuditLogInterceptor
in conjunction with using contextual sessions.

Below is the code I am currently using. I produced it by modifying
code that I had working when using an interceptor per session and
not using contextual sessions (which I can send if required).
Please can you inform me where I'm going wrong as I'm struggling to
understand what's required? n.b. I've shown the main parts of the code.

Code:

Auditable.java:
public interface Auditable {
        public Integer getId();
}

AuditLogInterceptor.java:
@Component
public class AuditLogInterceptor extends EmptyInterceptor {

        @Autowired
        private SessionFactory sessionFactory;
       
        private Set inserts = new HashSet();
        private Set updates = new HashSet();

        private org.hibernate.classic.Session session;

        public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
                        throws CallbackException {
                       
                if (entity instanceof Auditable) {
                        inserts.add(entity);
                }

                return false;
        }
       
        public boolean onFlushDirty(
                        Object entity,
                        Serializable id,
                        Object[] currentState,
                        Object[] previousState,
                        String[] propertyNames,
                        Type[] types)
                        throws CallbackException {
               
                return false;               
        }
       
        public void preFlush(Iterator iterator) {
        }
       
        public void postFlush(Iterator iterator) {
                for (Iterator it = inserts.iterator(); it.hasNext();) {
                        Auditable entity = (Auditable)it.next();
                        Logger.logEvent(sessionFactory.getCurrentSession().connection());
                }
        }
}

Logger.java
@Component
public class Logger {
       
  @Autowired
  private static SessionFactory sessionFactory;
   
    public static void logEvent(Connection connection)
    {
            org.hibernate.classic.Session tempSession = sessionFactory.openSession(connection);

            try {
                    AuditLog auditLog = new AuditLog(
                                                        "me",
                                                        new Date(),
                                                        "me",
                                                        new Date());
 
                    tempSession.save(auditLog);
                    tempSession.flush();

            } finally {
                    tempSession.close();
            }
    }
}

hibernate-context.xml:
...
    <bean id="auditLogInterceptor" class="com.simon.hib.AuditLogInterceptor"></bean>
 
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="entityInterceptor" ref="auditLogInterceptor"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
       
    <tx:annotation-driven />
   
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
       
</beans>
       
       
StockController:
@Controller
public class StockController {
       
    @Autowired
    private StockService stockService;
 
    @RequestMapping(value = "/addstock", method = RequestMethod.GET)
    public String addStock() {
         
            Stock stock = new Stock();
            stock.setName("si");
            stockService.addStock(stock);
 
            return "redirect:/liststocks";
 
    }
}

StockServiceImpl.java:
@Service
public class StockServiceImpl implements StockService {

  @Autowired
  private StockDao stockDao;     
 
  @Transactional
  public void addStock(Stock stock) {
      System.out.println("UserServiceImpl: addStock "+stock.getName());


      stockDao.add(stock);
  }
}

Stock.java:
@Entity
@Table(name="STOCKS")
public class Stock implements Auditable {
   
    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;
 
    @Column(name="NAME")
    private String name;
 
    public Integer getId() {
        return id;
    }   
    public String getName() {
        return name;
    }
    public void setId(Integer id) {
        this.id = id;
    }   
    public void setName(String name) {
        this.name = name;
    }
}

AuditLog.java:
@Entity
@Table(name="AUDITLOGS")
public class AuditLog {
   
    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;
   
    @Column(name="DATEADDED")
    private Date dateAdded;
   
    @Column(name="DATEMODIFIED")
    private Date dateModified;
 
    @Column(name="WHOADDED")
    private String whoAdded;
   
    @Column(name="WHOMODIFIED")
    private String whoModified;
 
    public AuditLog( String whoAdded,
                            Date dateAdded,
                            String whoModified,
                            Date dateModified) {
           
            this.whoAdded = whoAdded;
            this.dateAdded = dateAdded;
            this.whoModified = whoModified;
            this.dateModified = dateModified;
           
    }
    public Integer getId() {
        return id;
    }   
    public String getWhoAdded() {
        return whoAdded;
    }
    @Temporal(TemporalType.TIMESTAMP)
    public Date getDateAdded() {
        return dateAdded;
    }
    public String getWhoModified() {
        return whoModified;
    }
    @Temporal(TemporalType.TIMESTAMP)
    public Date getDateModified() {
        return dateModified;
    }
    public void setId(Integer id) {
        this.id = id;
    }   
    public void setDateAdded(Date dateAdded) {
        this.dateAdded = dateAdded;
    }
    public void setWhoAdded(String whoAdded) {
        this.whoAdded = whoAdded;
    }
    public void setDateModified(Date dateModified) {
        this.dateModified = dateModified;
    }
    public void setWhoModified(String whoModified) {
        this.whoModified = whoModified;
    }

}


Viewing all articles
Browse latest Browse all 297

Trending Articles