VersionedModelFilter.java

  1. /*
  2.  * Copyright 2019 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.genesys.blocks.model.filters;

  17. import java.util.List;
  18. import java.util.Set;

  19. import org.apache.commons.collections4.CollectionUtils;
  20. import org.genesys.blocks.model.QVersionedModel;
  21. import org.genesys.blocks.model.VersionedModel;

  22. import com.querydsl.core.types.Predicate;
  23. import com.querydsl.core.types.dsl.EntityPathBase;

  24. import lombok.EqualsAndHashCode;
  25. import lombok.Getter;
  26. import lombok.Setter;
  27. import lombok.experimental.Accessors;

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

  39.     private static final long serialVersionUID = -4226770121644301621L;

  40.     /** The version. */
  41.     public Set<Integer> version;

  42.     /** The active. */
  43.     public Boolean active;

  44.     /**
  45.      * Collects list of filter predicates
  46.      *
  47.      * @param instance the instance of Q-type of <em>R</em>
  48.      * @param versionedModel the versioned model
  49.      * @return list of predicates
  50.      */
  51.     protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, QVersionedModel versionedModel) {
  52.         List<Predicate> predicates = super.collectPredicates(instance);
  53.         if (CollectionUtils.isNotEmpty(version)) {
  54.             predicates.add(versionedModel.version.in(version));
  55.         }
  56.         if (active != null) {
  57.             predicates.add(versionedModel.active.eq(active));
  58.         }
  59.         return predicates;
  60.     }

  61. }