Project

General

Profile

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

    
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.data.RepositoryInterface;
5
import eu.dnetlib.domain.functionality.validator.*;
6
import eu.dnetlib.repo.manager.domain.Constants;
7
import eu.dnetlib.repo.manager.domain.InterfaceInformation;
8
import eu.dnetlib.repo.manager.domain.ValidationServiceException;
9
import eu.dnetlib.repo.manager.utils.CrisValidatorUtils;
10
import eu.dnetlib.repo.manager.utils.OaiTools;
11
import gr.uoa.di.driver.util.ServiceLocator;
12
import org.apache.log4j.Logger;
13
import org.eurocris.openaire.cris.validator.model.Job;
14
import org.eurocris.openaire.cris.validator.service.JobExecutor;
15
import org.eurocris.openaire.cris.validator.service.MapJobDao;
16
import org.json.JSONException;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.http.HttpStatus;
19
import org.springframework.http.ResponseEntity;
20
import org.springframework.security.access.prepost.PreAuthorize;
21
import org.springframework.security.core.context.SecurityContextHolder;
22
import org.springframework.stereotype.Service;
23

    
24
import javax.annotation.PostConstruct;
25
import javax.annotation.Resource;
26
import java.util.*;
27
import java.util.concurrent.ConcurrentHashMap;
28
import java.util.stream.Collectors;
29

    
30

    
31
@Service("validatorService")
32
public class ValidatorServiceImpl implements ValidatorService {
33

    
34
    @Autowired
35
    private MonitorServiceImpl monitorApi;
36

    
37
    @Autowired
38
    private RepositoryService repositoryService;
39

    
40
    @Resource(name = "validatorServiceLocator")
41
    private ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator;
42

    
43
    private eu.dnetlib.api.functionality.ValidatorService getValidationService() {
44
        return this.validatorServiceLocator.getService();
45
    }
46

    
47
    public ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> getValidatorServiceLocator() {
48
        return validatorServiceLocator;
49
    }
50

    
51
    public void setValidatorServiceLocator(ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator) {
52
        this.validatorServiceLocator = validatorServiceLocator;
53
    }
54

    
55
    private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
56

    
57
    private static final Logger LOGGER = Logger
58
            .getLogger(ValidatorServiceImpl.class);
59

    
60
    @Autowired
61
    private EmailUtils emailUtils;
62

    
63
    @Autowired
64
    private JobExecutor crisValidatorExecutor;
65

    
66
    @Autowired
67
    private MapJobDao crisJobs;
68

    
69
    @PostConstruct
70
    private void loadRules(){
71
        LOGGER.debug("PostConstruct method! Load rules!");
72
        try {
73
            for (RuleSet ruleSet : getValidationService().getRuleSets()) {
74
                if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
75
                    String key = "";
76
                    if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
77
                        key = Constants.VALIDATION_MODE_DATA;
78
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
79
                        key = Constants.VALIDATION_MODE_LITERATURE;
80
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
81
                        key = Constants.VALIDATION_MODE_CRIS;
82
                    else if (ruleSet.getGuidelinesAcronym().matches(".*fair$"))
83
                        key = Constants.VALIDATION_MODE_FAIR;
84

    
85
                    if (rulesetMap.containsKey(key))
86
                        rulesetMap.get(key).add(ruleSet);
87
                    else {
88
                        List<RuleSet> ruleSets = new ArrayList<RuleSet>();
89
                        ruleSets.add(ruleSet);
90
                        rulesetMap.put(key, ruleSets);
91
                    }
92
                }
93
            }
94

    
95
            /////////////////////////////////////////////////////////////////////////////////////////
96
            // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
97
            /////////////////////////////////////////////////////////////////////////////////////////
98
            int ruleId = -1000;
99
            Rule rule = new Rule();
100
            rule.setType("cris");
101
            rule.setName("CRIS Rules");
102
            rule.setId(ruleId);
103
            rule.setMandatory(true);
104
            RuleSet crisRuleSet = new RuleSet();
105
            crisRuleSet.setContentRulesIds(Collections.singleton(ruleId));
106
            crisRuleSet.setContentRules(Collections.singletonList(rule));
107
            crisRuleSet.setUsageRulesIds(Collections.singleton(ruleId));
108
            crisRuleSet.setUsageRules(Collections.singletonList(rule));
109
            crisRuleSet.setId(-1);
110
            crisRuleSet.setDescription("Validates using the CRIS Validator implementation");
111
            crisRuleSet.setName("CRIS Validation");
112

    
113
            String crisKey = Constants.VALIDATION_MODE_CRIS;
114
            if (rulesetMap.containsKey(crisKey))
115
                rulesetMap.get(crisKey).add(crisRuleSet);
116
            else {
117
                List<RuleSet> ruleSets = new ArrayList<RuleSet>();
118
                ruleSets.add(crisRuleSet);
119
                rulesetMap.put(crisKey, ruleSets);
120
            }
121
            /////////////////////////////////////////////////////////////////////////////////////////
122
            /////////////////////////////////////////////////////////////////////////////////////////
123
        } catch (ValidatorServiceException e) {
124
            LOGGER.error(e);
125
        }
126

    
127
    }
