AuditedModelFilter.java

  1. package org.gringlobal.service.filter;

  2. import java.time.Instant;
  3. import java.util.List;
  4. import java.util.Set;

  5. import lombok.EqualsAndHashCode;
  6. import lombok.Getter;
  7. import lombok.Setter;
  8. import lombok.experimental.Accessors;
  9. import org.apache.commons.collections4.CollectionUtils;
  10. import org.genesys.blocks.model.filters.EmptyModelFilter;
  11. import org.genesys.blocks.model.filters.TemporalFilter;
  12. import org.gringlobal.model.AuditedModel;
  13. import org.gringlobal.model.QAuditedModel;

  14. import com.querydsl.core.types.Predicate;
  15. import com.querydsl.core.types.dsl.BooleanExpression;
  16. import com.querydsl.core.types.dsl.EntityPathBase;
  17. import com.querydsl.core.types.dsl.ListPath;
  18. import com.querydsl.jpa.JPAExpressions;

  19. @Getter
  20. @Setter
  21. @EqualsAndHashCode(callSuper = true)
  22. @Accessors(fluent = true)
  23. public abstract class AuditedModelFilter<T extends AuditedModelFilter<T, R>, R extends AuditedModel> extends EmptyModelFilter<T, R> {

  24.     private static final long serialVersionUID = 3000864197634727372L;
  25.     /** The created by. */
  26.     public Set<Long> createdBy;
  27.     /** The created date. */
  28.     public TemporalFilter<Instant> createdDate;
  29.     /** The last modified by. */
  30.     public Set<Long> modifiedBy;
  31.     /** The last modified date. */
  32.     public TemporalFilter<Instant> modifiedDate;
  33.    
  34.     /**
  35.      * @deprecated Must use {@link #collectPredicates(EntityPathBase, QAuditedModel)}!
  36.      */
  37.     @Override
  38.     @Deprecated
  39.     protected List<Predicate> collectPredicates(EntityPathBase<R> instance) {
  40.         return super.collectPredicates(instance);
  41.     }

  42.     protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, final QAuditedModel auditedModel) {
  43.         List<Predicate> predicates = super.collectPredicates(instance);
  44.         if (CollectionUtils.isNotEmpty(createdBy)) {
  45.             predicates.add(auditedModel.createdBy.in(createdBy));
  46.         }
  47.         if (CollectionUtils.isNotEmpty(modifiedBy)) {
  48.             predicates.add(auditedModel.modifiedBy.in(modifiedBy));
  49.         }
  50.         if (createdDate != null) {
  51.             predicates.add(createdDate.buildQuery(auditedModel.createdDate));
  52.         }
  53.         if (modifiedDate != null) {
  54.             predicates.add(modifiedDate.buildQuery(auditedModel.modifiedDate));
  55.         }
  56.         return predicates;
  57.     }

  58.     /**
  59.      * Created date.
  60.      *
  61.      * @return the date filter
  62.      */
  63.     public synchronized TemporalFilter<Instant> createdDate() {
  64.         if (createdDate == null) {
  65.             createdDate = new TemporalFilter<Instant>();
  66.         }
  67.         return createdDate;
  68.     }

  69.     /**
  70.      * Last modified date.
  71.      *
  72.      * @return the date filter
  73.      */
  74.     public synchronized TemporalFilter<Instant> modifiedDate() {
  75.         if (modifiedDate == null) {
  76.             modifiedDate = new TemporalFilter<Instant>();
  77.         }
  78.         return modifiedDate;
  79.     }

  80.     /**
  81.      * Generate a single predicate that assures a nested collection is not empty,
  82.      * and uses a JPA sub-query to test for neste objects that match filter predicates
  83.      * in nestedPredicates.
  84.      *
  85.      * @param nestedCollection the ListPath to a nested collection of objects
  86.      * @param entityPath the alias that is used in joinExpression and to collect nested predicates
  87.      * @param joinExpression the join expression to the parent entity
  88.      * @param nestedPredicates predicates generated by the filter for the nested entity
  89.      *
  90.      * @return a single Predicate with: .isNotEmpty().and(subquery).
  91.      */
  92.     protected Predicate nestedFilter(ListPath<?, ?> nestedCollection, EntityPathBase<?> entityPath, BooleanExpression joinExpression, List<Predicate> nestedPredicates) {
  93.         nestedPredicates.add(0, joinExpression);
  94.         var sourceExpression = JPAExpressions.selectFrom(entityPath).where(nestedPredicates.toArray(Predicate[]::new)).exists();
  95.         return nestedCollection.isNotEmpty().and(sourceExpression);
  96.     }

  97. }