Search in sources :

Example 1 with DefaultViewRootJpqlMacro

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

the class EntityViewManagerImpl method createObjectBuilder.

public ObjectBuilder<?> createObjectBuilder(ManagedViewTypeImplementor<?> viewType, MappingConstructorImpl<?> mappingConstructor, Class<?> rootType, String entityViewRoot, String embeddingViewPath, FullQueryBuilder<?, ?> criteriaBuilder, EntityViewConfiguration configuration, int offset, int suffix, boolean nullFlatViewIfEmpty) {
    ExpressionFactory ef = criteriaBuilder.getService(ExpressionFactory.class);
    if (!viewType.getEntityClass().isAssignableFrom(rootType)) {
        if (rootType.isAssignableFrom(viewType.getEntityClass())) {
            entityViewRoot = "TREAT(" + entityViewRoot + " AS " + metamodel.getEntityMetamodel().getEntity(viewType.getJavaType()).getName() + ")";
        } else {
            throw new IllegalArgumentException("The given view type with the entity type '" + viewType.getEntityClass().getName() + "' can not be applied to the query builder with result type '" + rootType.getName() + "'");
        }
    }
    MacroConfiguration originalMacroConfiguration = ef.getDefaultMacroConfiguration();
    ExpressionFactory cachingExpressionFactory = ef.unwrap(AbstractCachingExpressionFactory.class);
    JpqlMacro viewRootJpqlMacro = new DefaultViewRootJpqlMacro(entityViewRoot);
    ViewJpqlMacro viewJpqlMacro = configuration.getViewJpqlMacro();
    EmbeddingViewJpqlMacro embeddingViewJpqlMacro = configuration.getEmbeddingViewJpqlMacro();
    viewJpqlMacro.setViewPath(entityViewRoot);
    Map<String, MacroFunction> macros = new HashMap<>();
    macros.put("view", new JpqlMacroAdapter(viewJpqlMacro, cachingExpressionFactory));
    macros.put("view_root", new JpqlMacroAdapter(viewRootJpqlMacro, cachingExpressionFactory));
    macros.put("embedding_view", new JpqlMacroAdapter(embeddingViewJpqlMacro, cachingExpressionFactory));
    MacroConfiguration macroConfiguration = originalMacroConfiguration.with(macros);
    MacroConfigurationExpressionFactory macroEf = new MacroConfigurationExpressionFactory(cachingExpressionFactory, macroConfiguration);
    criteriaBuilder.registerMacro("view_root", viewRootJpqlMacro);
    return getTemplate(macroEf, viewType, mappingConstructor, entityViewRoot, viewJpqlMacro, embeddingViewPath, embeddingViewJpqlMacro, offset).createObjectBuilder(criteriaBuilder, configuration.getOptionalParameters(), configuration, suffix, false, nullFlatViewIfEmpty);
}
Also used : AbstractCachingExpressionFactory(com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) ViewJpqlMacro(com.blazebit.persistence.view.spi.ViewJpqlMacro) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) DefaultViewRootJpqlMacro(com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) JpqlMacro(com.blazebit.persistence.spi.JpqlMacro) DefaultViewRootJpqlMacro(com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro) ViewJpqlMacro(com.blazebit.persistence.view.spi.ViewJpqlMacro) MacroFunction(com.blazebit.persistence.parser.expression.MacroFunction) MacroConfiguration(com.blazebit.persistence.parser.expression.MacroConfiguration)

Example 2 with DefaultViewRootJpqlMacro

use of com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro 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)

Aggregations

AbstractCachingExpressionFactory (com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory)2 ExpressionFactory (com.blazebit.persistence.parser.expression.ExpressionFactory)2 MacroConfiguration (com.blazebit.persistence.parser.expression.MacroConfiguration)2 MacroFunction (com.blazebit.persistence.parser.expression.MacroFunction)2 DefaultViewRootJpqlMacro (com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro)2 HashMap (java.util.HashMap)2 JpqlMacro (com.blazebit.persistence.spi.JpqlMacro)1 JpqlMacroAdapter (com.blazebit.persistence.view.impl.JpqlMacroAdapter)1 MacroConfigurationExpressionFactory (com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory)1 MutableEmbeddingViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro)1 MutableViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro)1 EmbeddingViewJpqlMacro (com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro)1 ViewJpqlMacro (com.blazebit.persistence.view.spi.ViewJpqlMacro)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1