128

    
129
    @Override
130
    @PreAuthorize("hasAuthority('REGISTERED_USER') and #jobForValidation.userEmail == authentication.userInfo.email")
131
    public JobForValidation submitJobForValidation(JobForValidation jobForValidation) throws ValidatorServiceException {
132
        LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId());
133
        try {
134
            emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(),jobForValidation);
135
            /////////////////////////////////////////////////////////////////////////////////////////
136
            // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
137
            /////////////////////////////////////////////////////////////////////////////////////////
138
            if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 &&
139
                    jobForValidation.getSelectedContentRules().contains(-1000)) {
140
                crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail());
141
            } else {
142
                this.getValidationService().submitValidationJob(jobForValidation);
143
            }
144
            /////////////////////////////////////////////////////////////////////////////////////////
145
            /////////////////////////////////////////////////////////////////////////////////////////
146
//            this.getValidationService().submitValidationJob(jobForValidation);
147
        } catch (ValidatorServiceException e) {
148
            LOGGER.debug("Exception on submitJobForValidation" , e);
149
            emailUtils.reportException(e);
150
            throw e;
151
        } catch (Exception e) {
152
            LOGGER.error(e);
153
        }
154
        return jobForValidation;
155
    }
156

    
157
    @Override
158
    @PreAuthorize("hasAuthority('REGISTERED_USER') and #email == authentication.userInfo.email")
159
    public ResponseEntity<Object> reSubmitJobForValidation(String email,
160
                                                           String jobId) throws JSONException, ValidatorServiceException {
161
        LOGGER.debug("Resubmit validation job with id : " + jobId);
162
        StoredJob job = monitorApi.getJobSummary(jobId, "all");
163
        Set<Integer> contentRules = new HashSet<Integer>();
164
        Set<Integer> usageRules = new HashSet<Integer>();
165

    
166
        RuleSet ruleSet = null;
167
        for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
168
            for (RuleSet rSet : ruleSets) {
169
                if (rSet.getName().equalsIgnoreCase("CRIS Validation")) {
170
                    crisValidatorExecutor.submit(job.getBaseUrl(), job.getUserEmail());
171
                    return new ResponseEntity<>("OK",HttpStatus.OK);
172
                } else if(rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())){
173
                    ruleSet = rSet;
174
                    break;
175
                }
176
            }
177
        }
178
        if (ruleSet != null){
179
            for (int ruleId : job.getRules()) {
180
                if (ruleSet.getContentRulesIds().contains(ruleId))
181
                    contentRules.add(ruleId);
182
                else if (ruleSet.getUsageRulesIds().contains(ruleId))
183
                    usageRules.add(ruleId);
184
            }
185
        }
186
        if (!contentRules.isEmpty())
