View Javadoc
1   package net.sumaris.core.dao.referential;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.ImmutableList;
26  import net.sumaris.core.dao.technical.jpa.SpecificationWithParameters;
27  import net.sumaris.core.model.referential.IItemReferentialEntity;
28  import net.sumaris.core.model.referential.IReferentialEntity;
29  import net.sumaris.core.model.referential.Status;
30  import net.sumaris.core.model.referential.gear.Gear;
31  import net.sumaris.core.model.referential.metier.Metier;
32  import org.apache.commons.lang3.ArrayUtils;
33  import org.apache.commons.lang3.StringUtils;
34  import org.springframework.data.jpa.domain.Specification;
35  
36  import javax.persistence.criteria.*;
37  
38  public class ReferentialSpecifications {
39  
40      public static Specification<Metier> inLevelIds(String levelProperty, Integer[] gearIds) {
41          if (ArrayUtils.isEmpty(gearIds)) return null;
42          return (root, query, cb) -> cb.in(
43                  root.join(levelProperty, JoinType.INNER).get(Gear.Fields.ID))
44                  .value(ImmutableList.copyOf(gearIds));
45      }
46  
47      public static <T extends IReferentialEntity> Specification<T> inStatusIds(Integer[] statusIds) {
48          if (ArrayUtils.isEmpty(statusIds)) return null;
49          return (root, query, cb) -> cb.in(
50                  root.get(IReferentialEntity.Fields.STATUS).get(Status.Fields.ID))
51                  .value(ImmutableList.copyOf(statusIds));
52      }
53  
54      public static <T extends IItemReferentialEntity> Specification<T> searchText(String searchAttribute, String paramName) {
55  
56          return new SpecificationWithParameters<T>() {
57  
58              public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
59                                           CriteriaBuilder cb) {
60                  ParameterExpression<String> searchTextParam = add(cb.parameter(String.class, paramName));
61  
62                  if (StringUtils.isNotBlank(searchAttribute)) {
63                      return cb.or(
64                              cb.isNull(searchTextParam),
65                              cb.like(cb.upper(root.get(searchAttribute)), cb.upper(searchTextParam)));
66                  }
67                  // Search on label+name
68                  return cb.or(
69                          cb.isNull(searchTextParam),
70                          cb.like(cb.upper(root.get(IItemReferentialEntity.Fields.LABEL)), cb.upper(searchTextParam)),
71                          cb.like(cb.upper(root.get(IItemReferentialEntity.Fields.NAME)), cb.upper(cb.concat("%", searchTextParam)))
72                  );
73              }
74          };
75      }
76  
77      public static <T extends IItemReferentialEntity> Specification<T> joinSearchText(String joinProperty, String searchAttribute, String paramName) {
78  
79          return new SpecificationWithParameters<T>() {
80  
81              public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
82                                           CriteriaBuilder cb) {
83                  ParameterExpression<String> searchTextParam = add(cb.parameter(String.class, paramName));
84  
85                  if (StringUtils.isNotBlank(searchAttribute)) {
86                      return cb.or(
87                              cb.isNull(searchTextParam),
88                              cb.like(cb.upper(root.join(joinProperty).get(searchAttribute)), cb.upper(searchTextParam)));
89                  }
90                  // Search on label+name
91                  return cb.or(
92                          cb.isNull(searchTextParam),
93                          cb.like(cb.upper(root.join(joinProperty)
94                                  .get(IItemReferentialEntity.Fields.LABEL)), cb.upper(searchTextParam)),
95                          cb.like(cb.upper(root.join(joinProperty)
96                                  .get(IItemReferentialEntity.Fields.NAME)), cb.upper(cb.concat("%", searchTextParam)))
97                  );
98              }
99          };
100     }
101 }