Project

General

Profile

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

    
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.StoredJob;
5
import eu.dnetlib.repo.manager.service.utils.OaiTools;
6
import eu.dnetlib.repo.manager.shared.InterfaceInformation;
7
import eu.dnetlib.repo.manager.shared.ValidationServiceException;
8
import gr.uoa.di.driver.util.ServiceLocator;
9
import eu.dnetlib.domain.functionality.validator.JobForValidation;
10
import eu.dnetlib.domain.functionality.validator.RuleSet;
11
import eu.dnetlib.repo.manager.shared.Constants;
12

    
13
import java.util.*;
14
import java.util.concurrent.ConcurrentHashMap;
15
import eu.dnetlib.api.functionality.ValidatorService;
16
import io.swagger.annotations.ApiParam;
17
import org.apache.log4j.Logger;
18
import org.json.JSONException;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.security.access.prepost.PreAuthorize;
21
import org.springframework.stereotype.Component;
22
import org.springframework.web.bind.annotation.PathVariable;
23
import org.springframework.web.bind.annotation.RequestBody;
24
import org.springframework.web.bind.annotation.RequestParam;
25

    
26
import javax.annotation.PostConstruct;
27
import javax.annotation.Resource;
28

    
29

    
30
@Component
31
public class ValidatorApiImpl implements ValidatorApi{
32

    
33
    @Autowired
34
    private MonitorApiImpl monitorApi;
35

    
36
    @Resource(name = "validatorServiceLocator")
37
    private ServiceLocator<ValidatorService> validatorServiceLocator;
38

    
39
    private ValidatorService getValidationService() {
40
        return this.validatorServiceLocator.getService();
41
    }
42

    
43
    public ServiceLocator<ValidatorService> getValidatorServiceLocator() {
44
        return validatorServiceLocator;
45
    }
46

    
47
    public void setValidatorServiceLocator(ServiceLocator<ValidatorService> validatorServiceLocator) {
48
        this.validatorServiceLocator = validatorServiceLocator;
49
    }
50

    
51
    private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
52

    
53
    private static final Logger LOGGER = Logger
54
            .getLogger(ValidatorApiImpl.class);
55

    
56
    @PostConstruct
57
    private void loadRules(){
58
        LOGGER.debug("PostConstruct method! Load rules!");
59
        try {
60
            for (RuleSet ruleSet : getValidationService().getRuleSets()) {
61
                if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
62
                    String key = "";
63
                    if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
64
                        key = Constants.VALIDATION_MODE_DATA;
65
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
66
                        key = Constants.VALIDATION_MODE_LITERATURE;
67
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
68
                        key = Constants.VALIDATION_MODE_CRIS;
69

    
70
                    if (rulesetMap.containsKey(key))
71
                        rulesetMap.get(key).add(ruleSet);
72
                    else {
73
                        List<RuleSet> ruleSets = new ArrayList<RuleSet>();
74
                        ruleSets.add(ruleSet);
75
                        rulesetMap.put(key, ruleSets);
76
                    }
77
                }
78
            }
79
        } catch (ValidatorServiceException e) {
80
            e.printStackTrace();
81
        }
82

    
83
    }
84

    
85
    @Override
86
    @PreAuthorize("hasRole('ROLE_USER')")
87
    public void submitJobForValidation(@RequestBody JobForValidation jobForValidation) {
88
        LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId());
89
        try {
90
            this.getValidationService().submitValidationJob(jobForValidation);
91
        } catch (ValidatorServiceException e) {
92
            e.printStackTrace();
93
        }
94
    }
95

    
96
    @Override
97
    @PreAuthorize("hasRole('ROLE_USER')")
98
    public void reSubmitJobForValidation(@PathVariable("jobId") String jobId) throws JSONException {
99
        LOGGER.debug("Resubmit validation job with id : " + jobId);
100
        StoredJob job = monitorApi.getJobSummary(jobId,"all");
101
        Set<Integer> contentRules = new HashSet<Integer>();
102
        Set<Integer> usageRules = new HashSet<Integer>();
103

    
104
        RuleSet ruleSet = null;
105
        for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
106
            for (RuleSet rSet : ruleSets)
107
                if (rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())) {
108
                    ruleSet = rSet;
109
                    break;
110
                }
