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 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
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 }