Search in sources :

Example 1 with PersistentEntityCriteriaQuery

use of io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaQuery in project micronaut-data by micronaut-projects.

the class FindMethodMatcher method match.

@Override
protected MethodMatch match(MethodMatchContext matchContext, java.util.regex.Matcher matcher) {
    if (isCompatibleReturnType(matchContext)) {
        return new QueryCriteriaMethodMatch(matcher) {

            boolean hasIdMatch;

            @Override
            protected <T> void apply(MethodMatchContext matchContext, PersistentEntityRoot<T> root, PersistentEntityCriteriaQuery<T> query, SourcePersistentEntityCriteriaBuilder cb) {
                super.apply(matchContext, root, query, cb);
                if (query instanceof AbstractPersistentEntityCriteriaQuery) {
                    hasIdMatch = ((AbstractPersistentEntityCriteriaQuery<T>) query).hasOnlyIdRestriction();
                }
            }

            @Override
            protected Map.Entry<ClassElement, Class<? extends DataInterceptor>> resolveReturnTypeAndInterceptor(MethodMatchContext matchContext) {
                Map.Entry<ClassElement, Class<? extends DataInterceptor>> e = super.resolveReturnTypeAndInterceptor(matchContext);
                Class<? extends DataInterceptor> interceptorType = e.getValue();
                ClassElement queryResultType = e.getKey();
                if (isFindByIdQuery(matchContext, queryResultType)) {
                    if (interceptorType == FindOneInterceptor.class) {
                        interceptorType = FindByIdInterceptor.class;
                    } else if (interceptorType == FindOneAsyncInterceptor.class) {
                        interceptorType = FindByIdAsyncInterceptor.class;
                    } else if (interceptorType == FindOneReactiveInterceptor.class) {
                        interceptorType = FindByIdReactiveInterceptor.class;
                    }
                }
                return new AbstractMap.SimpleEntry<>(queryResultType, interceptorType);
            }

            private boolean isFindByIdQuery(@NonNull MethodMatchContext matchContext, @NonNull ClassElement queryResultType) {
                return hasIdMatch && matchContext.supportsImplicitQueries() && queryResultType.getName().equals(matchContext.getRootEntity().getName()) && hasNoWhereAndJoinDeclaration(matchContext);
            }
        };
    }
    return null;
}
Also used : QueryCriteriaMethodMatch(io.micronaut.data.processor.visitors.finders.criteria.QueryCriteriaMethodMatch) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) FindByIdAsyncInterceptor(io.micronaut.data.intercept.async.FindByIdAsyncInterceptor) ClassElement(io.micronaut.inject.ast.ClassElement) PersistentEntityCriteriaQuery(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaQuery) AbstractPersistentEntityCriteriaQuery(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaQuery) SourcePersistentEntityCriteriaBuilder(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder) MethodMatchContext(io.micronaut.data.processor.visitors.MethodMatchContext) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) NonNull(io.micronaut.core.annotation.NonNull) AbstractPersistentEntityCriteriaQuery(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaQuery) AbstractMap(java.util.AbstractMap) Map(java.util.Map) FindOneAsyncInterceptor(io.micronaut.data.intercept.async.FindOneAsyncInterceptor)

Aggregations

NonNull (io.micronaut.core.annotation.NonNull)1 DataInterceptor (io.micronaut.data.intercept.DataInterceptor)1 FindByIdAsyncInterceptor (io.micronaut.data.intercept.async.FindByIdAsyncInterceptor)1 FindOneAsyncInterceptor (io.micronaut.data.intercept.async.FindOneAsyncInterceptor)1 PersistentEntityCriteriaQuery (io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaQuery)1 PersistentEntityRoot (io.micronaut.data.model.jpa.criteria.PersistentEntityRoot)1 AbstractPersistentEntityCriteriaQuery (io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaQuery)1 SourcePersistentEntityCriteriaBuilder (io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder)1 MethodMatchContext (io.micronaut.data.processor.visitors.MethodMatchContext)1 QueryCriteriaMethodMatch (io.micronaut.data.processor.visitors.finders.criteria.QueryCriteriaMethodMatch)1 ClassElement (io.micronaut.inject.ast.ClassElement)1 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1