Search in sources :

Example 1 with CTEProvider

use of com.blazebit.persistence.view.CTEProvider in project blaze-persistence by Blazebit.

the class WithCTEMappingTest method testCteBindingWithSubview.

@Test
public void testCteBindingWithSubview() {
    EntityViewManager evm = build(DocumentWithCTE.class, PersonWithPartnerDocument.class, PersonWithPartnerDocumentFullAged.class, PersonWithPartnerDocumentUnderAged.class);
    CriteriaBuilder<Person> cb = cbf.create(em, Person.class).orderByAsc("id");
    EntityViewSetting<PersonWithPartnerDocument, CriteriaBuilder<PersonWithPartnerDocument>> setting;
    setting = EntityViewSetting.create(PersonWithPartnerDocument.class);
    List<PersonWithPartnerDocument> list = evm.applySetting(setting, cb).getResultList();
    assertEquals(3, list.size());
    assertEquals("doc1", list.get(0).getPartnerDocument().getName());
    assertEquals(Long.valueOf(1), list.get(0).getPartnerDocument().getOwnedDocumentCount());
    boolean containsProvider = false;
    ManagedViewType<?> viewType = evm.getMetamodel().managedView(PersonWithPartnerDocumentFullAged.class);
    for (CTEProvider provider : viewType.getCteProviders()) {
        if (provider instanceof FullAgedCTEProvider) {
            containsProvider = true;
        }
    }
    assertTrue(containsProvider);
}
Also used : CriteriaBuilder(com.blazebit.persistence.CriteriaBuilder) FullAgedCTEProvider(com.blazebit.persistence.view.testsuite.cte.model.PersonWithPartnerDocumentFullAged.FullAgedCTEProvider) CTEProvider(com.blazebit.persistence.view.CTEProvider) FullAgedCTEProvider(com.blazebit.persistence.view.testsuite.cte.model.PersonWithPartnerDocumentFullAged.FullAgedCTEProvider) EntityViewManager(com.blazebit.persistence.view.EntityViewManager) PersonWithPartnerDocument(com.blazebit.persistence.view.testsuite.cte.model.PersonWithPartnerDocument) Person(com.blazebit.persistence.testsuite.entity.Person) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Example 2 with CTEProvider

use of com.blazebit.persistence.view.CTEProvider 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LockOwner(com.blazebit.persistence.view.LockOwner) LinkedHashMap(java.util.LinkedHashMap) With(com.blazebit.persistence.view.With) EntityViewRoots(com.blazebit.persistence.view.EntityViewRoots) ViewFilter(com.blazebit.persistence.view.ViewFilter) CreatableEntityView(com.blazebit.persistence.view.CreatableEntityView) ViewFilters(com.blazebit.persistence.view.ViewFilters) CTEProvider(com.blazebit.persistence.view.CTEProvider) UpdatableEntityView(com.blazebit.persistence.view.UpdatableEntityView) EntityView(com.blazebit.persistence.view.EntityView) UpdatableEntityView(com.blazebit.persistence.view.UpdatableEntityView) CreatableEntityView(com.blazebit.persistence.view.CreatableEntityView) EntityViewInheritanceMapping(com.blazebit.persistence.view.EntityViewInheritanceMapping) EntityViewRoot(com.blazebit.persistence.view.EntityViewRoot) Annotation(java.lang.annotation.Annotation) BatchFetch(com.blazebit.persistence.view.BatchFetch) ViewFilterProvider(com.blazebit.persistence.view.ViewFilterProvider) CtClass(javassist.CtClass) EntityViewRootMapping(com.blazebit.persistence.view.spi.EntityViewRootMapping) EntityViewInheritance(com.blazebit.persistence.view.EntityViewInheritance)

Example 3 with CTEProvider

use of com.blazebit.persistence.view.CTEProvider in project blaze-persistence by Blazebit.

the class ManagedViewTypeImpl method renderSecondaryMappings.

