Example usage for org.hibernate.event.spi PostUpdateEvent getSession

List of usage examples for org.hibernate.event.spi PostUpdateEvent getSession

Introduction

In this page you can find the example usage for org.hibernate.event.spi PostUpdateEvent getSession.

Prototype

public final EventSource getSession() 

Source Link

Document

Returns the session event source for this event.

Usage

From source file:at.molindo.esi4j.module.hibernate.HibernateEventListener.java

License:Apache License

@Override
public void onPostUpdate(PostUpdateEvent event) {
    EventSession eventSession = findEventSession(event.getSession());
    if (eventSession != null) {
        eventSession.onPostUpdate(event.getEntity());
    } else {//from   ww w  .j av  a  2 s  .c om
        _batchedEventProcessor.onPostUpdate(event.getEntity());
    }
}

From source file:com.corundumstudio.hibernate.dsc.QueryCacheEntityListener.java

License:Apache License

@Override
public void onPostUpdate(PostUpdateEvent event) {
    Set<Entry<String, QueryListenerEntry>> values = getValue(event.getPersister());
    for (Entry<String, QueryListenerEntry> entry : values) {
        UpdateCommand command = new UpdateCommand();
        CacheCallback handler = entry.getValue().getHandler();
        handler.commonParams(command, event.getEntity());
        handler.onUpdate(command, event.getEntity());

        DynamicQueryCache queryCache = getQueryCache(event.getPersister(), entry.getKey(), entry.getValue());
        if (command.isAddResult() || command.isUniqueResult()) {
            addResult(queryCache, entry.getValue(), command, event.getSession());
        }/*from ww  w  .  j a  v a2s  .  c  o m*/
        if (command.isRemoveResult()) {
            removeResult(queryCache, entry.getValue(), command, event.getSession());
        }
    }
}

From source file:com.googlecode.hibernate.audit.listener.AuditListener.java

License:Open Source License

public void onPostUpdate(PostUpdateEvent event) {
    try {//from  www.j a va  2  s. c o m
        String entityName = event.getPersister().getEntityName();

        if (auditConfiguration.getExtensionManager().getAuditableInformationProvider()
                .isAuditable(entityName)) {
            AuditProcess auditProcess = auditConfiguration.getAuditProcessManager().get(event.getSession());

            AuditWorkUnit workUnit = new UpdateAuditWorkUnit(entityName, event.getId(), event.getEntity(),
                    event.getPersister(), event.getOldState(), event.getState());
            auditProcess.addWorkUnit(workUnit);
        }
    } catch (RuntimeException e) {
        if (log.isErrorEnabled()) {
            log.error("RuntimeException occured during onPostUpdate, will re-throw the exception", e);
        }
        throw e;
    }
}

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

License:Open Source License

@Override
public void onPostUpdate(PostUpdateEvent postUpdateEvent) {
    Object entity = postUpdateEvent.getEntity();

    LOG.debug("begin onPostUpdatefor Entity : " + entity);

    AuditLogContext auditLogContext = AuditLogContextProvider.getAuditLogContext();

    /**//from w w w  . j a v a 2s .co 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 activate the listeners.
     */
    if (auditLogContext.getActionName() == null
            && CollectionUtils.isEmpty(auditLogContext.getRelationsNames())) {
        LOG.debug("No audit log context setup for update 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> updateRecord = new HashMap<>();
    if (entity instanceof IAuditLog) {
        IAuditLog iAuditLog = (IAuditLog) entity;
        String name = iAuditLog.getClass().getName();
        Class<?> className = null;
        try {
            className = Class.forName(name);
        } catch (ClassNotFoundException e) {

        }
        Object obj = postUpdateEvent.getSession().get(className, iAuditLog.getId());

        ClassMetadata classMetadata = postUpdateEvent.getSession().getSessionFactory()
                .getClassMetadata(obj.getClass());
        Type[] types = classMetadata.getPropertyTypes();

        String[] propertyNamesRelation = classMetadata.getPropertyNames();
        Phase phaseObject = null;
        boolean hasPhase = false;
        for (String nameAtrribute : propertyNamesRelation) {
            if (nameAtrribute.equals("phase")) {
                try {
                    phaseObject = (Phase) classMetadata.getPropertyValue(entity, nameAtrribute);
                } catch (Exception e) {
                    // The attribute is not a Phase Model Class
                    phaseObject = null;
                }
                if (phaseObject != null) {
                    phaseObject = (Phase) postUpdateEvent.getSession().get(Phase.class, phaseObject.getId());
                    if (phaseObject != null) {
                        hasPhase = true;
                    }
                }
            }
        }
        /*
         * if have phase and the phase is the current we are checking , we load the info
         */
        if (hasPhase || entity instanceof Project || entity instanceof FundingSource
                || entity instanceof Deliverable || entity instanceof ProjectOutcome
                || entity instanceof CrpProgram || entity instanceof ProjectExpectedStudy
                || entity instanceof ProjectInnovation) {
            if (hasPhase && (entity instanceof Deliverable == false)) {
                if (AuditLogContextProvider.getAuditLogContext().getPhase().equals(phaseObject)) {
                    updateRecord.put(IAuditLog.ENTITY, entity);
                    updateRecord.put(IAuditLog.PRINCIPAL, new Long(1));
                    auditLogContext.getUpdates().add(updateRecord);

                    auditLogContext.getUpdates().addAll(this.relations(postUpdateEvent.getState(), types,
                            ((IAuditLog) entity).getId(), true, postUpdateEvent.getSession(), obj));
                    postUpdateEvent.getSession().getActionQueue()
                            .registerProcess(new MARLOAuditBeforeTransactionCompletionProcess());

                }
            } else {
                updateRecord.put(IAuditLog.ENTITY, entity);
                updateRecord.put(IAuditLog.PRINCIPAL, new Long(1));
                auditLogContext.getUpdates().add(updateRecord);

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

        }

        // LOG.debug("COMPARE LOGS WITH STAGING BRANCH: " + auditLogContext.getUpdates().toString());

    }
}

From source file:org.granite.tide.hibernate4.HibernateDataChangePublishListener.java

License:Open Source License

public void onPostUpdate(PostUpdateEvent event) {
    if (DataPublishListener.handleExclude(event.getEntity()))
        return;/*from   ww w.ja v  a  2 s .c om*/

    if (event.getDirtyProperties() != null && event.getDirtyProperties().length > 0) {
        Object change = getChange(event.getPersister(), event.getSession(),
                event.getPersister().getEntityName(), event.getId(), event.getEntity());
        if (change instanceof Change) {
            for (int i = 0; i < event.getDirtyProperties().length; i++) {
                int pidx = event.getDirtyProperties()[i];
                String pname = event.getPersister().getPropertyNames()[pidx];
                if (AnnotationUtils.isAnnotatedWith(event.getEntity(), pname, ExcludeFromDataPublish.class))
                    continue;

                ((Change) change).getChanges().put(pname, event.getState()[pidx]);
            }
        } else if (change == null)
            DataContext.addUpdate(EntityUpdateType.UPDATE, event.getEntity(), event.getEntity());
    }
}