Search in sources :

Example 1 with PluralAttribute

use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.

the class ViewMapper method createAccessor.

private ObjectMapper createAccessor(ManagedViewType<S> sourceType, ManagedViewType<T> targetType, boolean ignoreMissing, EntityViewKindMapping entityViewKindMapping, EntityViewManager entityViewManager, ProxyFactory proxyFactory, MethodAttribute<? super T, ?> targetAttribute, String prefix, Map<String, Key<Object, Object>> subMappers) {
    String newPrefix;
    if (prefix.isEmpty()) {
        newPrefix = targetAttribute.getName();
    } else {
        newPrefix = prefix + "." + targetAttribute.getName();
    }
    Type<?> attributeType;
    MappingConstructorImpl<?> constructor = null;
    Boolean maybeMarkNew = entityViewKindMapping == EntityViewKindMapping.MARK_NEW ? null : false;
    Key<Object, Object> subMapperKey = subMappers.get(newPrefix);
    if (subMapperKey == Key.EXCLUDE_MARKER) {
        return null;
    } else if (subMapperKey != null) {
        ignoreMissing = subMapperKey.ignoreMissing;
        maybeMarkNew = subMapperKey.markNew;
    }
    // Try to find a source attribute
    MethodAttribute<?, ?> sourceAttribute;
    AttributeAccessor accessor;
    if (sourceType == null) {
        sourceAttribute = null;
        if (targetAttribute.getMappingType() == Attribute.MappingType.PARAMETER) {
            return new ParameterObjectMapper(((MappingAttribute<?, ?>) targetAttribute).getMapping());
        }
        accessor = Accessors.forEntityMapping((EntityViewManagerImpl) entityViewManager, targetAttribute);
        if (accessor == null) {
            if (ignoreMissing) {
                return null;
            }
            throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type is missing in source type!", targetType);
        }
    } else {
        sourceAttribute = sourceType.getAttribute(targetAttribute.getName());
        if (sourceAttribute == null) {
            if (targetAttribute.getMappingType() == Attribute.MappingType.PARAMETER) {
                return new ParameterObjectMapper(((MappingAttribute<?, ?>) targetAttribute).getMapping());
            }
            // Optionally ignore missing attributes
            if (ignoreMissing) {
                return null;
            }
            throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type is missing in source type!", sourceType, targetType);
        }
        accessor = Accessors.forViewAttribute(null, sourceAttribute, true);
    }
    // Handle conversion from one type to another
    if (targetAttribute.isCollection()) {
        if ((sourceAttribute != null) && (targetAttribute.getConvertedJavaType() != sourceAttribute.getConvertedJavaType())) {
            throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type has a different plural type than in source type!", sourceType, targetType);
        }
        PluralAttribute<?, ?, ?> targetPluralAttr = (PluralAttribute<?, ?, ?>) targetAttribute;
        Type<?> elementType = (sourceAttribute == null) ? null : ((PluralAttribute<?, ?, ?>) sourceAttribute).getElementType();
        ViewMapper<Object, Object> valueMapper = null;
        attributeType = targetPluralAttr.getElementType();
        if (subMapperKey != null) {
            attributeType = subMapperKey.targetType;
            constructor = subMapperKey.targetConstructor;
        }
        if (targetAttribute.isSubview()) {
            valueMapper = createViewMapper(elementType, attributeType, constructor, ignoreMissing, maybeMarkNew, entityViewManager, proxyFactory, newPrefix, subMappers);
        } else if ((sourceType != null) && (targetPluralAttr.getElementType() != elementType)) {
            throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type has a different element type than in source type!", sourceType, targetType);
        }
        boolean needsDirtyTracker = ((AbstractAttribute<?, ?>) targetAttribute).needsDirtyTracker();
        if (targetPluralAttr.getCollectionType() == PluralAttribute.CollectionType.MAP) {
            MapAttribute<?, ?, ?> targetMapAttr = (MapAttribute<?, ?, ?>) targetAttribute;
            Type<?> keyType = (sourceAttribute == null) ? null : ((MapAttribute<?, ?, ?>) sourceAttribute).getKeyType();
            ViewMapper<Object, Object> keyMapper = null;
            if (targetMapAttr.isKeySubview()) {
                String newKeyPrefix = "KEY(" + newPrefix + ")";
                Key<Object, Object> keySubMapperKey = subMappers.get(newKeyPrefix);
                if (keySubMapperKey == Key.EXCLUDE_MARKER) {
                    keyMapper = null;
                } else {
                    constructor = null;
                    Type<?> keyTargetType = targetMapAttr.getKeyType();
                    Boolean maybeMarkNewKey = entityViewKindMapping == EntityViewKindMapping.MARK_NEW ? null : false;
                    if (subMapperKey != null) {
                        constructor = keySubMapperKey.targetConstructor;
                        keyTargetType = keySubMapperKey.targetType;
                        ignoreMissing = keySubMapperKey.ignoreMissing;
                        maybeMarkNewKey = keySubMapperKey.markNew;
                    }
                    keyMapper = createViewMapper(keyType, keyTargetType, constructor, ignoreMissing, maybeMarkNewKey, entityViewManager, proxyFactory, newPrefix, subMappers);
                }
            } else if ((sourceType != null) && (targetMapAttr.getKeyType() != keyType)) {
                throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type has a different key type than in source type!", sourceType, targetType);
            }
            MapInstantiatorImplementor<?, ?> mapInstantiator = ((AbstractAttribute<?, ?>) targetAttribute).getMapInstantiator();
            return new MapObjectMapper(accessor, needsDirtyTracker, entityViewKindMapping != EntityViewKindMapping.MARK_NEW, mapInstantiator, keyMapper, valueMapper);
        } else {
            CollectionInstantiatorImplementor<?, ?> collectionInstantiator = ((AbstractAttribute<?, ?>) targetAttribute).getCollectionInstantiator();
            return new CollectionObjectMapper(accessor, needsDirtyTracker, entityViewKindMapping != EntityViewKindMapping.MARK_NEW, collectionInstantiator, valueMapper);
        }
    } else if (targetAttribute.isSubview()) {
        attributeType = ((SingularAttribute<?, ?>) targetAttribute).getType();
        if (subMapperKey != null) {
            attributeType = subMapperKey.targetType;
            constructor = subMapperKey.targetConstructor;
        }
        Type<?> type = (sourceAttribute == null) ? null : ((SingularAttribute<?, ?>) sourceAttribute).getType();
        ViewMapper<Object, Object> mapper = createViewMapper(type, attributeType, constructor, ignoreMissing, maybeMarkNew, entityViewManager, proxyFactory, newPrefix, subMappers);
        return new AttributeObjectMapper(accessor, mapper);
    } else if ((sourceAttribute != null) && (targetAttribute.getConvertedJavaType() != sourceAttribute.getConvertedJavaType())) {
        throw inconvertible("Attribute '" + targetAttribute.getName() + "' from target type has a different type than in source type!", sourceType, targetType);
    } else {
        return new PassthroughObjectMapper(accessor);
    }
}
Also used : AbstractAttribute(com.blazebit.persistence.view.impl.metamodel.AbstractAttribute) EntityViewManagerImpl(com.blazebit.persistence.view.impl.EntityViewManagerImpl) PluralAttribute(com.blazebit.persistence.view.metamodel.PluralAttribute) MapAttribute(com.blazebit.persistence.view.metamodel.MapAttribute) SingularAttribute(com.blazebit.persistence.view.metamodel.SingularAttribute) Type(com.blazebit.persistence.view.metamodel.Type) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ViewType(com.blazebit.persistence.view.metamodel.ViewType) AttributeAccessor(com.blazebit.persistence.view.impl.accessor.AttributeAccessor)

