Project

General

Profile

1
package eu.dnetlib.repo.manager.controllers;
2

    
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.JobForValidation;
5
import eu.dnetlib.domain.functionality.validator.RuleSet;
6
import eu.dnetlib.domain.functionality.validator.StoredJob;
7
import eu.dnetlib.repo.manager.domain.InterfaceInformation;
8
import eu.dnetlib.repo.manager.domain.ValidationServiceException;
9
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
10
import eu.dnetlib.repo.manager.service.EmailUtils;
11
import eu.dnetlib.repo.manager.service.ValidatorServiceImpl;
12
import io.swagger.annotations.Api;
13
import io.swagger.annotations.ApiParam;
14
import org.json.JSONException;
15
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.http.MediaType;
18
import org.springframework.http.ResponseEntity;
19
import org.springframework.security.access.prepost.PreAuthorize;
20
import org.springframework.security.core.context.SecurityContextHolder;
21
import org.springframework.web.bind.annotation.*;
22

    
23
import java.util.List;
24

    
25

    
26
@RestController
27
@RequestMapping(value = "/validator")
28
@Api(description = "Validator API",  tags = {"validator"})
29
public class ValidatorController {
30

    
31
    @Autowired
32
    private ValidatorServiceImpl validatorService;
33

    
34
    @Autowired
35
    private EmailUtils emailUtils;
36

    
37
    @RequestMapping(value = "/submitJobForValidation",method = RequestMethod.POST,
38
            consumes = MediaType.APPLICATION_JSON_VALUE,
39
            produces = MediaType.APPLICATION_JSON_VALUE)
40
    @ResponseBody
41
    @PreAuthorize("hasRole('ROLE_USER') and #jobForValidation.userEmail == authentication.userInfo.email")
42
    public JobForValidation submitJobForValidation(@RequestBody JobForValidation jobForValidation) throws ValidatorServiceException {
43
        return validatorService.submitJobForValidation(jobForValidation);
44
    }
45

    
46
    @RequestMapping(value = "/reSubmitJobForValidation/{jobId}",method = RequestMethod.POST,
47
            consumes = MediaType.APPLICATION_JSON_VALUE,
48
            produces = MediaType.APPLICATION_JSON_VALUE)
49
    @ResponseBody
50
    @PreAuthorize("hasRole('ROLE_USER')")
51
    public ResponseEntity<Object> reSubmitJobForValidation(@PathVariable("jobId") String jobId) throws JSONException, ValidatorServiceException {
52
        return validatorService.reSubmitJobForValidation(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), jobId);
53
    }
54

    
55
    @RequestMapping(value = "/getRuleSets/{mode}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
56
    @ResponseBody
57
    public List<RuleSet> getRuleSets(@PathVariable("mode") String mode) {
58
        return validatorService.getRuleSets(mode);
59
    }
60

    
61
    @RequestMapping(value = "/getSetsOfRepository" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
62
    @ResponseBody
63
    public List<String> getSetsOfRepository(@RequestParam(value = "url", required = true) String url) {
64
        return validatorService.getSetsOfRepository(url);
65
    }
66

    
67
    @RequestMapping(value = "/identifyRepository" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
68
    @ResponseBody
69
    public boolean identifyRepo(@RequestParam(value = "url", required = true) String url) {
70
        return validatorService.identifyRepo(url);
71
    }
72

    
73
    @RequestMapping(value = "/getRuleSet/{acronym}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
74
    @ResponseBody
75
    public RuleSet getRuleSet(@PathVariable("acronym") String acronym) {
76
        return validatorService.getRuleSet(acronym);
77
    }
78

    
79
    @RequestMapping(value = "/getStoredJobsNew" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
80
    @ResponseBody
81
    @PreAuthorize("hasRole('ROLE_USER')")
82
    public List<StoredJob> getStoredJobsNew(@RequestParam(value = "jobType", required = false)
83
                                            @ApiParam(value = "Equals to filter job type on validation history page") String jobType,
84
                                            @RequestParam("offset") @ApiParam(value = "Page number", required = true) String offset,
85
                                            @RequestParam(value = "limit", required = false,defaultValue = "10") @ApiParam(value = "Null value") String limit,
86
                                            @RequestParam(value = "dateFrom", required = false) @ApiParam(value = "Null value") String dateFrom,
87
                                            @RequestParam(value = "dateTo", required = false) @ApiParam(value = "Null value") String dateTo,
88
                                            @RequestParam("validationStatus") @ApiParam(value = "Equals to filter validation jobs", required = true) String validationStatus
89
                                            ) throws ValidatorServiceException {
90
        return validatorService.getStoredJobsNew(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), jobType, offset, limit, dateFrom, dateTo, validationStatus);
91
    }
92

    
93
    @RequestMapping(value = "/getStoredJobsTotalNumberNew" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
94
    @ResponseBody
95
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
96
        return validatorService.getStoredJobsTotalNumberNew(user, jobType, validationStatus);
97
    }
98

    
99
    @RequestMapping(value = "/getInterfaceInformation" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
100
    @ResponseBody
101
    public InterfaceInformation getInterfaceInformation(@RequestParam(value = "baseUrl") String baseUrl) throws ValidationServiceException {
102
        return validatorService.getInterfaceInformation(baseUrl);
103
    }
104

    
105
    @RequestMapping(value = "/validationSummary/{repoId}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
106
    @ResponseBody
107
    public List<StoredJob> getInterfaceInformation(@PathVariable(value = "repoId") String repoId, @RequestParam(name = "size", defaultValue = "20") int size ) throws ValidatorServiceException, ResourceNotFoundException, JSONException {
108
        return validatorService.getJobsSummary(repoId,size);
109
    }
110

    
111

    
112
    @RequestMapping(value = "/complete" , method = RequestMethod.POST,  produces = MediaType.APPLICATION_JSON_VALUE)
113
    @ResponseBody
114
    public void validationCompleted(
115
        @RequestParam(value = "interfaceId") String interfaceId,
116
        @RequestParam(value = "repoId") String repoId,
117
        @RequestParam(value = "jobId") String jobId,
118
        @RequestParam(value = "issuerEmail") String issuerEmail,
119
        @RequestParam(value = "isUpdate") boolean isUpdate,
120
        @RequestParam(value = "isSuccess") boolean isSuccess,
121
        @RequestParam(value = "scoreUsage") int scoreUsage,
122
        @RequestParam(value = "scoreContent") int scoreContent) throws Exception {
123

    
124
        emailUtils.sendUponJobCompletion(repoId,interfaceId,scoreUsage,scoreContent,isSuccess,isUpdate,issuerEmail, jobId);
125
    }
126

    
127

    
128
}
(12-12/12)