Skip to content

Commit

Permalink
Convert predicates from OperationPredicates to Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vierbergenlars committed Aug 29, 2023
1 parent 31825f7 commit 44e667f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.repository.RepositoryInvokerAdapterFactory;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import java.util.Optional;
import lombok.RequiredArgsConstructor;

/**
Expand All @@ -20,40 +21,40 @@ public class CollectionFilteringOperationPredicates implements OperationPredicat
public OperationPredicates and(OperationPredicates predicate) {
if (predicate instanceof CollectionFilteringOperationPredicates) {
return new CollectionFilteringOperationPredicates(ExpressionUtils.and(
this.collectionFilterPredicate(),
predicate.collectionFilterPredicate()
this.collectionFilterPredicate().orElse(null),
predicate.collectionFilterPredicate().orElse(null)
));
}
return OperationPredicates.super.and(predicate);
}

@Override
public Predicate collectionFilterPredicate() {
return predicate;
public Optional<Predicate> collectionFilterPredicate() {
return Optional.ofNullable(predicate);
}

@Override
public Predicate readPredicate() {
return null;
public Optional<Predicate> readPredicate() {
return Optional.empty();
}

@Override
public Predicate afterCreatePredicate() {
return null;
public Optional<Predicate> afterCreatePredicate() {
return Optional.empty();
}

@Override
public Predicate beforeUpdatePredicate() {
return null;
public Optional<Predicate> beforeUpdatePredicate() {
return Optional.empty();
}

@Override
public Predicate afterUpdatePredicate() {
return null;
public Optional<Predicate> afterUpdatePredicate() {
return Optional.empty();
}

@Override
public Predicate beforeDeletePredicate() {
return null;
public Optional<Predicate> beforeDeletePredicate() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.querydsl.core.types.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;
import org.springframework.lang.Nullable;
Expand All @@ -19,41 +20,40 @@ public OperationPredicates and(OperationPredicates predicate) {
return new CompositeOperationPredicates(copy);
}

@Nullable
private Predicate combine(Function<OperationPredicates, Predicate> extractor) {
private Optional<Predicate> combine(Function<OperationPredicates, Optional<Predicate>> extractor) {
return predicates.stream()
.map(extractor)
.reduce(ExpressionUtils::and)
.orElse(null);
.flatMap(Optional::stream)
.reduce(ExpressionUtils::and);
}

@Override
public Predicate collectionFilterPredicate() {
public Optional<Predicate> collectionFilterPredicate() {
return combine(OperationPredicates::collectionFilterPredicate);
}

@Override
public Predicate readPredicate() {
public Optional<Predicate> readPredicate() {
return combine(OperationPredicates::readPredicate);
}

@Override
public Predicate afterCreatePredicate() {
public Optional<Predicate> afterCreatePredicate() {
return combine(OperationPredicates::afterCreatePredicate);
}

@Override
public Predicate beforeUpdatePredicate() {
public Optional<Predicate> beforeUpdatePredicate() {
return combine(OperationPredicates::beforeUpdatePredicate);
}

@Override
public Predicate afterUpdatePredicate() {
public Optional<Predicate> afterUpdatePredicate() {
return combine(OperationPredicates::afterUpdatePredicate);
}

@Override
public Predicate beforeDeletePredicate() {
public Optional<Predicate> beforeDeletePredicate() {
return combine(OperationPredicates::beforeDeletePredicate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.querydsl.core.types.Predicate;
import java.util.List;
import java.util.Optional;
import org.springframework.lang.Nullable;

/**
Expand All @@ -22,36 +23,30 @@ default OperationPredicates and(OperationPredicates predicate) {
/**
* @return Predicate used for filtering a collection of entities
*/
@Nullable
Predicate collectionFilterPredicate();
Optional<Predicate> collectionFilterPredicate();

/**
* @return Predicate used for reading a single entity
*/
@Nullable
Predicate readPredicate();
Optional<Predicate> readPredicate();

/**
* @return Predicate used to check permissions for creating an entity with certain values
*/
@Nullable
Predicate afterCreatePredicate();
Optional<Predicate> afterCreatePredicate();

/**
* @return Predicate used to check permission before updating an entity
*/
@Nullable
Predicate beforeUpdatePredicate();
Optional<Predicate> beforeUpdatePredicate();

/**
* @return Predicate used to check permissions for updating an entity with certain values
*/
@Nullable
Predicate afterUpdatePredicate();
Optional<Predicate> afterUpdatePredicate();

