View Javadoc
1   package net.sumaris.core.dao.referential;
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 net.sumaris.core.dao.cache.CacheNames;
26  import net.sumaris.core.dao.technical.SortDirection;
27  import net.sumaris.core.model.referential.IReferentialEntity;
28  import net.sumaris.core.vo.filter.ReferentialFilterVO;
29  import net.sumaris.core.vo.referential.IReferentialVO;
30  import net.sumaris.core.vo.referential.ReferentialTypeVO;
31  import net.sumaris.core.vo.referential.ReferentialVO;
32  import org.springframework.cache.annotation.CacheEvict;
33  import org.springframework.cache.annotation.Cacheable;
34  import org.springframework.cache.annotation.Caching;
35  
36  import javax.persistence.TypedQuery;
37  import javax.persistence.criteria.CriteriaQuery;
38  import javax.persistence.criteria.Expression;
39  import javax.persistence.criteria.Root;
40  import java.util.List;
41  import java.util.Optional;
42  
43  public interface ReferentialDao {
44  
45      interface QueryVisitor<T> {
46          Expression<Boolean> apply(CriteriaQuery<T> query, Root<T> root);
47      }
48  
49      @Cacheable(cacheNames = CacheNames.REFERENTIAL_TYPES)
50      List<ReferentialTypeVO> getAllTypes();
51  
52      List<ReferentialVO> getAllLevels(String entityName);
53  
54      ReferentialVO getLevelById(String entityName, int levelId);
55  
56      List<ReferentialVO> findByFilter(String entityName,
57                                       ReferentialFilterVO filter,
58                                       int offset,
59                                       int size,
60                                       String sortAttribute,
61                                       SortDirection sortDirection);
62  
63      @Cacheable(cacheNames = CacheNames.REFERENTIAL_LEVEL_BY_UNIQUE_LABEL, key = "#entityName+':'+#label", condition = "#entityName == 'LocationLevel'")
64      ReferentialVO findByUniqueLabel(String entityName, String label);
65  
66      <T extends IReferentialEntity> ReferentialVO toReferentialVO(T source);
67  
68      <T extends IReferentialVO, S extends IReferentialEntity> Optional<T> toTypedVO(S source, Class<T> targetClazz);
69  
70  
71      @Caching(
72              evict = {
73                      @CacheEvict(cacheNames = CacheNames.PERSON_BY_ID, key = "#source.id", condition = "#source.entityName == 'Person'"),
74                      @CacheEvict(cacheNames = CacheNames.DEPARTMENT_BY_ID, key = "#source.id", condition = "#source.entityName == 'Department'"),
75                      @CacheEvict(cacheNames = CacheNames.PMFM_BY_ID, key = "#source.id", condition = "#source.entityName == 'Pmfm'"),
76                      @CacheEvict(cacheNames = CacheNames.PROGRAM_BY_LABEL, key = "#source.id", condition = "#source.entityName == 'Program'"),
77                      @CacheEvict(cacheNames = CacheNames.REFERENTIAL_LEVEL_BY_UNIQUE_LABEL, key = "#source.label", condition = "#source.entityName == 'LocationLevel'"),
78                      @CacheEvict(cacheNames = CacheNames.PROGRAM_BY_LABEL, key = "#source.label", condition = "#source.entityName == 'Program'"),
79                      @CacheEvict(cacheNames = CacheNames.PROGRAM_BY_ID, key = "#source.id", condition = "#source.entityName == 'Program'")
80              }
81      )
82      ReferentialVO/../../../../net/sumaris/core/vo/referential/ReferentialVO.html#ReferentialVO">ReferentialVO save(ReferentialVO source);
83  
84      @Caching(evict= {
85              @CacheEvict(cacheNames = CacheNames.PERSON_BY_ID, key = "#id", condition = "#entityName == 'Person'"),
86              @CacheEvict(cacheNames = CacheNames.DEPARTMENT_BY_ID, key = "#id", condition = "#entityName == 'Department'"),
87              @CacheEvict(cacheNames = CacheNames.PMFM_BY_ID, key = "#id", condition = "#entityName == 'Pmfm'"),
88              @CacheEvict(cacheNames = CacheNames.PROGRAM_BY_LABEL, key = "#id", condition = "#entityName == 'Program'"),
89              @CacheEvict(cacheNames = CacheNames.REFERENTIAL_LEVEL_BY_UNIQUE_LABEL, allEntries = true, condition = "#entityName == 'LocationLevel'"),
90              @CacheEvict(cacheNames = CacheNames.PROGRAM_BY_LABEL, allEntries = true, condition = "#entityName == 'Program'"),
91              @CacheEvict(cacheNames = CacheNames.PROGRAM_BY_ID, key = "#id", condition = "#entityName == 'Program'")
92      })
93      void delete(String entityName, int id);
94  
95      Long count(String entityName);
96  
97      Long countByLevelId(String entityName, Integer... levelIds);
98  
99      <T> TypedQuery<T> createFindQuery(Class<T> entityClass,
100                                       Integer levelId,
101                                       Integer[] levelIds,
102                                       String searchText,
103                                       String searchAttribute,
104                                       Integer[] statusIds,
105                                       String sortAttribute,
106                                       SortDirection sortDirection,
107                                       QueryVisitor<T> queryVisitor);
108 }