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;
}
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;
}
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()]));
}
Aggregations