/**
* @return Predicate used for deleting an entity
*/
@Nullable
Predicate beforeDeletePredicate();
Optional<Predicate> beforeDeletePredicate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.resolver.OperationPredicates;
import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.resolver.CollectionFilteringOperationPredicates;
import com.querydsl.core.types.Predicate;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.QuerydslRepositoryInvokerAdapter;
Expand All @@ -25,12 +26,15 @@ public RepositoryInvoker adaptRepositoryInvoker(RepositoryInvoker invoker, Class
return repositories.getRepositoryFor(domainType)
.filter(QuerydslPredicateExecutor.class::isInstance)
.map(QuerydslPredicateExecutor.class::cast)
.<RepositoryInvoker>map(
it -> new QuerydslRepositoryInvokerAdapter(invoker, it, unwrapQuerydslPredicates(predicate)))
.<RepositoryInvoker>flatMap(predicateExecutor -> {
return unwrapQuerydslPredicates(predicate)
.map(collectionFilterPredicate -> new QuerydslRepositoryInvokerAdapter(invoker,
predicateExecutor, collectionFilterPredicate));
})
.orElse(invoker);
}

private Predicate unwrapQuerydslPredicates(OperationPredicates operationPredicates) {
private Optional<Predicate> unwrapQuerydslPredicates(OperationPredicates operationPredicates) {
if (operationPredicates instanceof CollectionFilteringOperationPredicates) {
return operationPredicates.collectionFilterPredicate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.resolver.OperationPredicates;
import com.querydsl.core.types.Predicate;
import java.util.Optional;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
Expand All @@ -10,32 +11,32 @@ class AllOperationPredicates implements OperationPredicates {
private final Predicate sharedPredicate;

@Override
public Predicate collectionFilterPredicate() {
public Optional<Predicate> collectionFilterPredicate() {
return sharedPredicate;
}

@Override
public Predicate readPredicate() {
public Optional<Predicate> readPredicate() {
return sharedPredicate;
}

@Override
public Predicate afterCreatePredicate() {
public Optional<Predicate> afterCreatePredicate() {
return sharedPredicate;
}

@Override
public Predicate beforeUpdatePredicate() {
public Optional<Predicate> beforeUpdatePredicate() {
return sharedPredicate;
}

@Override
public Predicate afterUpdatePredicate() {
public Optional<Predicate> afterUpdatePredicate() {
return sharedPredicate;
}

@Override
public Predicate beforeDeletePredicate() {
public Optional<Predicate> beforeDeletePredicate() {
return sharedPredicate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public AbacRepositoryInvokerAdapter(
PathBuilder<?> pathBuilder,
ConversionService conversionService
) {
super(delegate, executor, predicate.collectionFilterPredicate());
super(delegate, executor, predicate.collectionFilterPredicate().orElse(new BooleanBuilder()));
this.executor = executor;
this.predicate = predicate;
this.transactionManager = transactionManager;
Expand All @@ -99,9 +99,9 @@ public <T> Optional<T> invokeFindById(Object id) {
return invokeFindById(id, predicate.readPredicate());
}

private <T> Optional<T> invokeFindById(Object id, Predicate predicate) {
private <T> Optional<T> invokeFindById(Object id, Optional<Predicate> predicate) {
BooleanBuilder builder = new BooleanBuilder();
builder.and(predicate);
predicate.ifPresent(builder::and);

var entityIdPath = pathBuilder.get(this.idName, this.idPropertyType);
Assert.notNull(entityIdPath, "id expression cannot be null");
Expand Down Expand Up @@ -159,7 +159,7 @@ public <T> T invokeSave(T object) {

var maybePreSaveId = idFunction.apply(object);

Predicate postSavePredicate;
Optional<Predicate> postSavePredicate;
// when object has no id, there is no pre-save-check required, because it is a newly created entity
// when object has an 'id', do:
// 1. invokeFindById without predicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.verification.VerificationMode;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.support.RepositoryInvoker;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
Expand Down Expand Up @@ -65,32 +64,32 @@ private static class TestOperationPredicates implements OperationPredicates {
private final Predicate predicate;

@Override
public Predicate collectionFilterPredicate() {
public Optional<Predicate> collectionFilterPredicate() {
return predicate;
}

@Override
public Predicate readPredicate() {
public Optional<Predicate> readPredicate() {
return predicate;
}

@Override
public Predicate afterCreatePredicate() {
public Optional<Predicate> afterCreatePredicate() {
return predicate;
}

@Override
public Predicate beforeUpdatePredicate() {
public Optional<Predicate> beforeUpdatePredicate() {
return predicate;
}

@Override
public Predicate afterUpdatePredicate() {
public Optional<Predicate> afterUpdatePredicate() {
return predicate;
}

@Override
public Predicate beforeDeletePredicate() {
public Optional<Predicate> beforeDeletePredicate() {
return predicate;
}
}
Expand Down

0 comments on commit 44e667f

Please sign in to comment.