View Javadoc
1   package net.sumaris.server.http.graphql.extraction;
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.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      /* -- aggregation service -- */
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      //@IsUser
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      // TODO: enable auth ?
97      //@IsUser
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         // Fill default values for strata
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 //    @GraphQLQuery(name = "aggregation", description = "Execute and read an aggregation")
137 //    @IsSupervisor
138 //    public AggregationResultVO getAggregation(@GraphQLArgument(name = "type") AggregationTypeVO type,
139 //                                              @GraphQLArgument(name = "filter") ExtractionFilterVO filter,
140 //                                              @GraphQLArgument(name = "strata") AggregationStrataVO strata,
141 //                                              @GraphQLArgument(name = "offset", defaultValue = "0") Integer offset,
142 //                                              @GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
143 //                                              @GraphQLArgument(name = "sortBy") String sort,
144 //                                              @GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction) {
145 //
146 //        return aggregationService.executeAndRead(type, filter, strata, offset, size, sort, SortDirection.fromString(direction));
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                 // Tables (=sheets)
154                 .withTables(fields.contains(AggregationTypeVO.PROPERTY_SHEET_NAMES))
155                 // Columns not need
156                 .withColumns(false)
157                 // Stratum
158                 .withStratum(fields.contains(AggregationTypeVO.PROPERTY_STRATUM))
159 
160                 .build();
161     }
162 
163 }