Search in sources :

Example 1 with MacroConfigurationExpressionFactory

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

the class Mappers method forViewConvertToViewAttributeMapping.

public static <S, T> Mapper<S, T> forViewConvertToViewAttributeMapping(EntityViewManagerImpl evm, ViewType<S> sourceViewType, ViewType<T> targetViewType, Map<String, String> mapping, Mapper<S, T> additionalMapper) {
    List<Mapper<S, T>> mappers = new ArrayList<>();
    ExpressionFactory ef = evm.getCriteriaBuilderFactory().getService(ExpressionFactory.class);
    for (MethodAttribute<?, ?> attribute : targetViewType.getAttributes()) {
        if (attribute.isUpdatable() && attribute instanceof MappingAttribute<?, ?> && attribute instanceof SingularAttribute<?, ?>) {
            for (Map.Entry<String, String> entry : mapping.entrySet()) {
                if (entry.getValue().equals(((MappingAttribute) attribute).getMapping())) {
                    Type<?> attributeType = ((SingularAttribute<?, ?>) attribute).getType();
                    AttributeAccessor entityAccessor;
                    if (entry.getKey().isEmpty()) {
                        entityAccessor = Accessors.forEntityMapping(evm, sourceViewType.getEntityClass(), ((MappingAttribute<?, ?>) sourceViewType.getIdAttribute()).getMapping());
                    } else {
                        entityAccessor = Accessors.forEntityMapping(evm, sourceViewType.getEntityClass(), entry.getKey());
                    }
                    AttributeAccessor targetAttributeAccessor = Accessors.forMutableViewAttribute(evm, attribute);
                    if (attributeType instanceof ViewType<?>) {
                        ViewType<?> viewType = (ViewType<?>) attributeType;
                        Type<?> attributeViewIdType = ((SingularAttribute<?, ?>) viewType.getIdAttribute()).getType();
                        EntityTupleizer entityTupleizer = null;
                        ObjectBuilder<?> idViewBuilder = null;
                        if (attributeViewIdType instanceof ManagedViewType<?>) {
                            entityTupleizer = new DefaultEntityTupleizer(evm, (ManagedViewType<?>) attributeViewIdType);
                            idViewBuilder = (ObjectBuilder<Object>) evm.getTemplate(new MacroConfigurationExpressionFactory(ef, ef.getDefaultMacroConfiguration()), (ManagedViewTypeImplementor<?>) attributeViewIdType, null, null, new MutableViewJpqlMacro(), null, new MutableEmbeddingViewJpqlMacro(), 0).createObjectBuilder(null, null, null, 0, false, false);
                        }
                        mappers.add(new ReferenceViewAttributeMapper<S, T>(evm, entityAccessor, viewType.getJavaType(), entityTupleizer, targetAttributeAccessor, idViewBuilder));
                    } else {
                        mappers.add((Mapper<S, T>) new AttributeMapper<>(Collections.singletonList(entityAccessor), Collections.singletonList(targetAttributeAccessor)));
                    }
                }
            }
        }
    }
    if (mappers.isEmpty()) {
        return additionalMapper;
    }
    return new CompositeMapper<>(mappers.toArray(new Mapper[mappers.size()]));
}
Also used : DefaultEntityTupleizer(com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer) EntityTupleizer(com.blazebit.persistence.view.impl.entity.EntityTupleizer) ArrayList(java.util.ArrayList) MappingAttribute(com.blazebit.persistence.view.metamodel.MappingAttribute) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) DefaultEntityTupleizer(com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) ManagedViewTypeImplementor(com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) SingularAttribute(com.blazebit.persistence.view.metamodel.SingularAttribute) AttributeAccessor(com.blazebit.persistence.view.impl.accessor.AttributeAccessor) HashMap(java.util.HashMap) Map(java.util.Map) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ViewType(com.blazebit.persistence.view.metamodel.ViewType)

Example 2 with MacroConfigurationExpressionFactory

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

the class MetamodelBuildingContextImpl method createTypeValidationExpressionFactory.

private MacroConfigurationExpressionFactory createTypeValidationExpressionFactory() {
    MacroConfiguration originalMacroConfiguration = expressionFactory.getDefaultMacroConfiguration();
    ExpressionFactory cachingExpressionFactory = expressionFactory.unwrap(AbstractCachingExpressionFactory.class);
    Map<String, MacroFunction> macros = new HashMap<>();
    macros.put("view", new JpqlMacroAdapter(new FunctionPassthroughJpqlMacro("VIEW"), cachingExpressionFactory));
    macros.put("view_root", new JpqlMacroAdapter(new FunctionPassthroughJpqlMacro("VIEW_ROOT"), cachingExpressionFactory));
    macros.put("embedding_view", new JpqlMacroAdapter(new FunctionPassthroughJpqlMacro("EMBEDDING_VIEW"), cachingExpressionFactory));
    MacroConfiguration macroConfiguration = originalMacroConfiguration.with(macros);
    return new MacroConfigurationExpressionFactory(cachingExpressionFactory, macroConfiguration);
}
Also used : MacroFunction(com.blazebit.persistence.parser.expression.MacroFunction) MacroConfiguration(com.blazebit.persistence.parser.expression.MacroConfiguration) AbstractCachingExpressionFactory(com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) FunctionPassthroughJpqlMacro(com.blazebit.persistence.view.impl.macro.FunctionPassthroughJpqlMacro) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JpqlMacroAdapter(com.blazebit.persistence.view.impl.JpqlMacroAdapter) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory)

Example 3 with MacroConfigurationExpressionFactory

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

the class MetamodelBuildingContextImpl method createMacroAwareExpressionFactory.

