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.stereotype.Component;
21
import org.springframework.web.bind.annotation.PathVariable;
22
import org.springframework.web.bind.annotation.RequestBody;
23
import org.springframework.web.bind.annotation.RequestParam;
24

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

    
28

    
29
@Component
30
public class ValidatorApiImpl implements ValidatorApi{
31

    
32
    @Autowired
33
    private MonitorApiImpl monitorApi;
34

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

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

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

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

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

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

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

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

    
82
    }
83

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

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

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

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

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

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

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

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

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

    
183
    @Override
184
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
185
        return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
186
    }
187

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

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

    
205

    
206
}
(12-12/12)