Example usage for org.hibernate.event.spi PostDeleteEvent getDeletedState

List of usage examples for org.hibernate.event.spi PostDeleteEvent getDeletedState

Introduction

In this page you can find the example usage for org.hibernate.event.spi PostDeleteEvent getDeletedState.

Prototype

public Object[] getDeletedState() 

Source Link

Usage

From source file:org.cgiar.ccafs.marlo.data.HibernateAuditLogListener.java

License:Open Source License

@Override
public void onPostDelete(PostDeleteEvent postDeleteEvent) {

    Object entity = postDeleteEvent.getEntity();
    LOG.debug("begin onPostDelete for Entity : " + entity);

    AuditLogContext auditLogContext = AuditLogContextProvider.getAuditLogContext();

    /**//from   w w w  .  j  a v a  2 s  .c o m
     * This is because our save(entity), update(entity) and delete(entity) methods on our DAOs should not
     * execute the listeners. There might be a better way to do this like conditionally active the listeners.
     */
    if (auditLogContext.getActionName() == null
            && CollectionUtils.isEmpty(auditLogContext.getRelationsNames())) {
        LOG.debug("auditLogContext has not been initiliazed for this request, so not audit logging");
        return;
    }

    /**
     * We might delete many entities on a single request, but we only want to do the audit logging on one.
     */
    if (!entity.getClass().getCanonicalName().equals(auditLogContext.getEntityCanonicalName())) {
        LOG.debug("Entity : " + entity + " , is not the entity we want to audit log");
        return;
    }

    HashMap<String, Object> deleteRecord = new HashMap<>();
    if (entity instanceof IAuditLog) {
        deleteRecord.put(IAuditLog.ENTITY, entity);
        deleteRecord.put(IAuditLog.PRINCIPAL, new Long(1));
        auditLogContext.getDeletes().add(deleteRecord);

        ClassMetadata classMetadata = postDeleteEvent.getSession().getSessionFactory()
                .getClassMetadata(entity.getClass());
        Type[] types = classMetadata.getPropertyTypes();
        auditLogContext.getDeletes().addAll(this.relations(postDeleteEvent.getDeletedState(), types,
                ((IAuditLog) entity).getId(), true, postDeleteEvent.getSession(), entity));
    }

    postDeleteEvent.getSession().getActionQueue()
            .registerProcess(new MARLOAuditBeforeTransactionCompletionProcess());
}