1 package net.sumaris.server.http.graphql.referential;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
86
87
88
89
90
91
92
93
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
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 }