Example 2 with PluralAttribute

use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.

the class EntityViewBuilderBaseImpl method withSetBuilder.

private <E> EntityViewNestedBuilder<E, ? extends BuilderType, ?> withSetBuilder(AbstractAttribute<?, ?> attr) {
    checkAttribute(attr, SetAttribute.class, "Set");
    checkType(attr.getElementType(), "element", attr);
    Map<ManagedViewType<? extends E>, String> inheritanceSubtypeMappings = ((PluralAttribute<?, ?, E>) attr).getElementInheritanceSubtypeMappings();
    return new EntityViewNestedBuilderImpl<>(evm, (ManagedViewTypeImplementor<E>) attr.getElementType(), inheritanceSubtypeMappings, optionalParameters, (BuilderType) this, new CollectionEntityViewBuilderListener(getCollection(attr)));
}
Also used : ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) PluralAttribute(com.blazebit.persistence.view.metamodel.PluralAttribute) CollectionEntityViewBuilderListener(com.blazebit.persistence.view.CollectionEntityViewBuilderListener)

Example 3 with PluralAttribute

use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.

the class EntityViewBuilderBaseImpl method withBuilder.

@Override
public <K, V> EntityViewNestedBuilder<V, ? extends BuilderType, ?> withBuilder(MapAttribute<ViewType, K, V> attribute, K key) {
    if (key == null) {
        throw new IllegalArgumentException("Illegal null key!");
    }
    AbstractAttribute<?, ?> attr = getAttribute(attribute);
    checkAttribute(attr, MapAttribute.class, "Map");
    Type<?> keyType = ((MapAttribute<?, ?, ?>) attr).getKeyType();
    checkType(attr.getElementType(), "element", attr);
    key = getValue(keyType, key);
    Map<ManagedViewType<? extends V>, String> inheritanceSubtypeMappings = ((PluralAttribute<?, ?, V>) attr).getElementInheritanceSubtypeMappings();
    return new EntityViewNestedBuilderImpl<>(evm, (ManagedViewTypeImplementor<V>) attr.getElementType(), inheritanceSubtypeMappings, optionalParameters, (BuilderType) this, new MapEntityViewBuilderListener(getMap(attr), key));
}
Also used : MapAttribute(com.blazebit.persistence.view.metamodel.MapAttribute) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) PluralAttribute(com.blazebit.persistence.view.metamodel.PluralAttribute) MapEntityViewBuilderListener(com.blazebit.persistence.view.MapEntityViewBuilderListener)

