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 com.google.common.base.Preconditions;
26  import io.leangen.graphql.annotations.GraphQLArgument;
27  import io.leangen.graphql.annotations.GraphQLQuery;
28  import net.sumaris.core.dao.technical.SortDirection;
29  import net.sumaris.core.extraction.service.ExtractionService;
30  import net.sumaris.core.extraction.vo.ExtractionFilterVO;
31  import net.sumaris.core.extraction.vo.ExtractionResultVO;
32  import net.sumaris.core.extraction.vo.filter.ExtractionTypeFilterVO;
33  import net.sumaris.core.extraction.vo.ExtractionTypeVO;
34  import net.sumaris.server.http.rest.DownloadController;
35  import net.sumaris.server.http.security.IsUser;
36  import org.springframework.beans.factory.annotation.Autowired;
37  import org.springframework.stereotype.Service;
38  import org.springframework.transaction.annotation.Transactional;
39  
40  import java.io.File;
41  import java.io.IOException;
42  import java.util.List;
43  
44  @Service
45  @Transactional
46  public class ExtractionGraphQLService {
47  
48      @Autowired
49      private ExtractionService extractionService;
50  
51      @Autowired
52      private DownloadController downloadController;
53  
54      /* -- Extraction / table -- */
55  
56      @GraphQLQuery(name = "extractionTypes", description = "Get all available extraction types")
57      @Transactional(readOnly = true)
58      public List<ExtractionTypeVO> getAllExtractionTypes(@GraphQLArgument(name = "filter") ExtractionTypeFilterVO filter) {
59          return extractionService.findByFilter(filter);
60      }
61  
62      @GraphQLQuery(name = "extractionRows", description = "Preview some extraction rows")
63      @Transactional
64      @IsUser
65      public ExtractionResultVO getExtractionRows(@GraphQLArgument(name = "type") ExtractionTypeVO type,
66                                                 @GraphQLArgument(name = "filter") ExtractionFilterVO filter,
67                                                 @GraphQLArgument(name = "offset", defaultValue = "0") Integer offset,
68                                                 @GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
69                                                 @GraphQLArgument(name = "sortBy") String sort,
70                                                 @GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction
71      ) {
72          Preconditions.checkNotNull(type, "Argument 'type' must not be null.");
73          Preconditions.checkNotNull(type.getLabel(), "Argument 'type.label' must not be null.");
74          Preconditions.checkNotNull(offset, "Argument 'offset' must not be null.");
75          Preconditions.checkNotNull(size, "Argument 'size' must not be null.");
76  
77          return extractionService.executeAndRead(type, filter, offset, size, sort, direction != null ? SortDirection.valueOf(direction.toUpperCase()) : null);
78      }
79  
80      @GraphQLQuery(name = "extractionFile", description = "Execute extraction to a file")
81      @Transactional
82      @IsUser
83      public String getExtractionFile(@GraphQLArgument(name = "type") ExtractionTypeVO type,
84                                   @GraphQLArgument(name = "filter") ExtractionFilterVO filter
85      ) throws IOException {
86          Preconditions.checkNotNull(type, "Argument 'type' must not be null.");
87          Preconditions.checkNotNull(type.getLabel(), "Argument 'type.label' must not be null.");
88  
89          File tempFile = extractionService.executeAndDump(type, filter);
90          String fileServerPath = downloadController.registerFile(tempFile, true);
91  
92         return fileServerPath;
93      }
94  
95  }