@Override
public void renderSecondaryMappings(String viewPath, BaseQueryBuilder<?, ?> baseQueryBuilder, Map<String, Object> optionalParameters, boolean renderFetches) {
    if (baseQueryBuilder instanceof CTEBuilder) {
        CTEBuilder<?> cteBuilder = (CTEBuilder<?>) baseQueryBuilder;
        for (CTEProvider cteProvider : getCteProviders()) {
            cteProvider.applyCtes(cteBuilder, optionalParameters);
        }
    }
    for (ViewRoot viewRoot : viewRoots) {
        String entityViewRootName = viewRoot.getName();
        CorrelationProvider correlationProvider = viewRoot.getCorrelationProviderFactory().create(baseQueryBuilder, optionalParameters);
        ExpressionFactory expressionFactory = baseQueryBuilder.getService(ExpressionFactory.class);
        Limiter limiter = createLimiter(expressionFactory, viewPath, viewRoot.getLimitExpression(), viewRoot.getOffsetExpression(), viewRoot.getOrderByItems());
        String correlationAlias;
        if (limiter == null) {
            correlationAlias = entityViewRootName;
        } else {
            correlationAlias = "_sub_" + entityViewRootName;
        }
        JoinCorrelationBuilder correlationBuilder = new JoinCorrelationBuilder(baseQueryBuilder, optionalParameters, baseQueryBuilder, viewPath, correlationAlias, entityViewRootName, null, viewRoot.getJoinType(), limiter);
        correlationProvider.applyCorrelation(correlationBuilder, viewPath);
        correlationBuilder.finish();
        if (renderFetches && baseQueryBuilder instanceof FetchBuilder<?>) {
            ((FetchBuilder<?>) baseQueryBuilder).fetch(viewRoot.getFetches());
        }
    }
}
Also used : CTEProvider(com.blazebit.persistence.view.CTEProvider) JoinCorrelationBuilder(com.blazebit.persistence.view.impl.objectbuilder.transformer.correlation.JoinCorrelationBuilder) FetchBuilder(com.blazebit.persistence.FetchBuilder) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) ViewRoot(com.blazebit.persistence.view.metamodel.ViewRoot) StaticCorrelationProvider(com.blazebit.persistence.view.impl.StaticCorrelationProvider) CorrelationProvider(com.blazebit.persistence.view.CorrelationProvider) StaticPathCorrelationProvider(com.blazebit.persistence.view.impl.StaticPathCorrelationProvider) CTEBuilder(com.blazebit.persistence.CTEBuilder) Limiter(com.blazebit.persistence.view.impl.objectbuilder.Limiter)

Aggregations

CTEProvider (com.blazebit.persistence.view.CTEProvider)3 CTEBuilder (com.blazebit.persistence.CTEBuilder)1 CriteriaBuilder (com.blazebit.persistence.CriteriaBuilder)1 FetchBuilder (com.blazebit.persistence.FetchBuilder)1 ExpressionFactory (com.blazebit.persistence.parser.expression.ExpressionFactory)1 Person (com.blazebit.persistence.testsuite.entity.Person)1 BatchFetch (com.blazebit.persistence.view.BatchFetch)1 CorrelationProvider (com.blazebit.persistence.view.CorrelationProvider)1 CreatableEntityView (com.blazebit.persistence.view.CreatableEntityView)1 EntityView (com.blazebit.persistence.view.EntityView)1 EntityViewInheritance (com.blazebit.persistence.view.EntityViewInheritance)1 EntityViewInheritanceMapping (com.blazebit.persistence.view.EntityViewInheritanceMapping)1 EntityViewManager (com.blazebit.persistence.view.EntityViewManager)1 EntityViewRoot (com.blazebit.persistence.view.EntityViewRoot)1 EntityViewRoots (com.blazebit.persistence.view.EntityViewRoots)1 LockOwner (com.blazebit.persistence.view.LockOwner)1 UpdatableEntityView (com.blazebit.persistence.view.UpdatableEntityView)1 ViewFilter (com.blazebit.persistence.view.ViewFilter)1 ViewFilterProvider (com.blazebit.persistence.view.ViewFilterProvider)1 ViewFilters (com.blazebit.persistence.view.ViewFilters)1