111
        }
112

    
113
        for (int ruleId : job.getRules()) {
114
            if (ruleSet.getContentRulesIds().contains(ruleId))
115
                contentRules.add(ruleId);
116
            else if (ruleSet.getUsageRulesIds().contains(ruleId))
117
                usageRules.add(ruleId);
118
        }
119
        if (!contentRules.isEmpty())
120
            job.setSelectedContentRules(contentRules);
121
        if (!usageRules.isEmpty())
122
            job.setSelectedUsageRules(usageRules);
123
        this.submitJobForValidation(job);
124
    }
125

    
126
    @Override
127
    public List<RuleSet> getRuleSets(@PathVariable("mode") String mode) {
128
        LOGGER.info("Getting rulesets for mode: " + mode);
129
        return rulesetMap.get(mode);
130
    }
131

    
132
    @Override
133
    public List<String> getSetsOfRepository(@RequestParam(value = "url", required = true) String url) {
134
        LOGGER.debug("Getting sets of repository with url : " + url);
135
        try {
136
            return OaiTools.getSetsOfRepo(url);
137
        } catch (Exception e) {
138
            e.printStackTrace();
139
        }
140
        return null;
141
    }
142

    
143
    @Override
144
    public boolean identifyRepo(@RequestParam(value = "url", required = true) String url) {
145
        LOGGER.debug("Identify repository with url : " + url);
146
        try {
147
            return OaiTools.identifyRepository(url);
148
        } catch (Exception e) {
149
            LOGGER.error("Error while identifying repository with url: " + url, e);
150
            return false;
151
        }
152
    }
153

    
154
    @Override
155
    public RuleSet getRuleSet(@PathVariable("acronym") String acronym) {
156
        LOGGER.debug("Getting ruleset with acronym : " + acronym);
157
        RuleSet ruleSet = null;
158
        try {
159
            for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
160
                for (RuleSet rSet : ruleSets)
161
                    if (rSet.getGuidelinesAcronym().equals(acronym)) {
162
                        ruleSet = rSet;
163
                        break;
164
                    }
165
            }
166
            return ruleSet;
167
        } catch (Exception e) {
168
            LOGGER.error("Error getting ruleset", e);
169
            return null;
170
        }
171
    }
172

    
173
    @Override
174
    @PreAuthorize("hasRole('ROLE_USER')")
175
    public List<StoredJob> getStoredJobsNew(@RequestParam("user") @ApiParam(value = "User email", required = true) String user,
176
                                            @RequestParam(value = "jobType", required = false)
177
                                            @ApiParam(value = "Equals to filter job type on validation history page") String jobType,
178
                                            @RequestParam("offset") @ApiParam(value = "Page number", required = true) String offset,
179
                                            @RequestParam(value = "limit", required = false,defaultValue = "10") @ApiParam(value = "Null value") String limit,
180
                                            @RequestParam(value = "dateFrom", required = false) @ApiParam(value = "Null value") String dateFrom,
181
                                            @RequestParam(value = "dateTo", required = false) @ApiParam(value = "Null value") String dateTo,
182
                                            @RequestParam("validationStatus") @ApiParam(value = "Equals to filter validation jobs", required = true) String validationStatus
183
                                            ) throws ValidatorServiceException {
184
        return getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
185
    }
186

    
187
    @Override
188
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
189
        return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
190
    }
191

    
192
    @Override
193
    public InterfaceInformation getInterfaceInformation(@RequestParam(value = "baseUrl", required = true) String baseUrl) throws ValidationServiceException {
194
        try {
195
            LOGGER.debug("Getting interface information with url: " + baseUrl);
196
            InterfaceInformation interfaceInformation = new InterfaceInformation();
197
            interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
198
            if (interfaceInformation.isIdentified())
199
                interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
200

    
201
            return interfaceInformation;
202
        } catch (Exception e) {
203
            LOGGER.error("Error getting interface information with url: " + baseUrl, e);
204
//            emailUtils.reportException(e);
205
            throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
206
        }
207
    }
208

    
209

    
210
}
(14-14/14)