Search in sources :

Example 1 with AbstractPersistentEntityCriteriaUpdate

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

the class UpdateMethodMatcher method entityUpdate.

private UpdateCriteriaMethodMatch entityUpdate(java.util.regex.Matcher matcher, ParameterElement entityParameter, ParameterElement entitiesParameter) {
    return new UpdateCriteriaMethodMatch(matcher) {

        ParameterElement entityParam = entityParameter == null ? entitiesParameter : entityParameter;

        @Override
        protected <T> void applyPredicates(List<ParameterElement> parameters, PersistentEntityRoot<T> root, PersistentEntityCriteriaUpdate<T> query, SourcePersistentEntityCriteriaBuilder cb) {
            final SourcePersistentEntity rootEntity = (SourcePersistentEntity) root.getPersistentEntity();
            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));
            }
            query.where(predicate);
        }

        @Override
        protected <T> void addPropertiesToUpdate(MethodMatchContext matchContext, PersistentEntityRoot<T> root, PersistentEntityCriteriaUpdate<T> query, SourcePersistentEntityCriteriaBuilder cb) {
            final SourcePersistentEntity rootEntity = matchContext.getRootEntity();
            Stream.concat(rootEntity.getPersistentProperties().stream(), Stream.of(rootEntity.getVersion())).filter(p -> p != null && !((p instanceof Association) && ((Association) p).isForeignKey()) && !p.isGenerated() && p.findAnnotation(AutoPopulated.class).map(ap -> ap.getRequiredValue(AutoPopulated.UPDATEABLE, Boolean.class)).orElse(true)).forEach(p -> query.set(p.getName(), cb.entityPropertyParameter(entityParam)));
            if (((AbstractPersistentEntityCriteriaUpdate<T>) query).getUpdateValues().isEmpty()) {
                // Workaround for only ID entities
                query.set(rootEntity.getIdentity().getName(), cb.entityPropertyParameter(entityParam));
            }
        }

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

        @Override
        protected Map.Entry<ClassElement, Class<? extends DataInterceptor>> resolveReturnTypeAndInterceptor(MethodMatchContext matchContext) {
            Map.Entry<ClassElement, Class<? extends DataInterceptor>> e = super.resolveReturnTypeAndInterceptor(matchContext);
            ClassElement returnType = e.getKey();
            if (returnType != null && !TypeUtils.isVoid(returnType) && !TypeUtils.isNumber(returnType) && !returnType.hasStereotype(MappedEntity.class) && !(TypeUtils.isReactiveOrFuture(matchContext.getReturnType()) && TypeUtils.isObjectClass(returnType))) {
                throw new MatchFailedException("Cannot implement update method for specified return type: " + returnType.getName());
            }
            return e;
        }

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

        @Override
        protected ParameterElement getEntitiesParameter() {
            return entitiesParameter;
        }
    };
}
Also used : SourcePersistentEntityCriteriaBuilder(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder) Arrays(java.util.Arrays) Parameter(io.micronaut.context.annotation.Parameter) MappedEntity(io.micronaut.data.annotation.MappedEntity) ParameterExpression(jakarta.persistence.criteria.ParameterExpression) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) ClassElement(io.micronaut.inject.ast.ClassElement) Internal(io.micronaut.core.annotation.Internal) AbstractPersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaUpdate) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) ParameterElement(io.micronaut.inject.ast.ParameterElement) Version(io.micronaut.data.annotation.Version) Map(java.util.Map) Id(io.micronaut.data.annotation.Id) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) UpdateCriteriaMethodMatch(io.micronaut.data.processor.visitors.finders.criteria.UpdateCriteriaMethodMatch) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) Predicate(jakarta.persistence.criteria.Predicate) AutoPopulated(io.micronaut.data.annotation.AutoPopulated) Set(java.util.Set) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) Collectors(java.util.stream.Collectors) PersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaUpdate) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) List(java.util.List) Association(io.micronaut.data.model.Association) Stream(java.util.stream.Stream) MethodMatchContext(io.micronaut.data.processor.visitors.MethodMatchContext) MethodElement(io.micronaut.inject.ast.MethodElement) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) MappedEntity(io.micronaut.data.annotation.MappedEntity) ClassElement(io.micronaut.inject.ast.ClassElement) Predicate(jakarta.persistence.criteria.Predicate) SourcePersistentEntityCriteriaBuilder(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder) MethodMatchContext(io.micronaut.data.processor.visitors.MethodMatchContext) Association(io.micronaut.data.model.Association) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) AbstractPersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaUpdate) PersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaUpdate) UpdateCriteriaMethodMatch(io.micronaut.data.processor.visitors.finders.criteria.UpdateCriteriaMethodMatch) List(java.util.List) ParameterElement(io.micronaut.inject.ast.ParameterElement) Map(java.util.Map) AutoPopulated(io.micronaut.data.annotation.AutoPopulated)