Example 4 with PluralAttribute

use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.

the class EntityViewBuilderBaseImpl method withListBuilder.

public <E> EntityViewNestedBuilder<E, ? extends BuilderType, ?> withListBuilder(AbstractAttribute<?, ?> attr, int index) {
    checkAttribute(attr, ListAttribute.class, "List");
    checkType(attr.getElementType(), "element", attr);
    Map<ManagedViewType<? extends E>, String> inheritanceSubtypeMappings = ((PluralAttribute<?, ?, E>) attr).getElementInheritanceSubtypeMappings();
    return new EntityViewNestedBuilderImpl<>(evm, (ManagedViewTypeImplementor<E>) attr.getElementType(), inheritanceSubtypeMappings, optionalParameters, (BuilderType) this, new ListEntityViewBuilderListener((List<Object>) getCollection(attr), index));
}
Also used : ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) PluralAttribute(com.blazebit.persistence.view.metamodel.PluralAttribute) ListEntityViewBuilderListener(com.blazebit.persistence.view.ListEntityViewBuilderListener) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with PluralAttribute

use of com.blazebit.persistence.view.metamodel.PluralAttribute in project blaze-persistence by Blazebit.

the class EntityViewBuilderBaseImpl method withBuilder.

@Override
public <E> EntityViewNestedBuilder<E, ? extends BuilderType, ?> withBuilder(ListAttribute<ViewType, E> attribute) {
    AbstractAttribute<?, ?> attr = getAttribute(attribute);
    checkType(attr.getElementType(), "element", attr);
    Map<ManagedViewType<? extends E>, String> inheritanceSubtypeMappings = ((PluralAttribute<?, ?, E>) attr).getElementInheritanceSubtypeMappings();
    return new EntityViewNestedBuilderImpl<>(evm, (ManagedViewTypeImplementor<E>) attr.getElementType(), inheritanceSubtypeMappings, optionalParameters, (BuilderType) this, new CollectionEntityViewBuilderListener(getCollection(attr)));
}
Also used : ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) PluralAttribute(com.blazebit.persistence.view.metamodel.PluralAttribute) CollectionEntityViewBuilderListener(com.blazebit.persistence.view.CollectionEntityViewBuilderListener)

Aggregations

PluralAttribute (com.blazebit.persistence.view.metamodel.PluralAttribute)20 ManagedViewType (com.blazebit.persistence.view.metamodel.ManagedViewType)16 MapAttribute (com.blazebit.persistence.view.metamodel.MapAttribute)13 CollectionEntityViewBuilderListener (com.blazebit.persistence.view.CollectionEntityViewBuilderListener)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ManagedViewTypeImplementor (com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor)4 SingularAttribute (com.blazebit.persistence.view.metamodel.SingularAttribute)4 ViewType (com.blazebit.persistence.view.metamodel.ViewType)4 ArrayList (java.util.ArrayList)4 EntityMetamodel (com.blazebit.persistence.parser.EntityMetamodel)3 CorrelationMultisetTupleElementMapper (com.blazebit.persistence.view.impl.objectbuilder.mapper.CorrelationMultisetTupleElementMapper)3 LinkedHashMap (java.util.LinkedHashMap)3 Expression (com.blazebit.persistence.parser.expression.Expression)2 CorrelationProviderFactory (com.blazebit.persistence.view.CorrelationProviderFactory)2 ListEntityViewBuilderListener (com.blazebit.persistence.view.ListEntityViewBuilderListener)2 MapEntityViewBuilderListener (com.blazebit.persistence.view.MapEntityViewBuilderListener)2 MapKeyEntityViewBuilderListener (com.blazebit.persistence.view.MapKeyEntityViewBuilderListener)2 AttributeAccessor (com.blazebit.persistence.view.impl.accessor.AttributeAccessor)2 AbstractAttribute (com.blazebit.persistence.view.impl.metamodel.AbstractAttribute)2