use of com.blazebit.persistence.view.metamodel.FlatViewType in project blaze-persistence by Blazebit.
the class EntityViewSettingHelper method getQueryBuilder.
private static <T, Q extends FullQueryBuilder<T, Q>> Q getQueryBuilder(EntityViewSetting<T, Q> setting, CriteriaBuilder<?> criteriaBuilder, String entityViewRoot, ManagedViewTypeImplementor<?> managedView, Map<String, Object> properties) {
if (setting.isPaginated()) {
KeysetPage keysetPage = setting.getKeysetPage();
boolean forceUseKeyset = keysetPage != null && getBooleanProperty(properties, ConfigurationProperties.PAGINATION_FORCE_USE_KEYSET, false);
if (forceUseKeyset) {
setting.withKeysetPage(null);
}
PaginatedCriteriaBuilder<?> builder;
if (managedView instanceof FlatViewType<?>) {
if (setting.isKeysetPaginated()) {
if (setting.getFirstResult() == -1) {
builder = criteriaBuilder.pageAndNavigate(setting.getEntityId(), setting.getMaxResults()).withKeysetExtraction(true);
} else {
builder = criteriaBuilder.page(setting.getKeysetPage(), setting.getFirstResult(), setting.getMaxResults());
}
} else {
if (setting.getFirstResult() == -1) {
builder = criteriaBuilder.page(0, setting.getMaxResults());
} else {
builder = criteriaBuilder.page(setting.getFirstResult(), setting.getMaxResults());
}
}
} else {
// When the result should be paginated, we have to properly paginate by the identifier of the view
MethodAttribute<?, ?> idAttribute = ((ViewTypeImplementor<?>) managedView).getIdAttribute();
String firstExpression;
List<String> expressions;
if (idAttribute.isSubview()) {
String prefix = getMapping(entityViewRoot, idAttribute, criteriaBuilder);
ManagedViewTypeImplementor<?> type = (ManagedViewTypeImplementor<?>) ((SingularAttribute<?, ?>) idAttribute).getType();
Set<MethodAttribute<?, ?>> attributes = (Set) type.getAttributes();
Iterator<MethodAttribute<?, ?>> iterator = attributes.iterator();
firstExpression = getMapping(prefix, iterator.next(), criteriaBuilder);
if (iterator.hasNext()) {
expressions = new ArrayList<>(attributes.size() - 1);
while (iterator.hasNext()) {
expressions.add(getMapping(prefix, iterator.next(), criteriaBuilder));
}
} else {
expressions = null;
}
} else {
expressions = null;
firstExpression = getMapping(entityViewRoot, idAttribute, criteriaBuilder);
}
if (setting.isKeysetPaginated()) {
if (setting.getFirstResult() == -1) {
builder = criteriaBuilder.pageByAndNavigate(setting.getEntityId(), setting.getMaxResults(), firstExpression, getExpressionArray(expressions)).withKeysetExtraction(true);
} else {
builder = criteriaBuilder.pageBy(setting.getKeysetPage(), setting.getFirstResult(), setting.getMaxResults(), firstExpression, getExpressionArray(expressions));
}
} else {
if (setting.getFirstResult() == -1) {
builder = criteriaBuilder.pageBy(0, setting.getMaxResults(), firstExpression, getExpressionArray(expressions));
} else {
builder = criteriaBuilder.pageBy(setting.getFirstResult(), setting.getMaxResults(), firstExpression, getExpressionArray(expressions));
}
}
}
if (forceUseKeyset) {
if (keysetPage.getLowest() != null) {
builder.beforeKeyset(keysetPage.getLowest());
} else if (keysetPage.getHighest() != null) {
builder.afterKeyset(keysetPage.getHighest());
}
}
boolean disableCountQuery = getBooleanProperty(properties, ConfigurationProperties.PAGINATION_DISABLE_COUNT_QUERY, false);
if (disableCountQuery) {
builder.withCountQuery(false);
} else {
Integer boundedCount = null;
Object o = properties.get(ConfigurationProperties.PAGINATION_BOUNDED_COUNT);
if (o != null) {
if (o instanceof Integer || o instanceof Long) {
boundedCount = ((Number) o).intValue();
} else if (o instanceof String) {
boundedCount = Integer.parseInt((String) o);
} else {
throw new IllegalArgumentException("Invalid value of type " + o.getClass().getName() + " given for the integer property: " + ConfigurationProperties.PAGINATION_BOUNDED_COUNT);
}
}
if (boundedCount != null) {
builder.withBoundedCount(boundedCount);
}
}
Integer highestKeyOffset = null;
Object o = properties.get(ConfigurationProperties.PAGINATION_HIGHEST_KEYSET_OFFSET);
if (o != null) {
if (o instanceof Integer) {
highestKeyOffset = ((Number) o).intValue();
} else if (o instanceof String) {
highestKeyOffset = Integer.parseInt((String) o);
} else {
throw new IllegalArgumentException("Invalid value of type " + o.getClass().getName() + " given for the integer property: " + ConfigurationProperties.PAGINATION_HIGHEST_KEYSET_OFFSET);
}
}
if (highestKeyOffset != null) {
builder.withHighestKeysetOffset(highestKeyOffset);
}
boolean extractAllKeysets = getBooleanProperty(properties, ConfigurationProperties.PAGINATION_EXTRACT_ALL_KEYSETS, false);
if (extractAllKeysets) {
builder.withExtractAllKeysets(true);
}
return (Q) builder;
} else {
return (Q) criteriaBuilder;
}
}
use of com.blazebit.persistence.view.metamodel.FlatViewType in project blaze-persistence by Blazebit.
the class ProxyFactory method addSetter.
private CtMethod addSetter(AbstractMethodAttribute<?, ?> attribute, CtClass cc, CtField attributeField, String methodName, CtField mutableStateField, boolean dirtyChecking, boolean isId) throws CannotCompileException, NotFoundException {
FieldInfo finfo = attributeField.getFieldInfo2();
String fieldType = finfo.getDescriptor();
String desc = "(" + fieldType + ")V";
ConstPool cp = finfo.getConstPool();
MethodInfo minfo = new MethodInfo(cp, methodName, desc);
minfo.setAccessFlags(AccessFlag.PUBLIC);
String fieldName = finfo.getName();
StringBuilder sb = new StringBuilder();
sb.append("{\n");
boolean invalidSetter = false;
// When the declaring type is updatable/creatable we only allow setting the id value on new objects
if (isId) {
if (attribute != null && attribute.getDeclaringType().isCreatable()) {
sb.append("\tif ($0.$$_kind != (byte) 2) {\n");
sb.append("\t\tthrow new IllegalArgumentException(\"Updating the id attribute '").append(attribute.getName()).append("' is only allowed for new entity view objects created via EntityViewManager.create()!\");\n");
sb.append("\t}\n");
} else if (attribute != null && attribute.getDeclaringType().isUpdatable()) {
sb.append("\tthrow new IllegalArgumentException(\"Updating the id attribute '").append(attribute.getName()).append("' is only allowed for new entity view objects created via EntityViewManager.create()!\");\n");
invalidSetter = true;
}
}
// Disallow calling the setter on a mutable only relation with objects of a different identity as that might indicate a programming error
if (attribute != null && attribute.getDirtyStateIndex() != -1 && !attribute.isUpdatable() && (attribute.getDeclaringType().isCreatable() || attribute.getDeclaringType().isUpdatable())) {
sb.append("\tObject tmp;\n");
sb.append("\tif ($1 != $0.").append(fieldName);
if (attribute.isCollection()) {
// TODO: We could theoretically support collections too by looking into them and asserting equality element-wise
} else {
SingularAttribute<?, ?> singularAttribute = (SingularAttribute<?, ?>) attribute;
Type<?> type = singularAttribute.getType();
if (attribute.isSubview()) {
if (!(type instanceof FlatViewType<?>)) {
String idMethodName = ((ViewType<?>) type).getIdAttribute().getJavaMethod().getName();
sb.append(" && ");
sb.append("($1 == null || (tmp = $1.");
sb.append(idMethodName);
sb.append("()) == null || !java.util.Objects.equals(tmp, $0.");
sb.append(fieldName);
sb.append('.').append(idMethodName);
sb.append("()))");
}
} else {
BasicTypeImpl<?> basicType = (BasicTypeImpl<?>) type;
boolean jpaEntity = basicType.isJpaEntity();
if (jpaEntity) {
IdentifiableType<?> identifiableType = (IdentifiableType<?>) basicType.getManagedType();
for (javax.persistence.metamodel.SingularAttribute<?, ?> idAttribute : JpaMetamodelUtils.getIdAttributes(identifiableType)) {
Class<?> idClass = JpaMetamodelUtils.resolveFieldClass(basicType.getJavaType(), idAttribute);
String idAccessor = addIdAccessor(cc, identifiableType, idAttribute, pool.get(idClass.getName()));
sb.append(" && ");
sb.append("($1 == null || (tmp = ");
sb.append(idAccessor);
sb.append("($1)) == null || !java.util.Objects.equals(tmp, ");
sb.append(idAccessor);
sb.append("($0.");
sb.append(fieldName);
sb.append(")))");
}
}
}
}
sb.append(") {\n");
sb.append("\t\tthrow new IllegalArgumentException(\"Updating the mutable-only attribute '").append(attribute.getName()).append("' with a value that has not the same identity is not allowed! Consider making the attribute updatable or update the value directly instead of replacing it!\");\n");
sb.append("\t}\n");
}
if (attribute != null && attribute.isUpdatable() && dirtyChecking) {
if (attribute.isCollection()) {
if (strictCascadingCheck) {
// With strict cascading checks enabled, we don't allow setting collections of mutable subviews
boolean mutableElement = !attribute.getUpdateCascadeAllowedSubtypes().isEmpty() || !attribute.getPersistCascadeAllowedSubtypes().isEmpty();
if (mutableElement && ((AbstractMethodPluralAttribute<?, ?, ?>) attribute).getElementType().getMappingType() != Type.MappingType.BASIC) {
sb.append("\t\tthrow new IllegalArgumentException(\"Replacing a collection that PERSIST or UPDATE cascades is prohibited by default! Instead, replace the contents by doing clear() and addAll()!\");\n");
}
}
} else {
// Only consider subviews here for now
if (attribute.isSubview()) {
String subtypeArray = addAllowedSubtypeField(cc, attribute);
addParentRequiringUpdateSubtypesField(cc, attribute);
addParentRequiringCreateSubtypesField(cc, attribute);
sb.append("\tif ($1 != null) {\n");
sb.append("\t\tClass c;\n");
sb.append("\t\tboolean isNew;\n");
sb.append("\t\tif ($1 instanceof ").append(EntityViewProxy.class.getName()).append(") {\n");
sb.append("\t\t\tc = ((").append(EntityViewProxy.class.getName()).append(") $1).$$_getEntityViewClass();\n");
sb.append("\t\t\tisNew = ((").append(EntityViewProxy.class.getName()).append(") $1).$$_isNew();\n");
sb.append("\t\t} else {\n");
sb.append("\t\t\tc = $1.getClass();\n");
sb.append("\t\t\tisNew = false;\n");
sb.append("\t\t}\n");
sb.append("\t\tif (!").append(attributeField.getDeclaringClass().getName()).append('#').append(attribute.getName()).append("_$$_subtypes.contains(c)) {\n");
sb.append("\t\t\tthrow new IllegalArgumentException(");
sb.append("\"Allowed subtypes for attribute '").append(attribute.getName()).append("' are [").append(subtypeArray).append("] but got an instance of: \"");
sb.append(".concat(c.getName())");
sb.append(");\n");
sb.append("\t\t}\n");
if (strictCascadingCheck) {
sb.append("\t\tif ($0 != $1 && !isNew && ").append(attributeField.getDeclaringClass().getName()).append('#').append(attribute.getName()).append("_$$_parentRequiringUpdateSubtypes.contains(c) && !((").append(DirtyTracker.class.getName()).append(") $1).$$_hasParent()) {\n");
sb.append("\t\t\tthrow new IllegalArgumentException(");
sb.append("\"Setting instances of type [\" + c.getName() + \"] on attribute '").append(attribute.getName()).append("' is not allowed until they are assigned to an attribute that cascades the type! ");
sb.append("If you want this attribute to cascade, annotate it with @UpdatableMapping(cascade = { UPDATE }). You can also turn off strict cascading checks by setting ConfigurationProperties.UPDATER_STRICT_CASCADING_CHECK to false\"");
sb.append(");\n");
sb.append("\t\t}\n");
sb.append("\t\tif ($0 != $1 && isNew && ").append(attributeField.getDeclaringClass().getName()).append('#').append(attribute.getName()).append("_$$_parentRequiringCreateSubtypes.contains(c) && !((").append(DirtyTracker.class.getName()).append(") $1).$$_hasParent()) {\n");
sb.append("\t\t\tthrow new IllegalArgumentException(");
sb.append("\"Setting instances of type [\" + c.getName() + \"] on attribute '").append(attribute.getName()).append("' is not allowed until they are assigned to an attribute that cascades the type! ");
sb.append("If you want this attribute to cascade, annotate it with @UpdatableMapping(cascade = { PERSIST }). You can also turn off strict cascading checks by setting ConfigurationProperties.UPDATER_STRICT_CASCADING_CHECK to false\"");
sb.append(");\n");
sb.append("\t\t}\n");
}
sb.append("\t}\n");
}
}
}
if (attribute != null && attribute.getDirtyStateIndex() != -1) {
int mutableStateIndex = attribute.getDirtyStateIndex();
// Unset previous object parent
if (attribute.isCollection()) {
sb.append("\tif ($0.").append(fieldName).append(" != null && $0.").append(fieldName).append(" != $1) {\n");
if (attribute instanceof MapAttribute<?, ?, ?>) {
sb.append("\t\tif ($0.").append(fieldName).append(" instanceof ").append(RecordingMap.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(RecordingMap.class.getName()).append(") $0.").append(fieldName).append(").$$_unsetParent();\n");
sb.append("\t\t}\n");
} else {
sb.append("\t\tif ($0.").append(fieldName).append(" instanceof ").append(RecordingCollection.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(RecordingCollection.class.getName()).append(") $0.").append(fieldName).append(").$$_unsetParent();\n");
sb.append("\t\t}\n");
}
sb.append("\t}\n");
} else if (attribute.isSubview()) {
if (attribute.isUpdatableOnly() && !attribute.isCorrelated()) {
sb.append("\tif ($0.").append(fieldName).append(" != $1 && $0.").append(fieldName).append(" instanceof ").append(MutableStateTrackable.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(MutableStateTrackable.class.getName()).append(") $0.").append(fieldName).append(").$$_removeReadOnlyParent($0, ").append(mutableStateIndex).append(");\n");
sb.append("\t} else if ($0.").append(fieldName).append(" != $1 && $0.").append(fieldName).append(" instanceof ").append(BasicDirtyTracker.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(BasicDirtyTracker.class.getName()).append(") $0.").append(fieldName).append(").$$_unsetParent();\n");
} else {
sb.append("\tif ($0.").append(fieldName).append(" != $1 && $0.").append(fieldName).append(" instanceof ").append(BasicDirtyTracker.class.getName()).append(") {\n");
sb.append("\t\t((").append(BasicDirtyTracker.class.getName()).append(") $0.").append(fieldName).append(").$$_unsetParent();\n");
}
sb.append("\t}\n");
}
if (mutableStateField != null) {
// this.mutableState[mutableStateIndex] = $1
sb.append("\t$0.").append(mutableStateField.getName()).append("[").append(mutableStateIndex).append("] = ");
renderValueForArray(sb, attributeField.getType(), "$1");
}
if (dirtyChecking) {
// this.dirty = true
sb.append("\t$0.$$_markDirty(").append(mutableStateIndex).append(");\n");
// Set new objects parent
if (attribute.isCollection() || attribute.isSubview()) {
sb.append("\tif ($0.$$_initialized && $1 != null && $0.").append(fieldName).append(" != $1) {\n");
if (attribute.isCollection()) {
if (attribute instanceof MapAttribute<?, ?, ?>) {
sb.append("\t\tif ($1 instanceof ").append(RecordingMap.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(RecordingMap.class.getName()).append(") $1).$$_setParent($0, ").append(mutableStateIndex).append(");\n");
sb.append("\t\t}\n");
} else {
sb.append("\t\tif ($1 instanceof ").append(RecordingCollection.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(RecordingCollection.class.getName()).append(") $1).$$_setParent($0, ").append(mutableStateIndex).append(");\n");
sb.append("\t\t}\n");
}
} else if (attribute.isSubview()) {
// Correlated attributes are treated special, we don't consider correlated attributes read-only parents
if (attribute.isUpdatableOnly() && !attribute.isCorrelated()) {
sb.append("\t\tif ($1 instanceof ").append(MutableStateTrackable.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(MutableStateTrackable.class.getName()).append(") $1).$$_addReadOnlyParent($0, ").append(mutableStateIndex).append(");\n");
sb.append("\t\t} else if ($1 instanceof ").append(BasicDirtyTracker.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(BasicDirtyTracker.class.getName()).append(") $1).$$_setParent($0, ").append(mutableStateIndex).append(");\n");
} else {
sb.append("\t\tif ($1 instanceof ").append(BasicDirtyTracker.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(BasicDirtyTracker.class.getName()).append(") $1).$$_setParent($0, ").append(mutableStateIndex).append(");\n");
}
sb.append("\t\t}\n");
}
sb.append("\t}\n");
}
}
}
if (!invalidSetter) {
sb.append("\t$0.").append(fieldName).append(" = $1;\n");
}
sb.append('}');
CtMethod method = CtMethod.make(minfo, cc);
method.setBody(sb.toString());
cc.addMethod(method);
return method;
}
use of com.blazebit.persistence.view.metamodel.FlatViewType 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.view.metamodel.FlatViewType in project blaze-persistence by Blazebit.
the class FlatViewTest method testMetamodel.
@Test
public void testMetamodel() {
ViewMetamodel metamodel = evm.getMetamodel();
ViewType<?> viewType = metamodel.view(DocumentFlatEmbeddingView.class);
assertEquals("id", viewType.getIdAttribute().getName());
assertTrue(metamodel.managedView(PersonFlatView.class) instanceof FlatViewType);
}
Aggregations