Search in sources :

Example 16 with ExpressionFactory

use of com.blazebit.persistence.parser.expression.ExpressionFactory in project blaze-persistence by Blazebit.

the class EntityViewSettingHelper method apply.

@SuppressWarnings("unchecked")
public static <T, Q extends FullQueryBuilder<T, Q>> Q apply(EntityViewSetting<T, Q> setting, EntityViewManagerImpl evm, CriteriaBuilder<?> criteriaBuilder, String entityViewRoot) {
    ManagedViewTypeImplementor<?> managedView = evm.getMetamodel().managedView(setting.getEntityViewClass());
    if (managedView == null) {
        throw new IllegalArgumentException("There is no entity view for the class '" + setting.getEntityViewClass().getName() + "' registered!");
    }
    MappingConstructorImpl<?> mappingConstructor = (MappingConstructorImpl<?>) managedView.getConstructor(setting.getViewConstructorName());
    if (managedView instanceof FlatViewType<?>) {
        if (managedView.hasJoinFetchedCollections()) {
            throw new IllegalArgumentException("Can't use the flat view '" + managedView.getJavaType().getName() + "' as view root because it contains join fetched collections! " + "Consider adding a @IdMapping to the entity view or use a different fetch strategy for the collections!");
        }
        if (mappingConstructor == null) {
            if (managedView.getConstructors().size() > 1) {
                mappingConstructor = (MappingConstructorImpl<T>) managedView.getConstructor("init");
            } else if (managedView.getConstructors().size() == 1) {
                mappingConstructor = (MappingConstructorImpl<T>) managedView.getConstructors().toArray()[0];
            }
        }
        if (mappingConstructor != null && mappingConstructor.hasJoinFetchedCollections()) {
            throw new IllegalArgumentException("Can't use the flat view '" + managedView.getJavaType().getName() + "' with the mapping constructor '" + mappingConstructor.getName() + "' as view root because it contains join fetched collections! " + "Consider adding a @IdMapping to the entity view or use a different fetch strategy for the collections!");
        }
    }
    if (managedView.isUpdatable() && !setting.getFetches().isEmpty()) {
        throw new IllegalArgumentException("Specifying fetches for @UpdatableEntityViews is currently disallowed. Remove the fetches!");
    }
    ExpressionFactory ef = criteriaBuilder.getService(ExpressionFactory.class);
    Map<String, Object> optionalParameters;
    if (setting.getOptionalParameters().isEmpty()) {
        optionalParameters = evm.getOptionalParameters();
    } else {
        optionalParameters = new HashMap<>(evm.getOptionalParameters());
        optionalParameters.putAll(setting.getOptionalParameters());
        optionalParameters = Collections.unmodifiableMap(optionalParameters);
    }
    Collection<String> requestedFetches;
    if (setting.getFetches().isEmpty() || !setting.hasAttributeFilters() && !setting.hasAttributeSorters()) {
        requestedFetches = setting.getFetches();
    } else {
        requestedFetches = new HashSet<>(setting.getFetches());
        addFetchesForNonMappingAttributes(setting.getAttributeFilterActivations().keySet(), managedView, requestedFetches);
        addFetchesForNonMappingAttributes(setting.getAttributeSorters().keySet(), managedView, requestedFetches);
    }
    Path root = criteriaBuilder.getPath(entityViewRoot);
    entityViewRoot = root.getPath();
    Q queryBuilder = getQueryBuilder(setting, criteriaBuilder, entityViewRoot, managedView, setting.getProperties());
    EntityViewConfiguration configuration = new EntityViewConfiguration(queryBuilder, ef, new MutableViewJpqlMacro(), new MutableEmbeddingViewJpqlMacro(), optionalParameters, setting.getProperties(), requestedFetches, managedView);
    queryBuilder.selectNew(evm.createObjectBuilder(managedView, mappingConstructor, root.getJavaType(), entityViewRoot, null, criteriaBuilder, configuration, 0, 0, false));
    Set<String> fetches = configuration.getFetches();
    applyAttributeFilters(setting, evm, queryBuilder, entityViewRoot, fetches, managedView);
    applyViewFilters(setting, evm, queryBuilder, managedView);
    applyAttributeSorters(setting, queryBuilder, entityViewRoot, fetches, managedView);
    applyOptionalParameters(optionalParameters, queryBuilder);
    return queryBuilder;
}
Also used : Path(com.blazebit.persistence.Path) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) MappingConstructorImpl(com.blazebit.persistence.view.impl.metamodel.MappingConstructorImpl) FlatViewType(com.blazebit.persistence.view.metamodel.FlatViewType)

Example 17 with ExpressionFactory

use of com.blazebit.persistence.parser.expression.ExpressionFactory in project blaze-persistence by Blazebit.

the class MacroConfigurationExpressionFactory method equals.

/*  WARNING: Be careful when changing the implementation of equals and hashCode. Extensions rely on the the logic for efficient caching.  */
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o instanceof ExpressionFactory) {
        ExpressionFactory that = (ExpressionFactory) o;
        ExpressionFactory thatExpressionFactory = that.unwrap(expressionFactory.getClass());
        if (thatExpressionFactory == null || !expressionFactory.equals(thatExpressionFactory)) {
            return false;
        }
        return getDefaultMacroConfiguration() != null ? getDefaultMacroConfiguration().equals(that.getDefaultMacroConfiguration()) : that.getDefaultMacroConfiguration() == null;
    }
    return false;
}
Also used : ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory)

Example 18 with ExpressionFactory

use of com.blazebit.persistence.parser.expression.ExpressionFactory 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)18 HashMap (java.util.HashMap)8 MutableEmbeddingViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro)5 MutableViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro)5 Expression (com.blazebit.persistence.parser.expression.Expression)4 MacroConfiguration (com.blazebit.persistence.parser.expression.MacroConfiguration)4 MacroFunction (com.blazebit.persistence.parser.expression.MacroFunction)4 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)4 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)4 MacroConfigurationExpressionFactory (com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory)4 ManagedViewType (com.blazebit.persistence.view.metamodel.ManagedViewType)4 ViewType (com.blazebit.persistence.view.metamodel.ViewType)4 AbstractCachingExpressionFactory (com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory)3 Map (java.util.Map)3 ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)2 ExpressionFactoryImpl (com.blazebit.persistence.parser.expression.ExpressionFactoryImpl)2 ListIndexExpression (com.blazebit.persistence.parser.expression.ListIndexExpression)2 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)2 NullExpression (com.blazebit.persistence.parser.expression.NullExpression)2 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)2