Example 2 with AbstractPersistentEntityCriteriaUpdate

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

the class UpdateCriteriaMethodMatch method build.

@Override
protected MethodMatchInfo build(MethodMatchContext matchContext) {
    MethodMatchSourcePersistentEntityCriteriaBuilderImpl cb = new MethodMatchSourcePersistentEntityCriteriaBuilderImpl(matchContext);
    PersistentEntityCriteriaUpdate<Object> criteriaQuery = cb.createCriteriaUpdate(null);
    apply(matchContext, criteriaQuery.from(matchContext.getRootEntity()), criteriaQuery, cb);
    Map.Entry<ClassElement, Class<? extends DataInterceptor>> entry = resolveReturnTypeAndInterceptor(matchContext);
    ClassElement resultType = entry.getKey();
    Class<? extends DataInterceptor> interceptorType = entry.getValue();
    AbstractPersistentEntityCriteriaUpdate<?> criteriaUpdate = (AbstractPersistentEntityCriteriaUpdate<?>) criteriaQuery;
    boolean optimisticLock = criteriaUpdate.hasVersionRestriction();
    final AnnotationMetadataHierarchy annotationMetadataHierarchy = new AnnotationMetadataHierarchy(matchContext.getRepositoryClass().getAnnotationMetadata(), matchContext.getAnnotationMetadata());
    QueryBuilder queryBuilder = matchContext.getQueryBuilder();
    Map<String, Object> propertiesToUpdate = criteriaUpdate.getUpdateValues();
    QueryModel queryModel = ((AbstractPersistentEntityCriteriaUpdate) criteriaQuery).getQueryModel();
    QueryResult queryResult = queryBuilder.buildUpdate(annotationMetadataHierarchy, queryModel, propertiesToUpdate);
    return new MethodMatchInfo(DataMethod.OperationType.UPDATE, resultType, getInterceptorElement(matchContext, interceptorType)).optimisticLock(optimisticLock).queryResult(queryResult);
}
Also used : DataInterceptor(io.micronaut.data.intercept.DataInterceptor) ClassElement(io.micronaut.inject.ast.ClassElement) QueryBuilder(io.micronaut.data.model.query.builder.QueryBuilder) AnnotationMetadataHierarchy(io.micronaut.data.processor.visitors.AnnotationMetadataHierarchy) QueryModel(io.micronaut.data.model.query.QueryModel) AbstractPersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaUpdate) QueryResult(io.micronaut.data.model.query.builder.QueryResult) MethodMatchInfo(io.micronaut.data.processor.visitors.finders.MethodMatchInfo) MethodMatchSourcePersistentEntityCriteriaBuilderImpl(io.micronaut.data.processor.model.criteria.impl.MethodMatchSourcePersistentEntityCriteriaBuilderImpl) Map(java.util.Map)

Example 3 with AbstractPersistentEntityCriteriaUpdate

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

the class UpdateCriteriaMethodMatch method apply.

/**
 * Apply query match.
 *
 * @param matchContext The match context
 * @param root         The root
 * @param query        The query
 * @param cb           The criteria builder
 * @param <T>          The entity type
 */