@Override
public MacroConfigurationExpressionFactory createMacroAwareExpressionFactory(String viewRoot) {
    MacroConfiguration originalMacroConfiguration = expressionFactory.getDefaultMacroConfiguration();
    ExpressionFactory cachingExpressionFactory = expressionFactory.unwrap(AbstractCachingExpressionFactory.class);
    Map<String, MacroFunction> macros = new HashMap<>();
    macros.put("view", new JpqlMacroAdapter(new MutableViewJpqlMacro(viewRoot), cachingExpressionFactory));
    macros.put("view_root", new JpqlMacroAdapter(new DefaultViewRootJpqlMacro(viewRoot), cachingExpressionFactory));
    macros.put("embedding_view", new JpqlMacroAdapter(new MutableEmbeddingViewJpqlMacro(), cachingExpressionFactory));
    MacroConfiguration macroConfiguration = originalMacroConfiguration.with(macros);
    return new MacroConfigurationExpressionFactory(cachingExpressionFactory, macroConfiguration);
}
Also used : MacroFunction(com.blazebit.persistence.parser.expression.MacroFunction) MacroConfiguration(com.blazebit.persistence.parser.expression.MacroConfiguration) AbstractCachingExpressionFactory(com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) JpqlMacroAdapter(com.blazebit.persistence.view.impl.JpqlMacroAdapter) DefaultViewRootJpqlMacro(com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory)

Example 4 with MacroConfigurationExpressionFactory

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

the class Mappers method forViewConvertToViewAttributeMapping.

public static <S, T> Mapper<S, T> forViewConvertToViewAttributeMapping(EntityViewManagerImpl evm, ViewType<S> sourceViewType, ViewType<T> targetViewType, String mappedBy, Mapper<S, T> additionalMapper) {
    List<Mapper<S, T>> mappers = new ArrayList<>();
    AttributeAccessor entityAccessor = Accessors.forEntityMapping(evm, sourceViewType.getEntityClass(), ((MappingAttribute<?, ?>) sourceViewType.getIdAttribute()).getMapping());
    ExpressionFactory ef = evm.getCriteriaBuilderFactory().getService(ExpressionFactory.class);
    for (MethodAttribute<?, ?> attribute : targetViewType.getAttributes()) {
        if (attribute.isUpdatable() && attribute instanceof MappingAttribute<?, ?> && attribute instanceof SingularAttribute<?, ?>) {
            if (mappedBy.equals(((MappingAttribute) attribute).getMapping())) {
                ViewType<?> attributeType = (ViewType<?>) ((SingularAttribute<?, ?>) attribute).getType();
                Type<?> attributeViewIdType = ((SingularAttribute<?, ?>) attributeType.getIdAttribute()).getType();
                EntityTupleizer entityTupleizer = null;
                ObjectBuilder<?> idViewBuilder = null;
                if (attributeViewIdType instanceof ManagedViewType<?>) {
                    entityTupleizer = new DefaultEntityTupleizer(evm, (ManagedViewType<?>) attributeViewIdType);
                    idViewBuilder = (ObjectBuilder<Object>) evm.getTemplate(new MacroConfigurationExpressionFactory(ef, ef.getDefaultMacroConfiguration()), (ManagedViewTypeImplementor<?>) attributeViewIdType, null, null, new MutableViewJpqlMacro(), null, new MutableEmbeddingViewJpqlMacro(), 0).createObjectBuilder(null, null, null, 0, false, false);
                }
                mappers.add(new ReferenceViewAttributeMapper<S, T>(evm, entityAccessor, attributeType.getJavaType(), entityTupleizer, Accessors.forMutableViewAttribute(evm, attribute), idViewBuilder));
            }
        }
    }
    if (mappers.isEmpty()) {
        return additionalMapper;
    }
    if (additionalMapper != null) {
        mappers.add(additionalMapper);
    }
    return new CompositeMapper<>(mappers.toArray(new Mapper[mappers.size()]));
}
Also used : DefaultEntityTupleizer(com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer) EntityTupleizer(com.blazebit.persistence.view.impl.entity.EntityTupleizer) ArrayList(java.util.ArrayList) MappingAttribute(com.blazebit.persistence.view.metamodel.MappingAttribute) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) DefaultEntityTupleizer(com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) ManagedViewTypeImplementor(com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) SingularAttribute(com.blazebit.persistence.view.metamodel.SingularAttribute) AttributeAccessor(com.blazebit.persistence.view.impl.accessor.AttributeAccessor) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ViewType(com.blazebit.persistence.view.metamodel.ViewType)

Aggregations

ExpressionFactory (com.blazebit.persistence.parser.expression.ExpressionFactory)4 MacroConfigurationExpressionFactory (com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory)4 MutableEmbeddingViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro)3 MutableViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro)3 HashMap (java.util.HashMap)3 AbstractCachingExpressionFactory (com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory)2 MacroConfiguration (com.blazebit.persistence.parser.expression.MacroConfiguration)2 MacroFunction (com.blazebit.persistence.parser.expression.MacroFunction)2 JpqlMacroAdapter (com.blazebit.persistence.view.impl.JpqlMacroAdapter)2 AttributeAccessor (com.blazebit.persistence.view.impl.accessor.AttributeAccessor)2 EntityTupleizer (com.blazebit.persistence.view.impl.entity.EntityTupleizer)2 ManagedViewTypeImplementor (com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor)2 DefaultEntityTupleizer (com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer)2 ManagedViewType (com.blazebit.persistence.view.metamodel.ManagedViewType)2 MappingAttribute (com.blazebit.persistence.view.metamodel.MappingAttribute)2 SingularAttribute (com.blazebit.persistence.view.metamodel.SingularAttribute)2 ViewType (com.blazebit.persistence.view.metamodel.ViewType)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 DefaultViewRootJpqlMacro (com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro)1