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/{email}/{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("email") String email,
52
                                                           @PathVariable("jobId") String jobId) throws JSONException, ValidatorServiceException {
53
        email = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail();
54
        return validatorService.reSubmitJobForValidation(email, jobId);
55
    }
56

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

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

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

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

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

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

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

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

    
115

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

    
128
        emailUtils.sendUponJobCompletion(repoId,interfaceId,scoreUsage,scoreContent,isSuccess,isUpdate,issuerEmail, jobId);
129
    }
130

    
131

    
132
}
(11-11/11)