use of com.blazebit.persistence.view.BatchFetch in project blaze-persistence by Blazebit.
the class AbstractAnnotationAttributeMappingReader method applyCommonMappings.
protected void applyCommonMappings(AttributeMapping attributeMapping, AnnotatedElement annotatedElement) {
CollectionMapping collectionMapping = annotatedElement.getAnnotation(CollectionMapping.class);
Class<?> collectionType = attributeMapping.getDeclaredType();
if (collectionMapping != null && collectionMapping.ignoreIndex() && Map.class.isAssignableFrom(collectionType)) {
context.addError("Illegal ignoreIndex mapping for the " + attributeMapping.getErrorLocation());
}
if (collectionMapping != null) {
Class<? extends Comparator<?>> comparatorClass;
Class<?> c = collectionMapping.comparator();
if (c == Comparator.class) {
comparatorClass = null;
} else {
comparatorClass = (Class<? extends Comparator<?>>) c;
}
if (comparatorClass != null || MetamodelUtils.isSorted(collectionType)) {
if (collectionMapping.ignoreIndex()) {
context.addError("Illegal ignoreIndex mapping for the sorted " + attributeMapping.getErrorLocation());
}
if (collectionMapping.ordered()) {
context.addError("Illegal ordered mapping for the sorted " + attributeMapping.getErrorLocation());
}
attributeMapping.setContainerSorted(comparatorClass);
} else if (collectionType == List.class) {
// Except if the ignore index flag is set
if (collectionMapping.ignoreIndex()) {
attributeMapping.setContainerDefault();
}
} else if (collectionMapping.ordered()) {
attributeMapping.setContainerOrdered();
} else {
attributeMapping.setContainerDefault();
}
attributeMapping.setForceUniqueness(collectionMapping.forceUnique());
} else {
// List types have to be resolved during building against the metamodel
if (collectionType != List.class) {
if (MetamodelUtils.isSorted(collectionType)) {
attributeMapping.setContainerSorted(null);
} else {
attributeMapping.setContainerDefault();
}
}
}
MultiCollectionMapping multiCollectionMapping = annotatedElement.getAnnotation(MultiCollectionMapping.class);
PluralAttribute.ElementCollectionType elementCollectionType = attributeMapping.getElementCollectionType();
if (multiCollectionMapping != null && elementCollectionType == null) {
context.addError("Illegal @MultiCollectionMapping mapping on non-multi collection " + attributeMapping.getErrorLocation());
}
if (multiCollectionMapping != null) {
Class<? extends Comparator<?>> comparatorClass;
Class<?> c = multiCollectionMapping.comparator();
if (c == Comparator.class) {
comparatorClass = null;
} else {
comparatorClass = (Class<? extends Comparator<?>>) c;
}
if (comparatorClass != null || elementCollectionType == PluralAttribute.ElementCollectionType.SORTED_SET) {
if (multiCollectionMapping.ordered()) {
context.addError("Illegal ordered mapping for the sorted element collection " + attributeMapping.getErrorLocation());
}
attributeMapping.setElementCollectionSorted(comparatorClass);
} else if (multiCollectionMapping.ordered()) {
attributeMapping.setElementCollectionOrdered();
} else {
attributeMapping.setElementCollectionDefault();
}
attributeMapping.setElementCollectionForceUniqueness(multiCollectionMapping.forceUnique());
} else if (elementCollectionType == PluralAttribute.ElementCollectionType.SORTED_SET) {
attributeMapping.setContainerSorted(null);
} else if (elementCollectionType != null) {
attributeMapping.setContainerDefault();
}
BatchFetch batchFetch = annotatedElement.getAnnotation(BatchFetch.class);
if (batchFetch != null) {
attributeMapping.setDefaultBatchSize(batchFetch.size());
}
EmptyFlatViewCreation emptyFlatViewCreation = annotatedElement.getAnnotation(EmptyFlatViewCreation.class);
if (emptyFlatViewCreation != null) {
attributeMapping.setCreateEmptyFlatViews(emptyFlatViewCreation.value());
}
Limit limit = annotatedElement.getAnnotation(Limit.class);
if (limit != null) {
attributeMapping.setLimit(limit.limit(), limit.offset(), Arrays.asList(limit.order()));
}
}
use of com.blazebit.persistence.view.BatchFetch in project blaze-persistence by Blazebit.
the class AnnotationMappingReader method readViewMapping.
@Override
public ViewMapping readViewMapping(Class<?> entityViewClass) {
ViewMapping existingMapping = context.getViewMapping(entityViewClass);
if (existingMapping != null) {
return existingMapping;
}
EntityView entityView = AnnotationUtils.findAnnotation(entityViewClass, EntityView.class);
if (entityView == null) {
return null;
}
Class<?> entityClass = entityView.value();
ViewMapping viewMapping = new ViewMappingImpl(entityViewClass, entityClass, context);
context.addViewMapping(entityViewClass, viewMapping);
boolean isAbstract = entityViewClass.isInterface() || Modifier.isAbstract(entityViewClass.getModifiers());
BatchFetch batchFetch = AnnotationUtils.findAnnotation(entityViewClass, BatchFetch.class);
if (batchFetch != null) {
viewMapping.setDefaultBatchSize(batchFetch.size());
}
Set<Class<? extends CTEProvider>> cteProviders = new LinkedHashSet<>();
Map<String, Class<? extends ViewFilterProvider>> viewFilterProviders = new HashMap<>();
Map<String, EntityViewRootMapping> viewRootMappings = new LinkedHashMap<>();
for (Annotation a : AnnotationUtils.getAllAnnotations(entityViewClass)) {
if (a instanceof With) {
cteProviders.addAll(Arrays.asList(((With) a).value()));
} else if (a instanceof ViewFilter) {
ViewFilter viewFilter = (ViewFilter) a;
addFilterMapping(viewFilter.name(), viewFilter.value(), viewFilterProviders, entityViewClass, context);
} else if (a instanceof ViewFilters) {
ViewFilters viewFilters = (ViewFilters) a;
for (ViewFilter viewFilter : viewFilters.value()) {
addFilterMapping(viewFilter.name(), viewFilter.value(), viewFilterProviders, entityViewClass, context);
}
} else if (a instanceof EntityViewRoot) {
EntityViewRoot entityViewRoot = (EntityViewRoot) a;
addEntityViewRootMapping(entityViewRoot, viewRootMappings, entityViewClass, context);
} else if (a instanceof EntityViewRoots) {
EntityViewRoots entityViewRoots = (EntityViewRoots) a;
for (EntityViewRoot entityViewRoot : entityViewRoots.value()) {
addEntityViewRootMapping(entityViewRoot, viewRootMappings, entityViewClass, context);
}
}
}
viewMapping.setCteProviders(cteProviders);
viewMapping.setViewFilterProviders(viewFilterProviders);
viewMapping.setEntityViewRoots(viewRootMappings.isEmpty() ? Collections.<EntityViewRootMapping>emptySet() : new LinkedHashSet<>(viewRootMappings.values()));
UpdatableEntityView updatableEntityView = AnnotationUtils.findAnnotation(entityViewClass, UpdatableEntityView.class);
if (updatableEntityView != null) {
if (isAbstract) {
viewMapping.setUpdatable(true);
viewMapping.setFlushMode(updatableEntityView.mode());
viewMapping.setFlushStrategy(updatableEntityView.strategy());
viewMapping.setLockMode(updatableEntityView.lockMode());
} else {
context.addError("Only abstract class entity views can be updatable! Remove the @UpdatableEntityView annotation from the entity view class '" + entityViewClass.getName() + "' or make it abstract.");
}
}
CreatableEntityView creatableEntityView = AnnotationUtils.findAnnotation(entityViewClass, CreatableEntityView.class);
if (creatableEntityView != null) {
if (isAbstract) {
viewMapping.setCreatable(true);
viewMapping.setValidatePersistability(creatableEntityView.validatePersistability());
viewMapping.getExcludedAttributes().addAll(Arrays.asList(creatableEntityView.excludedEntityAttributes()));
} else {
context.addError("Only abstract class entity views can be creatable! Remove the @CreatableEntityView annotation from the entity view class '" + entityViewClass.getName() + "' or make it abstract.");
}
}
if (updatableEntityView != null || creatableEntityView != null) {
LockOwner lockOwner = AnnotationUtils.findAnnotation(entityViewClass, LockOwner.class);
if (lockOwner != null) {
viewMapping.setLockOwner(lockOwner.value());
}
} else {
viewMapping.setLockMode(LockMode.NONE);
}
// Inheritance
// Note that the usage of Class.getAnnotation is on purpose
// For the full discussion see: https://github.com/Blazebit/blaze-persistence/issues/475
EntityViewInheritance inheritanceAnnotation = entityViewClass.getAnnotation(EntityViewInheritance.class);
EntityViewInheritanceMapping inheritanceMappingAnnotation = entityViewClass.getAnnotation(EntityViewInheritanceMapping.class);
String inheritanceMapping;
if (inheritanceMappingAnnotation != null) {
inheritanceMapping = inheritanceMappingAnnotation.value();
} else {
inheritanceMapping = null;
}
if (isAbstract) {
viewMapping.setInheritanceMapping(inheritanceMapping);
if (inheritanceAnnotation == null) {
viewMapping.setInheritanceSubtypesResolved(true);
} else if (inheritanceAnnotation.value().length > 0) {
viewMapping.getInheritanceSubtypeClasses().addAll(Arrays.asList(inheritanceAnnotation.value()));
viewMapping.setInheritanceSubtypesResolved(true);
}
} else if (inheritanceMapping != null || inheritanceAnnotation != null) {
context.addError("Only abstract class entity views can use inheritance mappings! Remove the inheritance annotations from the entity view class '" + entityViewClass.getName() + "' or make it abstract.");
} else {
viewMapping.setInheritanceSubtypesResolved(true);
}
// Attributes
if (isAbstract) {
readAbstractClassMappings(entityViewClass, viewMapping);
} else {
readClassMappings(entityViewClass, viewMapping);
}
return viewMapping;
}
Aggregations