Example usage for org.hibernate.event.spi PostInsertEvent getState

List of usage examples for org.hibernate.event.spi PostInsertEvent getState

Introduction

In this page you can find the example usage for org.hibernate.event.spi PostInsertEvent getState.

Prototype

public Object[] getState() 

Source Link

Usage

From source file:es.logongas.ix3.dao.impl.rules.EventListenerImplRuleEngine.java

License:Apache License

@Override
public void onPostInsert(PostInsertEvent pie) {
    autowired();/* ww  w  . java2s .  c o  m*/
    EntityMode entityMode = pie.getPersister().getEntityMode();

    RuleContext ruleContext = new RuleContextImplNoPrincipal(pie.getEntity(), null);

    fireRules(ruleContext, pie.getPersister(), pie.getState(), entityMode, RuleGroupPredefined.PostInsert.class,
            RuleGroupPredefined.PostInsertOrUpdate.class, RuleGroupPredefined.PostInsertOrUpdateOrDelete.class);

}

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

License:Open Source License

@Override
public void onPostInsert(PostInsertEvent postInsertEvent) {

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

    AuditLogContext auditLogContext = AuditLogContextProvider.getAuditLogContext();

    /**//from   w  ww .  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("No audit log context setup for insert on entity: " + entity);
        return;
    }

    /**
     * We might save 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> insertRecord = new HashMap<>();
    if (entity instanceof IAuditLog) {
        insertRecord.put(IAuditLog.ENTITY, entity);
        insertRecord.put(IAuditLog.PRINCIPAL, new Long(1));
        auditLogContext.getInserts().add(insertRecord);
        ClassMetadata classMetadata = postInsertEvent.getSession().getSessionFactory()
                .getClassMetadata(entity.getClass());
        Type[] types = classMetadata.getPropertyTypes();

        auditLogContext.getInserts().addAll(this.relations(postInsertEvent.getState(), types,
                ((IAuditLog) entity).getId(), true, postInsertEvent.getSession(), entity));
    }
    postInsertEvent.getSession().getActionQueue()
            .registerProcess(new MARLOAuditBeforeTransactionCompletionProcess());

}