Example usage for org.hibernate.collection.spi PersistentCollection getStoredSnapshot

List of usage examples for org.hibernate.collection.spi PersistentCollection getStoredSnapshot

Introduction

In this page you can find the example usage for org.hibernate.collection.spi PersistentCollection getStoredSnapshot.

Prototype

Serializable getStoredSnapshot();

Source Link

Document

Get the snapshot cached by the collection instance

Usage

From source file:com.blazebit.security.impl.interceptor.ChangeInterceptor.java

License:Apache License

/**
 * /*from w  w w .  j av  a  2 s  .co  m*/
 */
@Override
public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException {
    if (!EntityFeatures.isInterceptorActive()) {
        super.onCollectionUpdate(collection, key);
        return;
    }
    if (collection instanceof PersistentCollection) {
        PersistentCollection newValuesCollection = (PersistentCollection) collection;
        Object entity = newValuesCollection.getOwner();
        if (AnnotationUtils.findAnnotation(entity.getClass(), EntityResourceType.class) == null) {
            super.onCollectionUpdate(collection, key);
            return;
        }
        // copy new values and old values
        @SuppressWarnings({ "unchecked", "rawtypes" })
        Collection<?> newValues = new HashSet((Collection<?>) newValuesCollection.getValue());
        @SuppressWarnings({ "unchecked", "rawtypes" })
        Set<?> oldValues = new HashSet(((Map<?, ?>) newValuesCollection.getStoredSnapshot()).keySet());

        String fieldName = StringUtils.replace(newValuesCollection.getRole(), entity.getClass().getName() + ".",
                "");
        UserContext userContext = BeanProvider.getContextualReference(UserContext.class);
        ActionFactory actionFactory = BeanProvider.getContextualReference(ActionFactory.class);
        EntityResourceFactory resourceFactory = BeanProvider
                .getContextualReference(EntityResourceFactory.class);
        PermissionService permissionService = BeanProvider.getContextualReference(PermissionService.class);

        // find all objects that were added
        boolean isGrantedToAdd = true;
        boolean isGrantedToRemove = true;

        @SuppressWarnings({ "unchecked", "rawtypes" })
        Set<?> retained = new HashSet(oldValues);
        retained.retainAll(newValues);

        oldValues.removeAll(retained);
        // if there is a difference between oldValues and newValues
        if (!oldValues.isEmpty()) {
            // if something remained
            isGrantedToRemove = permissionService.isGranted(actionFactory.createAction(Action.REMOVE),
                    resourceFactory.createResource(entity, fieldName));
        }
        newValues.removeAll(retained);
        if (!newValues.isEmpty()) {
            isGrantedToAdd = permissionService.isGranted(actionFactory.createAction(Action.ADD),
                    resourceFactory.createResource(entity, fieldName));
        }

        if (!isGrantedToAdd) {
            throw new PermissionActionException("Element cannot be added to entity " + entity + "'s collection "
                    + fieldName + " by " + userContext.getUser());
        } else {
            if (!isGrantedToRemove) {
                throw new PermissionActionException("Element cannot be removed from entity " + entity
                        + "'s collection " + fieldName + " by " + userContext.getUser());
            } else {
                super.onCollectionUpdate(collection, key);
                return;
            }
        }
    } else {
        // not a persistent collection?
    }
}

From source file:com.googlecode.hibernate.audit.synchronization.work.UpdateCollectionAuditWorkUnit.java

License:Open Source License

public UpdateCollectionAuditWorkUnit(String entityName, Serializable id, Object entity,
        PersistentCollection persistentCollection) {
    this.entityName = entityName;
    this.id = id;
    this.entity = entity;
    this.persistentCollection = persistentCollection;
    this.snapshot = persistentCollection.getStoredSnapshot();
}

From source file:de.micromata.genome.db.jpa.xmldump.impl.HibernateCollectionConverter.java

License:Apache License

/**
 * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
 *      com.thoughtworks.xstream.io.HierarchicalStreamWriter, com.thoughtworks.xstream.converters.MarshallingContext)
 *///from   ww w  .  j  ava2  s . c o m
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    Object collection = source;

    if (source instanceof PersistentCollection) {
        PersistentCollection col = (PersistentCollection) source;
        col.forceInitialization();
        // ToDo ES: collection = col.getCollectionSnapshot().getSnapshot();
        collection = col.getStoredSnapshot();
    }

    // the set is returned as a map by Hibernate (unclear why exactly)
    if (source instanceof PersistentSortedSet) {
        collection = new TreeSet(((HashMap) collection).values());
    } else if (source instanceof PersistentSet) {
        // collection = new HashSet(((HashMap)collection).entrySet());
        collection = new HashSet(((HashMap) collection).values());
    }

    // delegate the collection to the approapriate converter
    if (listSetConverter.canConvert(collection.getClass())) {
        listSetConverter.marshal(collection, writer, context);
        return;
    }
    if (mapConverter.canConvert(collection.getClass())) {
        mapConverter.marshal(collection, writer, context);
        return;
    }
    if (treeMapConverter.canConvert(collection.getClass())) {
        treeMapConverter.marshal(collection, writer, context);
        return;
    }
    if (treeSetConverter.canConvert(collection.getClass())) {
        treeSetConverter.marshal(collection, writer, context);
        return;
    }

    defaultConverter.marshal(collection, writer, context);
}

From source file:org.granite.hibernate4.HibernateExternalizer.java

License:Open Source License

protected AbstractExternalizablePersistentCollection newExternalizableCollection(PersistentCollection value) {
    final boolean initialized = Hibernate.isInitialized(value);
    final boolean dirty = value.isDirty();

    AbstractExternalizablePersistentCollection coll = null;

    if (value instanceof PersistentSet)
        coll = new ExternalizablePersistentSet(initialized ? (Set<?>) value : null, initialized, dirty);
    else if (value instanceof PersistentList)
        coll = new ExternalizablePersistentList(initialized ? (List<?>) value : null, initialized, dirty);
    else if (value instanceof PersistentBag)
        coll = new ExternalizablePersistentBag(initialized ? (List<?>) value : null, initialized, dirty);
    else if (value instanceof PersistentMap)
        coll = new ExternalizablePersistentMap(initialized ? (Map<?, ?>) value : null, initialized, dirty);
    else/* w ww. ja v  a 2  s. com*/
        throw new UnsupportedOperationException("Unsupported Hibernate collection type: " + value);

    if (serializeMetadata != SerializeMetadata.NO
            && (serializeMetadata == SerializeMetadata.YES || !initialized) && value.getRole() != null) {
        char[] hexKey = StringUtil.bytesToHexChars(serializeSerializable(value.getKey()));
        char[] hexSnapshot = StringUtil.bytesToHexChars(serializeSerializable(value.getStoredSnapshot()));
        String metadata = new StringBuilder(
                hexKey.length + 1 + hexSnapshot.length + 1 + value.getRole().length()).append(hexKey)
                        .append(':').append(hexSnapshot).append(':').append(value.getRole()).toString();
        coll.setMetadata(metadata);
    }

    return coll;
}