DiversityTreeFilter.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.genesys.server.service.filter;

  17. import java.util.Collections;
  18. import java.util.HashSet;
  19. import java.util.List;
  20. import java.util.Set;

  21. import org.genesys.blocks.model.filters.StringFilter;
  22. import org.genesys.blocks.model.filters.UuidModelFilter;
  23. import org.genesys.server.model.PublishState;
  24. import org.genesys.server.model.impl.DiversityTree;
  25. import org.genesys.server.model.impl.QDiversityTree;

  26. import com.querydsl.core.types.Predicate;

  27. import lombok.EqualsAndHashCode;
  28. import lombok.Getter;
  29. import lombok.Setter;
  30. import lombok.experimental.Accessors;

  31. /**
  32.  * Filters for {@link DiversityTree}.
  33.  *
  34.  * @author Maxym Borodenko
  35.  */
  36. @Getter
  37. @Setter
  38. @EqualsAndHashCode(callSuper = true)
  39. @Accessors(fluent = true)
  40. public class DiversityTreeFilter extends UuidModelFilter<DiversityTreeFilter, DiversityTree> implements IFullTextFilter {

  41.     private static final long serialVersionUID = 3948516024666269840L;

  42.     /** Any text. */
  43.     public String _text;

  44.     /** The title. */
  45.     public StringFilter title;

  46.     /** The description. */
  47.     public StringFilter description;

  48.     /** The date created. */
  49.     public StringFilter dateCreated;

  50.     /** The publish state. */
  51.     public Set<PublishState> state;

  52.     public Set<String> crop;

  53.     /** Is current version */
  54.     public Boolean current;

  55.     /**
  56.      * Builds the query.
  57.      *
  58.      * @return the predicate
  59.      */
  60.     @Override
  61.     public List<Predicate> collectPredicates() {
  62.         return collectPredicates(QDiversityTree.diversityTree);
  63.     }

  64.     /**
  65.      * Builds the query.
  66.      *
  67.      * @param diversityTree the diversityTree
  68.      * @return the predicate
  69.      */
  70.     public List<Predicate> collectPredicates(QDiversityTree diversityTree) {
  71.         final List<Predicate> predicates = super.collectPredicates(diversityTree, diversityTree._super);

  72.         if (title != null) {
  73.             predicates.add(title.buildQuery(diversityTree.title));
  74.         }
  75.         if (description != null) {
  76.             predicates.add(description.buildQuery(diversityTree.description));
  77.         }
  78.         if (dateCreated != null) {
  79.             predicates.add(dateCreated.buildQuery(diversityTree.dateCreated));
  80.         }
  81.         if (state != null && !state.isEmpty()) {
  82.             predicates.add(diversityTree.state.in(state));
  83.         }
  84.         if (crop != null && !crop.isEmpty()) {
  85.             predicates.add(diversityTree.crop.in(crop));
  86.         }
  87.         if (current != null) {
  88.             predicates.add(diversityTree.current.eq(current));
  89.         }

  90.         return predicates;
  91.     }

  92.     public synchronized DiversityTreeFilter state(PublishState... state) {
  93.         if (this.state == null) {
  94.             this.state = new HashSet<>();
  95.         }
  96.         Collections.addAll(this.state, state);
  97.         return this;
  98.     }

  99.     public synchronized DiversityTreeFilter crop(String... crop) {
  100.         if (this.crop == null) {
  101.             this.crop = new HashSet<>();
  102.         }
  103.         Collections.addAll(this.crop, crop);
  104.         return this;
  105.     }

  106.     public synchronized DiversityTreeFilter crop(Set<String> crop) {
  107.         this.crop = crop;
  108.         return this;
  109.     }

  110.     @Override
  111.     public String get_text() {
  112.         return _text;
  113.     }
  114. }