Example usage for org.hibernate.event.spi PreCollectionUpdateEvent getAffectedOwnerOrNull

List of usage examples for org.hibernate.event.spi PreCollectionUpdateEvent getAffectedOwnerOrNull

Introduction

In this page you can find the example usage for org.hibernate.event.spi PreCollectionUpdateEvent getAffectedOwnerOrNull.

Prototype

public Object getAffectedOwnerOrNull() 

Source Link

Document

Get the collection owner entity that is affected by this event.

Usage

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

License:Open Source License

public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
    try {/*from  www. ja v a2s . co m*/
        String entityName = event.getAffectedOwnerEntityName();

        if (auditConfiguration.getExtensionManager().getAuditableInformationProvider()
                .isAuditable(entityName)) {

            AuditProcess auditProcess = auditConfiguration.getAuditProcessManager().get(event.getSession());
            AuditWorkUnit workUnit = new UpdateCollectionAuditWorkUnit(entityName,
                    event.getAffectedOwnerIdOrNull(), event.getAffectedOwnerOrNull(), event.getCollection());

            auditProcess.addWorkUnit(workUnit);
        }
    } catch (RuntimeException e) {
        if (log.isErrorEnabled()) {
            log.error("RuntimeException occured during onPreUpdateCollection, will re-throw the exception", e);
        }
        throw e;
    }
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
    Object owner = event.getAffectedOwnerOrNull();
    if (DataPublishListener.handleExclude(owner))
        return;/*  w w w. j a  v  a 2s .c om*/

    CollectionEntry collectionEntry = event.getSession().getPersistenceContext()
            .getCollectionEntry(event.getCollection());

    String propertyName = collectionEntry.getRole().substring(
            collectionEntry.getLoadedPersister().getOwnerEntityPersister().getEntityName().length() + 1);

    Object change = getChange(collectionEntry.getLoadedPersister().getOwnerEntityPersister(),
            event.getSession(), event.getAffectedOwnerEntityName(), event.getAffectedOwnerIdOrNull(), owner);
    if (change == null || !(change instanceof Change))
        return;

    PersistentCollection newColl = event.getCollection();
    Serializable oldColl = collectionEntry.getSnapshot();

    if (oldColl == null && newColl.hasQueuedOperations()) {
        List<Object[]> added = new ArrayList<Object[]>();
        List<Object[]> removed = new ArrayList<Object[]>();
        List<Object[]> updated = new ArrayList<Object[]>();

        List<?> queuedOperations = get(newColl, "operationQueue", List.class);
        for (Object op : queuedOperations) {
            // Great !!
            if (op.getClass().getName().endsWith("$Add")) {
                added.add(new Object[] { get(op, "index"), get(op, "value") });
            } else if (op.getClass().getName().endsWith("$SimpleAdd")) {
                added.add(new Object[] { null, get(op, "value") });
            } else if (op.getClass().getName().endsWith("$Put")) {
                added.add(new Object[] { get(op, "index"), get(op, "value") });
            } else if (op.getClass().getName().endsWith("$Remove")) {
                removed.add(new Object[] { get(op, "index"), get(op, "old") });
            } else if (op.getClass().getName().endsWith("$SimpleRemove")) {
                removed.add(new Object[] { null, get(op, "value") });
            } else if (op.getClass().getName().endsWith("$Set")) {
                updated.add(new Object[] { get(op, "index"), get(op, "value") });
            }
        }

        CollectionChange[] collChanges = new CollectionChange[added.size() + removed.size() + updated.size()];
        int idx = 0;
        for (Object[] obj : added)
            collChanges[idx++] = new CollectionChange(1, obj[0], obj[1]);

        for (Object[] obj : removed) {
            Object value = obj[1];
            if (value != null && value.getClass().isAnnotationPresent(Entity.class)) {
                org.granite.util.Entity e = new org.granite.util.Entity(value);
                value = new ChangeRef(e.getName(), (String) get(value, "uid"),
                        (Serializable) e.getIdentifier());
            }
            collChanges[idx++] = new CollectionChange(-1, obj[0], value);
        }

        for (Object[] obj : updated)
            collChanges[idx++] = new CollectionChange(0, obj[0], obj[1]);

        ((Change) change).addCollectionChanges(propertyName, collChanges);
    } else if (oldColl != null && newColl instanceof List<?>) {
        List<?> oldSnapshot = (List<?>) oldColl;
        List<?> newList = (List<?>) newColl;

        List<Object[]> ops = Utils.diffLists(oldSnapshot, newList);

        CollectionChange[] collChanges = new CollectionChange[ops.size()];
        int idx = 0;
        for (Object[] obj : ops)
            collChanges[idx++] = new CollectionChange((Integer) obj[0], obj[1], obj[2]);

        ((Change) change).addCollectionChanges(propertyName, collChanges);
    } else if (oldColl != null && newColl instanceof Collection<?>) {
        Map<?, ?> oldSnapshot = (Map<?, ?>) oldColl;

        Set<Object> added = new HashSet<Object>();
        added.addAll((Collection<?>) newColl);
        added.removeAll(new HashSet<Object>(oldSnapshot.keySet()));

        Set<Object> removed = new HashSet<Object>();
        removed.addAll(new HashSet<Object>(oldSnapshot.keySet()));
        removed.removeAll((Collection<?>) newColl);

        CollectionChange[] collChanges = new CollectionChange[added.size() + removed.size()];
        int idx = 0;
        for (Object obj : added)
            collChanges[idx++] = new CollectionChange(1, null, obj);

        for (Object obj : removed)
            collChanges[idx++] = new CollectionChange(-1, null, obj);

        ((Change) change).addCollectionChanges(propertyName, collChanges);
    } else if (oldColl != null && newColl instanceof Map<?, ?>) {
        Map<?, ?> oldSnapshot = (Map<?, ?>) oldColl;

        Set<Entry<Object, Object>> added = new HashSet<Entry<Object, Object>>();
        added.addAll(((Map<Object, Object>) newColl).entrySet());
        added.removeAll(new HashMap<Object, Object>(oldSnapshot).entrySet());

        Set<Entry<Object, Object>> removed = new HashSet<Entry<Object, Object>>();
        removed.addAll(new HashMap<Object, Object>(oldSnapshot).entrySet());
        removed.removeAll(((Map<Object, Object>) newColl).entrySet());

        CollectionChange[] collChanges = new CollectionChange[added.size() + removed.size()];
        int idx = 0;
        for (Entry<?, ?> me : added)
            collChanges[idx++] = new CollectionChange(1, me.getKey(), me.getValue());

        for (Entry<?, ?> me : removed)
            collChanges[idx++] = new CollectionChange(-1, me.getKey(), me.getValue());

        ((Change) change).addCollectionChanges(propertyName, collChanges);
    }
}