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.RequestParam;
23

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

    
27

    
28
@Component
29
public class ValidatorApiImpl implements ValidatorApi{
30

    
31
    @Autowired
32
    private MonitorApiImpl monitorApi;
33

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

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

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

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

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

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

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

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

    
81
    }
82

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

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

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

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

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

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

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

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

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

    
180
    @Override
181
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
182
        return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
183
    }
184

    
185
    @Override
186
    public InterfaceInformation getInterfaceInformation(@PathVariable("baseUrl") String baseUrl) throws ValidationServiceException {
187
        try {
188
            LOGGER.debug("Getting interface information with url: " + baseUrl);
189
            InterfaceInformation interfaceInformation = new InterfaceInformation();
190
            interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
191
            if (interfaceInformation.isIdentified())
192
                interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
193

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

    
202

    
203
}
(12-12/12)