protected <T> void apply(MethodMatchContext matchContext, PersistentEntityRoot<T> root, PersistentEntityCriteriaUpdate<T> query, SourcePersistentEntityCriteriaBuilder cb) {
    String querySequence = matcher.group(3);
    if (matcher.group(2).endsWith("By")) {
        applyPredicates(querySequence, matchContext.getParameters(), root, query, cb);
    } else {
        applyPredicates(matchContext.getParametersNotInRole(), root, query, cb);
    }
    if (query.getRestriction() == null) {
        throw new MatchFailedException("Cannot implement batch update operation that doesn't perform a query");
    }
    SourcePersistentEntity entity = matchContext.getRootEntity();
    addPropertiesToUpdate(matchContext, root, query, cb);
    // Add updatable auto-populated parameters
    entity.getPersistentProperties().stream().filter(p -> p != null && p.findAnnotation(AutoPopulated.class).map(ap -> ap.getRequiredValue(AutoPopulated.UPDATEABLE, Boolean.class)).orElse(false)).forEach(p -> query.set(p.getName(), cb.parameter((ParameterElement) null)));
    if (entity.getVersion() != null) {
        if (((AbstractPersistentEntityCriteriaUpdate<T>) query).hasVersionRestriction()) {
            query.set(entity.getVersion().getName(), cb.parameter((ParameterElement) null));
        }
    }
    if (((AbstractPersistentEntityCriteriaUpdate<T>) query).getUpdateValues().isEmpty()) {
        throw new MatchFailedException("At least one parameter required to update");
    }
}
Also used : SourcePersistentEntityCriteriaBuilder(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) AbstractCriteriaMethodMatch(io.micronaut.data.processor.visitors.finders.AbstractCriteriaMethodMatch) QueryModel(io.micronaut.data.model.query.QueryModel) AutoPopulated(io.micronaut.data.annotation.AutoPopulated) ClassElement(io.micronaut.inject.ast.ClassElement) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) AbstractPersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaUpdate) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) PersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaUpdate) ParameterElement(io.micronaut.inject.ast.ParameterElement) DataMethod(io.micronaut.data.intercept.annotation.DataMethod) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) Matcher(java.util.regex.Matcher) MethodMatchContext(io.micronaut.data.processor.visitors.MethodMatchContext) Map(java.util.Map) QueryBuilder(io.micronaut.data.model.query.builder.QueryBuilder) QueryResult(io.micronaut.data.model.query.builder.QueryResult) MethodMatchSourcePersistentEntityCriteriaBuilderImpl(io.micronaut.data.processor.model.criteria.impl.MethodMatchSourcePersistentEntityCriteriaBuilderImpl) AnnotationMetadataHierarchy(io.micronaut.data.processor.visitors.AnnotationMetadataHierarchy) Experimental(io.micronaut.core.annotation.Experimental) MethodMatchInfo(io.micronaut.data.processor.visitors.finders.MethodMatchInfo) AbstractPersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaUpdate) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) ParameterElement(io.micronaut.inject.ast.ParameterElement) AutoPopulated(io.micronaut.data.annotation.AutoPopulated)

Aggregations

DataInterceptor (io.micronaut.data.intercept.DataInterceptor)3 AbstractPersistentEntityCriteriaUpdate (io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaUpdate)3 ClassElement (io.micronaut.inject.ast.ClassElement)3 Map (java.util.Map)3 AutoPopulated (io.micronaut.data.annotation.AutoPopulated)2 PersistentEntityCriteriaUpdate (io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaUpdate)2 PersistentEntityRoot (io.micronaut.data.model.jpa.criteria.PersistentEntityRoot)2 QueryModel (io.micronaut.data.model.query.QueryModel)2 QueryBuilder (io.micronaut.data.model.query.builder.QueryBuilder)2 QueryResult (io.micronaut.data.model.query.builder.QueryResult)2 SourcePersistentEntity (io.micronaut.data.processor.model.SourcePersistentEntity)2 SourcePersistentEntityCriteriaBuilder (io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder)2 MethodMatchSourcePersistentEntityCriteriaBuilderImpl (io.micronaut.data.processor.model.criteria.impl.MethodMatchSourcePersistentEntityCriteriaBuilderImpl)2 AnnotationMetadataHierarchy (io.micronaut.data.processor.visitors.AnnotationMetadataHierarchy)2 MatchFailedException (io.micronaut.data.processor.visitors.MatchFailedException)2 MethodMatchContext (io.micronaut.data.processor.visitors.MethodMatchContext)2 MethodMatchInfo (io.micronaut.data.processor.visitors.finders.MethodMatchInfo)2 ParameterElement (io.micronaut.inject.ast.ParameterElement)2 Parameter (io.micronaut.context.annotation.Parameter)1 Experimental (io.micronaut.core.annotation.Experimental)1