187
            job.setSelectedContentRules(contentRules);
188
        if (!usageRules.isEmpty())
189
            job.setSelectedUsageRules(usageRules);
190
        this.submitJobForValidation(job);
191
        return new ResponseEntity<>("OK",HttpStatus.OK);
192
    }
193

    
194
    @Override
195
    public List<RuleSet> getRuleSets(String mode) {
196
        LOGGER.info("Getting rulesets for mode: " + mode);
197
        return rulesetMap.get(mode);
198
    }
199

    
200
    @Override
201
    public List<String> getSetsOfRepository(String url) {
202
        LOGGER.debug("Getting sets of repository with url : " + url);
203
        try {
204
            return OaiTools.getSetsOfRepo(url);
205
        } catch (Exception e) {
206
            LOGGER.debug("Exception on getSetsOfRepository" , e);
207
            emailUtils.reportException(e);
208
        }
209
        return null;
210
    }
211

    
212
    @Override
213
    public boolean identifyRepo(String url) {
214
        LOGGER.debug("Identify repository with url : " + url);
215
        try {
216
            return OaiTools.identifyRepository(url);
217
        } catch (Exception e) {
218
            LOGGER.error("Error while identifying repository with url: " + url, e);
219
            emailUtils.reportException(e);
220
            return false;
221
        }
222
    }
223

    
224
    @Override
225
    public RuleSet getRuleSet(String acronym) {
226
        LOGGER.debug("Getting ruleset with acronym : " + acronym);
227
        RuleSet ruleSet = null;
228
        try {
229
            for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
230
                for (RuleSet rSet : ruleSets)
231
                    if (rSet.getGuidelinesAcronym().equals(acronym)) {
232
                        ruleSet = rSet;
233
                        break;
234
                    }
235
            }
236
            return ruleSet;
237
        } catch (Exception e) {
238
            LOGGER.error("Error getting ruleset", e);
239
            emailUtils.reportException(e);
240
            return null;
241
        }
242
    }
243

    
244
    @Override
245
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
246
    public List<StoredJob> getStoredJobsNew(String user,
247
                                            String jobType,
248
                                            String offset,
249
                                            String limit,
250
                                            String dateFrom,
251
                                            String dateTo,
252
                                            String validationStatus ) throws ValidatorServiceException {
253
//        return getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
254
        List<StoredJob> jobs = getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
255
        /////////////////////////////////////////////////////////////////////////////////////////
256
        // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
257
        /////////////////////////////////////////////////////////////////////////////////////////
258
        if (jobs == null) {
259
            jobs = new ArrayList<>();
260
        }
261
        List<Job> cj = crisJobs.getJobs(user);
262
        for (Job job : cj) {
263
            jobs.add(CrisValidatorUtils.convertJobToStoredJob(job));
264
        }
265
        /////////////////////////////////////////////////////////////////////////////////////////
266
        /////////////////////////////////////////////////////////////////////////////////////////
267
        return jobs;
268
    }
269

    
270
    @Override
271
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
272
        return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
273
    }
274

    
275
    @Override
276
    public InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException {
277
        try {
278
            LOGGER.debug("Getting interface information with url: " + baseUrl);
279
            InterfaceInformation interfaceInformation = new InterfaceInformation();
280
            interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
281
            if (interfaceInformation.isIdentified())
282
                interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
283

    
284
            return interfaceInformation;
285
        } catch (Exception e) {
286
            LOGGER.error("Error getting interface information with url: " + baseUrl, e);
287
            emailUtils.reportException(e);
288
            throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
289
        }
290
    }
291

    
292
    @Override
293
    public List<StoredJob> getJobsSummary(String repoId, int limit) throws JSONException, ValidatorServiceException {
294
        return getValidationService().getJobSummary(repositoryService.getRepositoryInterface(repoId).stream().map(RepositoryInterface::getBaseUrl).collect(Collectors.toList()),limit);
295
    }
296

    
297
}
(20-20/20)