View Javadoc
1   package net.sumaris.core.dao.referential.metier;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 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.base.Preconditions;
26  import net.sumaris.core.dao.referential.ReferentialDao;
27  import net.sumaris.core.dao.referential.taxon.TaxonGroupRepository;
28  import net.sumaris.core.dao.technical.SortDirection;
29  import net.sumaris.core.dao.technical.jpa.SumarisJpaRepositoryImpl;
30  import net.sumaris.core.model.referential.IItemReferentialEntity;
31  import net.sumaris.core.model.referential.metier.Metier;
32  import net.sumaris.core.util.Beans;
33  import net.sumaris.core.vo.filter.ReferentialFilterVO;
34  import net.sumaris.core.vo.referential.MetierVO;
35  import net.sumaris.core.vo.referential.ReferentialVO;
36  import org.apache.commons.lang3.StringUtils;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  import org.springframework.beans.factory.annotation.Autowired;
40  import org.springframework.data.domain.Pageable;
41  import org.springframework.data.jpa.domain.Specification;
42  
43  import javax.annotation.Nullable;
44  import javax.persistence.EntityManager;
45  import javax.persistence.Parameter;
46  import javax.persistence.TypedQuery;
47  import java.util.List;
48  import java.util.stream.Collectors;
49  
50  import static net.sumaris.core.dao.referential.metier.MetierSpecifications.*;
51  
52  public class MetierRepositoryImpl
53          extends SumarisJpaRepositoryImpl<Metier, Integer>
54          implements MetierRepositoryExtend {
55  
56  
57      private static final Logger log =
58              LoggerFactory.getLogger(MetierRepositoryImpl.class);
59  
60      @Autowired
61      private ReferentialDao referentialDao;
62  
63      @Autowired
64      private TaxonGroupRepository taxonGroupRepository;
65  
66      public MetierRepositoryImpl(EntityManager entityManager) {
67          super(Metier.class, entityManager);
68      }
69  
70      @Override
71      public List<ReferentialVO> findByFilter(
72                                                 ReferentialFilterVO filter,
73                                                 int offset,
74                                                 int size,
75                                                 String sortAttribute,
76                                                 SortDirection sortDirection) {
77  
78          Preconditions.checkNotNull(filter);
79  
80          // Prepare query parameters
81          Integer[] levelIds = (filter.getLevelId() != null) ? new Integer[]{filter.getLevelId()} :
82                  filter.getLevelIds();
83          String searchJoinProperty = filter.getSearchJoin() != null ? StringUtils.uncapitalize(filter.getSearchJoin()) : null;
84          String searchText = StringUtils.isBlank(filter.getSearchText()) ? null : (filter.getSearchText() + "*") // add trailing wildcard
85                  .replaceAll("[*]+", "*") // group escape chars
86                  .replaceAll("[%]", "\\%") // protected '%' chars
87                  .replaceAll("[*]", "%"); // replace asterix
88  
89          // With join property
90          Specification<Metier> searchTextSpecification;
91          if (searchJoinProperty != null) {
92              searchTextSpecification = joinSearchText(
93                      searchJoinProperty,
94                      filter.getSearchAttribute(), "searchText");
95          }
96          else {
97              searchTextSpecification = searchText(filter.getSearchAttribute(), "searchText");
98          }
99  
100         Specification<Metier> specification = Specification.where(searchTextSpecification)
101                 .and(inStatusIds(filter.getStatusIds()))
102                 .and(inGearIds(levelIds));
103 
104 
105         Pageable page = getPageable(offset, size, sortAttribute, sortDirection);
106         TypedQuery<Metier> query = getQuery(specification, Metier.class, page);
107 
108         Parameter<String> searchTextParam = query.getParameter("searchText", String.class);
109         if (searchTextParam != null) {
110             query.setParameter(searchTextParam, searchText);
111         }
112 
113         return query
114                 .setFirstResult(offset)
115                 .setMaxResults(size)
116                 .getResultStream()
117                     .distinct()
118                     .map(source -> {
119                         MetierVO target = this.toMetierVO(source);
120 
121                         // Copy join search to label/name
122                         if (searchJoinProperty != null) {
123                             Object joinSource = Beans.getProperty(source, searchJoinProperty);
124                             if (joinSource != null && joinSource instanceof IItemReferentialEntity) {
125                                 target.setLabel(Beans.getProperty(joinSource, IItemReferentialEntity.Fields.LABEL));
126                                 target.setName(Beans.getProperty(joinSource, IItemReferentialEntity.Fields.NAME));
127                             }
128                         }
129                         return target;
130                     })
131                     .collect(Collectors.toList());
132     }
133 
134 
135     @Override
136     public MetierVO toMetierVO(@Nullable Metier source) {
137         if (source == null) return null;
138 
139         MetierVOrential/MetierVO.html#MetierVO">MetierVO target = new MetierVO();
140 
141         Beans.copyProperties(source, target);
142 
143         // StatusId
144         target.setStatusId(source.getStatus().getId());
145 
146         // Gear
147         if (source.getGear() != null) {
148             target.setGear(referentialDao.toReferentialVO(source.getGear()));
149             target.setLevelId(source.getGear().getId());
150         }
151 
152         // Taxon group
153         if (source.getTaxonGroup() != null) {
154             target.setTaxonGroup(taxonGroupRepository.toTaxonGroupVO(source.getTaxonGroup()));
155         }
156 
157         return target;
158     }
159 
160     @Override
161     public MetierVO getById(int id) {
162         return toMetierVO(getOne(id));
163     }
164 }