Search in sources :

Example 1 with DeleteCriteriaMethodMatch

use of io.micronaut.data.processor.visitors.finders.criteria.DeleteCriteriaMethodMatch in project micronaut-data by micronaut-projects.

the class DeleteMethodMatcher method match.

@Override
public MethodMatch match(MethodMatchContext matchContext, java.util.regex.Matcher matcher) {
    ParameterElement[] parameters = matchContext.getParameters();
    boolean isSpecificDelete = matcher.group(2).endsWith("By");
    ParameterElement entityParameter = null;
    ParameterElement entitiesParameter = null;
    if (matchContext.getParametersNotInRole().size() == 1) {
        entityParameter = Arrays.stream(parameters).filter(p -> TypeUtils.isEntity(p.getGenericType())).findFirst().orElse(null);
        entitiesParameter = Arrays.stream(parameters).filter(p -> TypeUtils.isIterableOfEntity(p.getGenericType())).findFirst().orElse(null);
    }
    if (isSpecificDelete) {
        // Un-mark the entity parameter if there is a property named the same and 'By' syntax is used
        if (entityParameter != null) {
            if (matchContext.getRootEntity().getPropertyByName(getName(entityParameter)) != null) {
                entityParameter = null;
            }
        }
        if (entitiesParameter != null) {
            if (matchContext.getRootEntity().getPropertyByName(getName(entitiesParameter)) != null) {
                entitiesParameter = null;
            }
        }
    }
    if (entityParameter == null && entitiesParameter == null) {
        if (!TypeUtils.isValidBatchUpdateReturnType(matchContext.getMethodElement())) {
            return null;
        }
        return new DeleteCriteriaMethodMatch(matcher);
    }
    SourcePersistentEntity rootEntity = matchContext.getRootEntity();
    if (!rootEntity.hasIdentity() && !rootEntity.hasCompositeIdentity()) {
        throw new MatchFailedException("Delete all not supported for entities with no ID");
    }
    boolean supportedByImplicitQueries = !matcher.group(2).endsWith("By");
    boolean generateInIdList = entitiesParameter != null && !rootEntity.hasCompositeIdentity() && !(rootEntity.getIdentity() instanceof Embedded) && rootEntity.getVersion() == null;
    ParameterElement finalEntityParameter = entityParameter;
    ParameterElement finalEntitiesParameter = entitiesParameter;
    if (generateInIdList) {
        return new DeleteCriteriaMethodMatch(matcher) {

            @Override
            protected boolean supportedByImplicitQueries() {
                return supportedByImplicitQueries;
            }

            @Override
            protected <T> void applyPredicates(List<ParameterElement> parameters, PersistentEntityRoot<T> root, PersistentEntityCriteriaDelete<T> query, SourcePersistentEntityCriteriaBuilder cb) {
                Predicate restriction = query.getRestriction();
                Predicate predicate = root.id().in(cb.entityPropertyParameter(finalEntitiesParameter));
                if (restriction == null) {
                    query.where(predicate);
                } else {
                    query.where(cb.and(predicate, restriction));
                }
            }

            @Override
            protected ParameterElement getEntityParameter() {
                return finalEntityParameter;
            }

            @Override
            protected ParameterElement getEntitiesParameter() {
                return finalEntitiesParameter;
            }
        };
    }
    ParameterElement entityParam = entityParameter == null ? entitiesParameter : entityParameter;
    return new DeleteCriteriaMethodMatch(matcher) {

        @Override
        protected boolean supportedByImplicitQueries() {
            return supportedByImplicitQueries;
        }

        @Override
        protected <T> void applyPredicates(List<ParameterElement> parameters, PersistentEntityRoot<T> root, PersistentEntityCriteriaDelete<T> query, SourcePersistentEntityCriteriaBuilder cb) {
            Predicate restriction = query.getRestriction();
            Predicate predicate;
            if (rootEntity.getVersion() != null) {
                predicate = cb.and(cb.equal(root.id(), cb.entityPropertyParameter(entityParam)), cb.equal(root.version(), cb.entityPropertyParameter(entityParam)));
            } else {
                predicate = cb.equal(root.id(), cb.entityPropertyParameter(entityParam));
            }
            if (restriction == null) {
                query.where(predicate);
            } else {
                query.where(cb.and(predicate, restriction));
            }
        }

        @Override
        protected ParameterElement getEntityParameter() {
            return finalEntityParameter;
        }

        @Override
        protected ParameterElement getEntitiesParameter() {
            return finalEntitiesParameter;
        }
    };
}
Also used : SourcePersistentEntityCriteriaBuilder(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder) Arrays(java.util.Arrays) Parameter(io.micronaut.context.annotation.Parameter) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) DeleteCriteriaMethodMatch(io.micronaut.data.processor.visitors.finders.criteria.DeleteCriteriaMethodMatch) Predicate(jakarta.persistence.criteria.Predicate) Internal(io.micronaut.core.annotation.Internal) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) ParameterElement(io.micronaut.inject.ast.ParameterElement) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) Embedded(io.micronaut.data.model.Embedded) List(java.util.List) MethodMatchContext(io.micronaut.data.processor.visitors.MethodMatchContext) PersistentEntityCriteriaDelete(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaDelete) Pattern(java.util.regex.Pattern) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) Predicate(jakarta.persistence.criteria.Predicate) SourcePersistentEntityCriteriaBuilder(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) PersistentEntityCriteriaDelete(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaDelete) List(java.util.List) ParameterElement(io.micronaut.inject.ast.ParameterElement) DeleteCriteriaMethodMatch(io.micronaut.data.processor.visitors.finders.criteria.DeleteCriteriaMethodMatch) Embedded(io.micronaut.data.model.Embedded)

Aggregations

Parameter (io.micronaut.context.annotation.Parameter)1 Internal (io.micronaut.core.annotation.Internal)1 Embedded (io.micronaut.data.model.Embedded)1 PersistentEntityCriteriaDelete (io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaDelete)1 PersistentEntityRoot (io.micronaut.data.model.jpa.criteria.PersistentEntityRoot)1 SourcePersistentEntity (io.micronaut.data.processor.model.SourcePersistentEntity)1 SourcePersistentEntityCriteriaBuilder (io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder)1 MatchFailedException (io.micronaut.data.processor.visitors.MatchFailedException)1 MethodMatchContext (io.micronaut.data.processor.visitors.MethodMatchContext)1 DeleteCriteriaMethodMatch (io.micronaut.data.processor.visitors.finders.criteria.DeleteCriteriaMethodMatch)1 ParameterElement (io.micronaut.inject.ast.ParameterElement)1 Predicate (jakarta.persistence.criteria.Predicate)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1