Search in sources :

Example 1 with MapViewToEntityMapper

use of com.blazebit.persistence.view.impl.entity.MapViewToEntityMapper in project blaze-persistence by Blazebit.

the class MapRemoveAllEntriesAction method doAction.

@Override
@SuppressWarnings("unchecked")
public void doAction(C map, UpdateContext context, MapViewToEntityMapper mapper, CollectionRemoveListener keyRemoveListener, CollectionRemoveListener valueRemoveListener) {
    if (mapper != null) {
        Collection<Map.Entry<K, V>> entrySet = map.entrySet();
        ViewToEntityMapper keyMapper = mapper.getKeyMapper();
        ViewToEntityMapper valueMapper = mapper.getValueMapper();
        if (elements.size() == 1) {
            Map.Entry<K, V> entry = elements.iterator().next();
            K k = entry.getKey();
            V v = entry.getValue();
            if (keyMapper != null) {
                k = (K) keyMapper.applyToEntity(context, null, k);
            }
            if (valueMapper != null) {
                v = (V) valueMapper.applyToEntity(context, null, v);
            }
            Map.Entry<K, V> e = new AbstractMap.SimpleEntry<K, V>(k, v);
            if (entrySet.remove(e)) {
                if (keyRemoveListener != null && k != null) {
                    keyRemoveListener.onCollectionRemove(context, entry.getKey());
                }
                if (valueRemoveListener != null && v != null) {
                    valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(entry.getKey()));
                }
            }
        } else {
            if (keyMapper == null) {
                if (valueMapper == null) {
                    for (Map.Entry<? extends K, ? extends V> entry : elements) {
                        K k = entry.getKey();
                        V v = entry.getValue();
                        if (entrySet.remove(entry)) {
                            if (keyRemoveListener != null && k != null) {
                                keyRemoveListener.onCollectionRemove(context, entry.getKey());
                            }
                            if (valueRemoveListener != null && v != null) {
                                valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(entry.getKey()));
                            }
                        }
                    }
                } else {
                    List<V> entities = new ArrayList<>(elements.size());
                    for (Map.Entry<K, V> entry : elements) {
                        entities.add(entry.getValue());
                    }
                    valueMapper.applyAll(context, (List<Object>) entities);
                    int i = 0;
                    for (Map.Entry<? extends K, ? extends V> entry : elements) {
                        K k = entry.getKey();
                        V v = entities.get(i++);
                        Map.Entry<K, V> e = new AbstractMap.SimpleEntry<K, V>(k, v);
                        if (entrySet.remove(e)) {
                            if (keyRemoveListener != null && k != null) {
                                keyRemoveListener.onCollectionRemove(context, entry.getKey());
                            }
                            if (valueRemoveListener != null && v != null) {
                                valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(entry.getKey()));
                            }
                        }
                    }
                }
            } else if (valueMapper == null) {
                List<K> entities = new ArrayList<>(elements.size());
                for (Map.Entry<K, V> entry : elements) {
                    entities.add(entry.getKey());
                }
                keyMapper.applyAll(context, (List<Object>) entities);
                int i = 0;
                for (Map.Entry<? extends K, ? extends V> entry : elements) {
                    K k = entities.get(i++);
                    V v = entry.getValue();
                    Map.Entry<K, V> e = new AbstractMap.SimpleEntry<K, V>(k, v);
                    if (entrySet.remove(e)) {
                        if (keyRemoveListener != null && k != null) {
                            keyRemoveListener.onCollectionRemove(context, entry.getKey());
                        }
                        if (valueRemoveListener != null && v != null) {
                            valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(entry.getKey()));
                        }
                    }
                }
            } else {
                List<K> keyEntities = new ArrayList<>(elements.size());
                List<V> valueEntities = new ArrayList<>(elements.size());
                for (Map.Entry<? extends K, ? extends V> entry : elements) {
                    keyEntities.add(entry.getKey());
                    valueEntities.add(entry.getValue());
                }
                keyMapper.applyAll(context, (List<Object>) keyEntities);
                valueMapper.applyAll(context, (List<Object>) valueEntities);
                int i = 0;
                for (Map.Entry<? extends K, ? extends V> entry : elements) {
                    K k = keyEntities.get(i);
                    V v = valueEntities.get(i++);
                    Map.Entry<K, V> e = new AbstractMap.SimpleEntry<K, V>(k, v);
                    if (entrySet.remove(e)) {
                        if (keyRemoveListener != null && k != null) {
                            keyRemoveListener.onCollectionRemove(context, entry.getKey());
                        }
                        if (valueRemoveListener != null && v != null) {
                            valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(entry.getKey()));
                        }
                    }
                }
            }
        }
    } else {
        if (map.size() > 0 && (keyRemoveListener != null || valueRemoveListener != null)) {
            Collection<Map.Entry<K, V>> entrySet = map.entrySet();
            for (Map.Entry<? extends K, ? extends V> e : elements) {
                if (entrySet.remove(e)) {
                    K k = e.getKey();
                    V v = e.getValue();
                    if (keyRemoveListener != null && k != null) {
                        keyRemoveListener.onCollectionRemove(context, k);
                    }
                    if (valueRemoveListener != null && v != null) {
                        valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(k));
                    }
                }
            }
        } else {
            map.entrySet().removeAll(elements);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AbstractMap(java.util.AbstractMap) List(java.util.List) ArrayList(java.util.ArrayList) MapViewToEntityMapper(com.blazebit.persistence.view.impl.entity.MapViewToEntityMapper) ViewToEntityMapper(com.blazebit.persistence.view.impl.entity.ViewToEntityMapper) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 2 with MapViewToEntityMapper

use of com.blazebit.persistence.view.impl.entity.MapViewToEntityMapper in project blaze-persistence by Blazebit.

the class MapPutAllAction method doAction.

@Override
@SuppressWarnings("unchecked")
public void doAction(C map, UpdateContext context, MapViewToEntityMapper mapper, CollectionRemoveListener keyRemoveListener, CollectionRemoveListener valueRemoveListener) {
    if (mapper != null) {
        ViewToEntityMapper keyMapper = mapper.getKeyMapper();
        ViewToEntityMapper valueMapper = mapper.getValueMapper();
        if (elements.size() == 1) {
            Map.Entry<? extends K, ? extends V> e = elements.entrySet().iterator().next();
            K k = e.getKey();
            V v = e.getValue();
            if (keyMapper != null) {
                k = (K) keyMapper.applyToEntity(context, null, k);
            }
            if (valueMapper != null) {
                v = (V) valueMapper.applyToEntity(context, null, v);
            }
            V oldValue = map.put(k, v);
            if (valueRemoveListener != null && oldValue != null) {
                valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(e.getKey()));
            }
        } else {
            if (keyMapper == null) {
                if (valueMapper == null) {
                    for (Map.Entry<? extends K, ? extends V> e : elements.entrySet()) {
                        V oldValue = map.put(e.getKey(), e.getValue());
                        if (valueRemoveListener != null && oldValue != null) {
                            valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(e.getKey()));
                        }
                    }
                } else {
                    List<V> entities = new ArrayList<>(elements.size());
                    entities.addAll(elements.values());
                    valueMapper.applyAll(context, (List<Object>) entities);
                    int i = 0;
                    for (Map.Entry<? extends K, ? extends V> e : elements.entrySet()) {
                        V value = entities.get(i++);
                        V oldValue = map.put(e.getKey(), value);
                        if (valueRemoveListener != null && oldValue != null) {
                            valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(e.getKey()));
                        }
                    }
                }
            } else if (valueMapper == null) {
                List<K> entities = new ArrayList<>(elements.size());
                entities.addAll(elements.keySet());
                keyMapper.applyAll(context, (List<Object>) entities);
                int i = 0;
                for (Map.Entry<? extends K, ? extends V> e : elements.entrySet()) {
                    K key = entities.get(i++);
                    V oldValue = map.put(key, e.getValue());
                    if (valueRemoveListener != null && oldValue != null) {
                        valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(e.getKey()));
                    }
                }
            } else {
                List<K> keyEntities = new ArrayList<>(elements.size());
                List<V> valueEntities = new ArrayList<>(elements.size());
                for (Map.Entry<? extends K, ? extends V> entry : elements.entrySet()) {
                    keyEntities.add(entry.getKey());
                    valueEntities.add(entry.getValue());
                }
                keyMapper.applyAll(context, (List<Object>) keyEntities);
                valueMapper.applyAll(context, (List<Object>) valueEntities);
                int i = 0;
                for (Map.Entry<? extends K, ? extends V> e : elements.entrySet()) {
                    K key = keyEntities.get(i);
                    V value = valueEntities.get(i++);
                    V oldValue = map.put(key, value);
                    if (valueRemoveListener != null && oldValue != null) {
                        valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(e.getKey()));
                    }
                }
            }
        }
    } else {
        if (map.size() > 0 && valueRemoveListener != null) {
            for (Map.Entry<? extends K, ? extends V> e : elements.entrySet()) {
                V oldValue = map.put(e.getKey(), e.getValue());
                if (oldValue != null) {
                    valueRemoveListener.onCollectionRemove(context, removedObjectsInView.get(e.getKey()));
                }
            }
        } else {
            map.putAll(elements);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) MapViewToEntityMapper(com.blazebit.persistence.view.impl.entity.MapViewToEntityMapper) ViewToEntityMapper(com.blazebit.persistence.view.impl.entity.ViewToEntityMapper) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with MapViewToEntityMapper

use of com.blazebit.persistence.view.impl.entity.MapViewToEntityMapper in project blaze-persistence by Blazebit.

the class EntityViewUpdaterImpl method createPluralAttributeFlusher.

private DirtyAttributeFlusher<?, ?, ?> createPluralAttributeFlusher(EntityViewManagerImpl evm, Map<Object, EntityViewUpdaterImpl> localCache, ManagedViewTypeImplementor<?> viewType, String idAttributeName, FlushStrategy flushStrategy, AbstractMethodAttribute<?, ?> attribute, DirtyAttributeFlusher<?, ?, ?> ownerIdFlusher, EntityViewUpdaterImpl owner, String ownerMapping) {
    EntityMetamodel entityMetamodel = evm.getMetamodel().getEntityMetamodel();
    Class<?> entityClass = viewType.getEntityClass();
    ExtendedManagedType managedType = entityMetamodel.getManagedType(ExtendedManagedType.class, entityClass);
    String attributeName = attribute.getName();
    String attributeMapping = attribute.getMapping();
    AttributeAccessor entityAttributeAccessor = Accessors.forEntityMapping(evm, attribute);
    boolean cascadeDelete = attribute.isDeleteCascaded();
    boolean viewOnlyDeleteCascaded = cascadeDelete && !managedType.getAttribute(attributeMapping).isDeleteCascaded();
    boolean optimisticLockProtected = attribute.isOptimisticLockProtected();
    JpaProvider jpaProvider = evm.getJpaProvider();
    PluralAttribute<?, ?, ?> pluralAttribute = (PluralAttribute<?, ?, ?>) attribute;
    InitialValueAttributeAccessor viewAttributeAccessor = Accessors.forMutableViewAttribute(evm, attribute);
    ManagedType<?> ownerManagedType = owner == null ? viewType.getJpaManagedType() : owner.managedViewType.getJpaManagedType();
    EntityType<?> ownerEntityType = ownerManagedType instanceof EntityType<?> ? (EntityType<?>) ownerManagedType : null;
    DirtyAttributeFlusher<?, ?, ?> attributeOwnerFlusher;
    if (attributeMapping == null || ownerEntityType == null) {
        // In case of cascading only attributes i.e. correlations, we use the owner id flusher as it's not updatable anyway
        attributeOwnerFlusher = ownerIdFlusher;
    } else {
        Map<String, String> joinTableOwnerProperties;
        if (ownerMapping == null) {
            joinTableOwnerProperties = jpaProvider.getJoinMappingPropertyNames(ownerEntityType, ownerMapping, attributeMapping);
        } else {
            joinTableOwnerProperties = jpaProvider.getJoinMappingPropertyNames(ownerEntityType, ownerMapping, ownerMapping + "." + attributeMapping);
        }
        if (joinTableOwnerProperties.size() != 1) {
            String idMapping = ownerIdFlusher.getMapping();
            String prefix;
            if (idMapping.endsWith(".")) {
                prefix = idMapping;
            } else {
                prefix = idMapping + ".";
            }
            for (String joinTableOwnerProperty : joinTableOwnerProperties.keySet()) {
                if (!joinTableOwnerProperty.startsWith(prefix)) {
                    throw new IllegalArgumentException("Multiple joinable owner properties for attribute '" + attributeName + "' of " + ownerEntityType.getJavaType().getName() + " found which is not yet supported. Consider using the primary key instead!");
                }
            }
            attributeOwnerFlusher = ownerIdFlusher;
        } else if (ownerIdFlusher.getMapping().equals(joinTableOwnerProperties.values().iterator().next())) {
            attributeOwnerFlusher = ownerIdFlusher;
        } else {
            attributeOwnerFlusher = findSingularAttributeFlusherByMapping(evm, localCache, owner, viewType, attributeName, joinTableOwnerProperties.keySet().iterator().next());
        }
    }
    TypeDescriptor elementDescriptor = TypeDescriptor.forType(evm, localCache, this, attribute, pluralAttribute.getElementType(), owner, ownerMapping);
    boolean collectionUpdatable = attribute.isUpdatable();
    CollectionRemoveListener elementRemoveListener = createOrphanRemoveListener(attribute, elementDescriptor);
    CollectionRemoveListener elementCascadeDeleteListener = createCascadeDeleteListener(attribute, elementDescriptor);
    boolean jpaProviderDeletesCollection;
    boolean supportsCollectionDml = jpaProvider.supportsInsertStatement();
    if (elementDescriptor.getAttributeIdAttributeName() != null) {
        jpaProviderDeletesCollection = jpaProvider.supportsJoinTableCleanupOnDelete();
    } else {
        jpaProviderDeletesCollection = jpaProvider.supportsCollectionTableCleanupOnDelete();
    }
    if (attribute instanceof MapAttribute<?, ?, ?>) {
        MapAttribute<?, ?, ?> mapAttribute = (MapAttribute<?, ?, ?>) attribute;
        TypeDescriptor keyDescriptor = TypeDescriptor.forType(evm, localCache, this, attribute, mapAttribute.getKeyType(), owner, ownerMapping);
        // TODO: currently there is no possibility to set this
        CollectionRemoveListener keyRemoveListener = null;
        CollectionRemoveListener keyCascadeDeleteListener = null;
        if (collectionUpdatable || keyDescriptor.shouldFlushMutations() || elementDescriptor.shouldFlushMutations() || shouldPassThrough(evm, viewType, attribute)) {
            MapViewToEntityMapper mapper = new SimpleMapViewToEntityMapper(keyDescriptor.getViewToEntityMapper(), elementDescriptor.getViewToEntityMapper());
            MapViewToEntityMapper loadOnlyMapper = new SimpleMapViewToEntityMapper(keyDescriptor.getLoadOnlyViewToEntityMapper(), elementDescriptor.getLoadOnlyViewToEntityMapper());
            MapInstantiatorImplementor<?, ?> mapInstantiator = attribute.getMapInstantiator();
            return new MapAttributeFlusher<Object, RecordingMap<Map<?, ?>, ?, ?>>(attributeName, attributeMapping, owner == null ? entityClass : owner.fullEntityLoader.getEntityClass(), idAttributeName, ownerMapping, attributeOwnerFlusher, createPluralAttributeSubFlusher(evm, localCache, viewType, attribute, "element", mapAttribute.getElementType(), owner, ownerMapping), supportsCollectionDml, flushStrategy, entityAttributeAccessor, viewAttributeAccessor, optimisticLockProtected, collectionUpdatable, keyCascadeDeleteListener, elementCascadeDeleteListener, keyRemoveListener, elementRemoveListener, viewOnlyDeleteCascaded, jpaProviderDeletesCollection, keyDescriptor, elementDescriptor, mapper, loadOnlyMapper, mapInstantiator);
        } else {
            return null;
        }
    } else {
        if (collectionUpdatable || elementDescriptor.shouldFlushMutations() || shouldPassThrough(evm, viewType, attribute)) {
            InverseFlusher<Object> inverseFlusher = InverseFlusher.forAttribute(evm, localCache, viewType, attribute, elementDescriptor, owner, ownerMapping);
            InverseRemoveStrategy inverseRemoveStrategy = attribute.getInverseRemoveStrategy();
            CollectionInstantiatorImplementor<?, ?> collectionInstantiator = attribute.getCollectionInstantiator();
            if (pluralAttribute.isIndexed()) {
                return new IndexedListAttributeFlusher<Object, RecordingList<List<?>>>(attributeName, attributeMapping, owner == null ? entityClass : owner.fullEntityLoader.getEntityClass(), idAttributeName, ownerMapping, attributeOwnerFlusher, createPluralAttributeSubFlusher(evm, localCache, viewType, attribute, "element", pluralAttribute.getElementType(), owner, ownerMapping), supportsCollectionDml, flushStrategy, entityAttributeAccessor, viewAttributeAccessor, optimisticLockProtected, collectionUpdatable, viewOnlyDeleteCascaded, jpaProviderDeletesCollection, elementCascadeDeleteListener, elementRemoveListener, collectionInstantiator, elementDescriptor, inverseFlusher, inverseRemoveStrategy);
            } else {
                return new CollectionAttributeFlusher(attributeName, attributeMapping, owner == null ? entityClass : owner.fullEntityLoader.getEntityClass(), idAttributeName, ownerMapping, attributeOwnerFlusher, createPluralAttributeSubFlusher(evm, localCache, viewType, attribute, "element", pluralAttribute.getElementType(), owner, ownerMapping), supportsCollectionDml, flushStrategy, entityAttributeAccessor, viewAttributeAccessor, optimisticLockProtected, collectionUpdatable, viewOnlyDeleteCascaded, jpaProviderDeletesCollection, elementCascadeDeleteListener, elementRemoveListener, collectionInstantiator, elementDescriptor, inverseFlusher, inverseRemoveStrategy);
            }
        } else {
            return null;
        }
    }
}
Also used : InitialValueAttributeAccessor(com.blazebit.persistence.view.impl.accessor.InitialValueAttributeAccessor) MapAttributeFlusher(com.blazebit.persistence.view.impl.update.flush.MapAttributeFlusher) InverseRemoveStrategy(com.blazebit.persistence.view.InverseRemoveStrategy) CollectionRemoveListener(com.blazebit.persistence.view.impl.collection.CollectionRemoveListener) ViewCollectionRemoveListener(com.blazebit.persistence.view.impl.update.flush.ViewCollectionRemoveListener) EntityCollectionRemoveListener(com.blazebit.persistence.view.impl.update.flush.EntityCollectionRemoveListener) IndexedListAttributeFlusher(com.blazebit.persistence.view.impl.update.flush.IndexedListAttributeFlusher) List(java.util.List) RecordingList(com.blazebit.persistence.view.impl.collection.RecordingList) ArrayList(java.util.ArrayList) CollectionAttributeFlusher(com.blazebit.persistence.view.impl.update.flush.CollectionAttributeFlusher) JpaProvider(com.blazebit.persistence.spi.JpaProvider) PluralAttribute(com.blazebit.persistence.view.metamodel.PluralAttribute) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) MapAttribute(com.blazebit.persistence.view.metamodel.MapAttribute) TypeDescriptor(com.blazebit.persistence.view.impl.update.flush.TypeDescriptor) SimpleMapViewToEntityMapper(com.blazebit.persistence.view.impl.update.flush.SimpleMapViewToEntityMapper) MapViewToEntityMapper(com.blazebit.persistence.view.impl.entity.MapViewToEntityMapper) InitialValueAttributeAccessor(com.blazebit.persistence.view.impl.accessor.InitialValueAttributeAccessor) AttributeAccessor(com.blazebit.persistence.view.impl.accessor.AttributeAccessor) SimpleMapViewToEntityMapper(com.blazebit.persistence.view.impl.update.flush.SimpleMapViewToEntityMapper) EntityMetamodel(com.blazebit.persistence.parser.EntityMetamodel) Map(java.util.Map) RecordingMap(com.blazebit.persistence.view.impl.collection.RecordingMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Aggregations

MapViewToEntityMapper (com.blazebit.persistence.view.impl.entity.MapViewToEntityMapper)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)3 ViewToEntityMapper (com.blazebit.persistence.view.impl.entity.ViewToEntityMapper)2 EntityMetamodel (com.blazebit.persistence.parser.EntityMetamodel)1 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)1 JpaProvider (com.blazebit.persistence.spi.JpaProvider)1 InverseRemoveStrategy (com.blazebit.persistence.view.InverseRemoveStrategy)1 AttributeAccessor (com.blazebit.persistence.view.impl.accessor.AttributeAccessor)1 InitialValueAttributeAccessor (com.blazebit.persistence.view.impl.accessor.InitialValueAttributeAccessor)1 CollectionRemoveListener (com.blazebit.persistence.view.impl.collection.CollectionRemoveListener)1 RecordingList (com.blazebit.persistence.view.impl.collection.RecordingList)1 RecordingMap (com.blazebit.persistence.view.impl.collection.RecordingMap)1 CollectionAttributeFlusher (com.blazebit.persistence.view.impl.update.flush.CollectionAttributeFlusher)1 EntityCollectionRemoveListener (com.blazebit.persistence.view.impl.update.flush.EntityCollectionRemoveListener)1 IndexedListAttributeFlusher (com.blazebit.persistence.view.impl.update.flush.IndexedListAttributeFlusher)1 MapAttributeFlusher (com.blazebit.persistence.view.impl.update.flush.MapAttributeFlusher)1 SimpleMapViewToEntityMapper (com.blazebit.persistence.view.impl.update.flush.SimpleMapViewToEntityMapper)1