View Javadoc
1   package net.sumaris.server.http.graphql.referential;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Server
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 io.leangen.graphql.annotations.GraphQLArgument;
26  import io.leangen.graphql.annotations.GraphQLContext;
27  import io.leangen.graphql.annotations.GraphQLMutation;
28  import io.leangen.graphql.annotations.GraphQLQuery;
29  import net.sumaris.core.dao.referential.metier.MetierRepository;
30  import net.sumaris.core.dao.technical.SortDirection;
31  import net.sumaris.core.service.referential.ReferentialService;
32  import net.sumaris.core.service.referential.taxon.TaxonGroupService;
33  import net.sumaris.core.service.referential.taxon.TaxonNameService;
34  import net.sumaris.core.vo.filter.ReferentialFilterVO;
35  import net.sumaris.core.vo.filter.TaxonNameFilterVO;
36  import net.sumaris.core.vo.referential.ReferentialTypeVO;
37  import net.sumaris.core.vo.referential.ReferentialVO;
38  import net.sumaris.core.vo.referential.TaxonNameVO;
39  import net.sumaris.server.http.security.IsAdmin;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  import org.springframework.beans.factory.annotation.Autowired;
43  import org.springframework.stereotype.Service;
44  import org.springframework.transaction.annotation.Transactional;
45  
46  import java.util.List;
47  
48  @Service
49  @Transactional
50  public class ReferentialGraphQLService {
51  
52      private static final Logger log = LoggerFactory.getLogger(ReferentialGraphQLService.class);
53  
54      @Autowired
55      private ReferentialService referentialService;
56  
57      @Autowired
58      private TaxonNameService taxonNameService;
59  
60      @Autowired
61      private TaxonGroupService taxonGroupService;
62  
63      @Autowired
64      private MetierRepository metierRepository;
65  
66      /* -- Referential queries -- */
67  
68      @GraphQLQuery(name = "referentialTypes", description = "Get all types of referential")
69      @Transactional(readOnly = true)
70      public List<ReferentialTypeVO> getAllReferentialTypes() {
71          return referentialService.getAllTypes();
72      }
73  
74      @GraphQLQuery(name = "referentials", description = "Search in referentials")
75      @Transactional(readOnly = true)
76      public List<? extends ReferentialVO> findReferentialsByFilter(
77              @GraphQLArgument(name = "entityName") String entityName,
78              @GraphQLArgument(name = "filter") ReferentialFilterVO filter,
79              @GraphQLArgument(name = "offset", defaultValue = "0") Integer offset,
80              @GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
81              @GraphQLArgument(name = "sortBy", defaultValue = ReferentialVO.Fields.NAME) String sort,
82              @GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction) {
83  
84  
85          // TODO: not used in app: remove
86          //if ("TargetSpecies".equals(entityName)) {
87          //    return taxonGroupService.findTargetSpeciesByFilter(
88          //            filter != null ? filter : new ReferentialFilterVO(),
89          //            offset, size, sort,
90          //            SortDirection.valueOf(direction.toUpperCase()));
91          //}
92  
93          // Special case
94          if ("Metier".equals(entityName)) {
95              return metierRepository.findByFilter(
96                      filter != null ? filter : new ReferentialFilterVO(),
97                      offset, size, sort,
98                      SortDirection.valueOf(direction.toUpperCase()));
99          }
100 
101         return referentialService.findByFilter(entityName, filter, offset, size, sort, SortDirection.valueOf(direction.toUpperCase()));
102     }
103 
104     @GraphQLQuery(name = "referentialsCount", description = "Get referentials count")
105     @Transactional(readOnly = true)
106     public Long getReferentialsCount(@GraphQLArgument(name = "entityName") String entityName) {
107         return referentialService.count(entityName);
108     }
109 
110     @GraphQLQuery(name = "referentialLevels", description = "Get all levels from entityName")
111     @Transactional(readOnly = true)
112     public List<ReferentialVO> getAllReferentialLevels(
113             @GraphQLArgument(name = "entityName") String entityName) {
114         return referentialService.getAllLevels(entityName);
115     }
116 
117     @GraphQLQuery(name = "level", description = "Get the level from a referential entity")
118     @Transactional(readOnly = true)
119     public ReferentialVO getReferentialLevel(
120             @GraphQLContext ReferentialVO referential) {
121         return referentialService.getLevelById(referential.getEntityName(), referential.getLevelId());
122     }
123 
124     @GraphQLMutation(name = "saveReferential", description = "Create or update a referential")
125     @IsAdmin
126     public ReferentialVO saveReferential(
127             @GraphQLArgument(name = "referential") ReferentialVO referential) {
128         return referentialService.save(referential);
129     }
130 
131     @GraphQLMutation(name = "saveReferentials", description = "Create or update many referential")
132     @IsAdmin
133     public List<ReferentialVO> saveReferentials(
134             @GraphQLArgument(name = "referentials") List<ReferentialVO> referential) {
135         return referentialService.save(referential);
136     }
137 
138     @GraphQLMutation(name = "deleteReferential", description = "Delete a referential (by id)")
139     @IsAdmin
140     public void deleteReferential(
141             @GraphQLArgument(name = "entityName") String entityName,
142             @GraphQLArgument(name = "id") int id) {
143         referentialService.delete(entityName, id);
144     }
145 
146     @GraphQLMutation(name = "deleteReferentials", description = "Delete many referential (by ids)")
147     @IsAdmin
148     public void deleteReferentials(
149             @GraphQLArgument(name = "entityName") String entityName,
150             @GraphQLArgument(name = "ids") List<Integer> ids) {
151         referentialService.delete(entityName, ids);
152     }
153 
154     /* -- taxon -- */
155 
156     @GraphQLQuery(name = "taxonNames", description = "Search in taxon names")
157     @Transactional(readOnly = true)
158     public List<TaxonNameVO> findTaxonNames(
159             @GraphQLArgument(name = "filter") TaxonNameFilterVO filter,
160             @GraphQLArgument(name = "offset", defaultValue = "0") Integer offset,
161             @GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
162             @GraphQLArgument(name = "sortBy", defaultValue = ReferentialVO.Fields.NAME) String sort,
163             @GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction) {
164 
165         filter = filter != null ? filter : new TaxonNameFilterVO();
166         return taxonNameService.findByFilter(filter, offset, size, sort, SortDirection.valueOf(direction.toUpperCase()));
167     }
168 
169 }