1 package net.sumaris.server.http.graphql.extraction;
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.GraphQLEnvironment;
27 import io.leangen.graphql.annotations.GraphQLMutation;
28 import io.leangen.graphql.annotations.GraphQLQuery;
29 import net.sumaris.core.dao.technical.SortDirection;
30 import net.sumaris.core.dao.technical.model.IEntity;
31 import net.sumaris.core.extraction.dao.trip.rdb.AggregationRdbTripDao;
32 import net.sumaris.core.extraction.service.AggregationService;
33 import net.sumaris.core.extraction.vo.AggregationResultVO;
34 import net.sumaris.core.extraction.vo.AggregationStrataVO;
35 import net.sumaris.core.extraction.vo.AggregationTypeVO;
36 import net.sumaris.core.extraction.vo.ExtractionFilterVO;
37 import net.sumaris.core.extraction.vo.filter.AggregationTypeFilterVO;
38 import net.sumaris.core.model.data.IWithRecorderDepartmentEntity;
39 import net.sumaris.core.model.data.IWithRecorderPersonEntity;
40 import net.sumaris.core.vo.technical.extraction.ExtractionProductColumnVO;
41 import net.sumaris.core.vo.technical.extraction.ProductFetchOptions;
42 import net.sumaris.server.http.geojson.extraction.GeoJsonExtractions;
43 import net.sumaris.server.http.security.IsSupervisor;
44 import net.sumaris.server.http.security.IsUser;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47
48 import java.util.Arrays;
49 import java.util.List;
50 import java.util.Set;
51
52 @Component
53 public class AggregationGraphQLService {
54
55 @Autowired
56 private AggregationService aggregationService;
57
58
59
60
61 @GraphQLQuery(name = "aggregationType", description = "Get one aggregation type")
62 public AggregationTypeVO getAllAggregationTypes(@GraphQLArgument(name = "id") int id,
63 @GraphQLEnvironment() Set<String> fields) {
64 return aggregationService.get(id, getFetchOptions(fields));
65 }
66
67 @GraphQLQuery(name = "aggregationTypes", description = "Get all available aggregation types")
68 public List<AggregationTypeVO> getAllAggregationTypes(@GraphQLArgument(name = "filter") AggregationTypeFilterVO filter,
69 @GraphQLEnvironment() Set<String> fields) {
70 return aggregationService.findByFilter(filter, getFetchOptions(fields));
71 }
72
73 @GraphQLQuery(name = "aggregationRows", description = "Read an aggregation")
74 @IsUser
75 public AggregationResultVO getAggregationRows(@GraphQLArgument(name = "type") AggregationTypeVO type,
76 @GraphQLArgument(name = "filter") ExtractionFilterVO filter,
77 @GraphQLArgument(name = "strata") AggregationStrataVO strata,
78 @GraphQLArgument(name = "offset", defaultValue = "0") Integer offset,
79 @GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
80 @GraphQLArgument(name = "sortBy") String sort,
81 @GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction) {
82
83 return aggregationService.read(type, filter, strata, offset, size, sort, SortDirection.fromString(direction));
84 }
85
86 @GraphQLQuery(name = "aggregationColumns", description = "Read columns from aggregation")
87
88 public List<ExtractionProductColumnVO> getAggregationColumns(@GraphQLArgument(name = "type") AggregationTypeVO type,
89 @GraphQLArgument(name = "sheet") String sheet) {
90
91 return aggregationService.getColumnsBySheetName(type, sheet);
92 }
93
94
95 @GraphQLQuery(name = "aggregationGeoJson", description = "Execute an aggregation and return as GeoJson")
96
97
98 public Object getGeoJsonAggregation(@GraphQLArgument(name = "type") AggregationTypeVO type,
99 @GraphQLArgument(name = "filter") ExtractionFilterVO filter,
100 @GraphQLArgument(name = "strata") AggregationStrataVO strata,
101 @GraphQLArgument(name = "offset", defaultValue = "0") Integer offset,
102 @GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
103 @GraphQLArgument(name = "sortBy") String sort,
104 @GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction) {
105 strata = (strata == null) ? new AggregationStrataVO() : strata;
106 filter = filter == null ? new ExtractionFilterVO() : filter;
107
108
109 if (strata.getSpaceColumnName() == null) {
110 strata.setSpaceColumnName(AggregationRdbTripDao.COLUMN_SQUARE);
111 }
112 if (strata.getTimeColumnName() == null){
113 strata.setTimeColumnName(AggregationRdbTripDao.COLUMN_YEAR);
114 }
115
116 return GeoJsonExtractions.toFeatureCollection(
117 getAggregationRows(type, filter, strata, offset, size, sort, direction),
118 strata.getSpaceColumnName()
119 );
120 }
121
122 @GraphQLMutation(name = "saveAggregation", description = "Create or update a data aggregation")
123 @IsSupervisor
124 public AggregationTypeVO saveAggregation(@GraphQLArgument(name = "type") AggregationTypeVO type,
125 @GraphQLArgument(name = "filter") ExtractionFilterVO filter
126 ) {
127 return aggregationService.save(type, filter);
128 }
129
130 @GraphQLMutation(name = "deleteAggregations", description = "Delete some aggregations")
131 @IsSupervisor
132 public void deleteAggregations(@GraphQLArgument(name = "ids") int[] id) {
133 Arrays.stream(id).forEach(aggregationService::delete);
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 protected ProductFetchOptions getFetchOptions(Set<String> fields) {
150 return ProductFetchOptions.builder()
151 .withRecorderDepartment(fields.contains(IWithRecorderDepartmentEntity.Fields.RECORDER_DEPARTMENT + "/" + IEntity.Fields.ID))
152 .withRecorderPerson(fields.contains(IWithRecorderPersonEntity.Fields.RECORDER_PERSON + "/" + IEntity.Fields.ID))
153
154 .withTables(fields.contains(AggregationTypeVO.PROPERTY_SHEET_NAMES))
155
156 .withColumns(false)
157
158 .withStratum(fields.contains(AggregationTypeVO.PROPERTY_STRATUM))
159
160 .build();
161 }
162
163 }