CooperatorOwnedModelFilter.java

  1. /*
  2.  * Copyright 2020 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.gringlobal.service.filter;

  17. import java.time.Instant;
  18. import java.util.List;
  19. import java.util.Set;

  20. import lombok.EqualsAndHashCode;
  21. import lombok.Getter;
  22. import lombok.Setter;
  23. import lombok.experimental.Accessors;
  24. import org.apache.commons.collections4.CollectionUtils;
  25. import org.genesys.blocks.model.filters.TemporalFilter;
  26. import org.gringlobal.model.CooperatorOwnedModel;
  27. import org.gringlobal.model.QCooperatorOwnedModel;

  28. import com.querydsl.core.types.Predicate;
  29. import com.querydsl.core.types.dsl.EntityPathBase;

  30. /**
  31.  * {@link CooperatorOwnedModel} match by sample filters.
  32.  *
  33.  * @param <T> the generic type
  34.  * @param <R> the generic type
  35.  */
  36. @Getter
  37. @Setter
  38. @EqualsAndHashCode(callSuper = true)
  39. @Accessors(fluent = true)
  40. public abstract class CooperatorOwnedModelFilter<T extends CooperatorOwnedModelFilter<T, R>, R extends CooperatorOwnedModel> extends AuditedModelFilter<T, R> {

  41.     private static final long serialVersionUID = -8096327516700573523L;

  42.     /** The owned by. */
  43.     public Set<Long> ownedBy;

  44.     /** The owned date. */
  45.     public TemporalFilter<Instant> ownedDate;

  46.     /**
  47.      * Collects list of filter predicates
  48.      *
  49.      * @param instance the instance of Q-type of <em>R</em>
  50.      * @param cooperatorOwnedModel the cooperator owned model
  51.      * @return list of predicates
  52.      */
  53.     protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, final QCooperatorOwnedModel cooperatorOwnedModel) {
  54.         List<Predicate> predicates = super.collectPredicates(instance, cooperatorOwnedModel._super);
  55.         if (CollectionUtils.isNotEmpty(ownedBy)) {
  56.             predicates.add(cooperatorOwnedModel.ownedBy().id.in(ownedBy));
  57.         }
  58.         if (ownedDate != null) {
  59.             predicates.add(ownedDate.buildQuery(cooperatorOwnedModel.ownedDate));
  60.         }
  61.         return predicates;
  62.     }

  63.     /**
  64.      * Owned date.
  65.      *
  66.      * @return the date filter
  67.      */
  68.     public synchronized TemporalFilter<Instant> ownedDate() {
  69.         if (ownedDate == null) {
  70.             ownedDate = new TemporalFilter<>();
  71.         }
  72.         return ownedDate;
  73.     }

  74.     public String convertToString(Boolean value) {
  75.         return value ? "Y" : "N";
  76.     }

  77. }