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;
}
};
}
Aggregations