1
|
package eu.dnetlib.repo.manager.api.application.rest;
|
2
|
|
3
|
import eu.dnetlib.api.functionality.ValidatorServiceException;
|
4
|
import eu.dnetlib.domain.functionality.validator.StoredJob;
|
5
|
import eu.dnetlib.repo.manager.api.application.utils.OaiTools;
|
6
|
import gr.uoa.di.driver.util.ServiceLocator;
|
7
|
import eu.dnetlib.domain.functionality.validator.JobForValidation;
|
8
|
import eu.dnetlib.domain.functionality.validator.RuleSet;
|
9
|
import eu.dnetlib.repo.manager.api.application.utils.Constants;
|
10
|
|
11
|
import java.util.*;
|
12
|
import java.util.concurrent.ConcurrentHashMap;
|
13
|
import eu.dnetlib.api.functionality.ValidatorService;
|
14
|
import org.json.JSONObject;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.stereotype.Component;
|
17
|
import org.springframework.web.bind.annotation.PathVariable;
|
18
|
|
19
|
import javax.annotation.PostConstruct;
|
20
|
|
21
|
@Component
|
22
|
public class ValidatorApiImpl implements ValidatorApi{
|
23
|
|
24
|
@Autowired
|
25
|
private MonitorApiImpl monitorApi;
|
26
|
|
27
|
private ServiceLocator<ValidatorService> validatorServiceLocator;
|
28
|
|
29
|
private ValidatorService getValidationService() {
|
30
|
return this.validatorServiceLocator.getService();
|
31
|
}
|
32
|
|
33
|
public ServiceLocator<ValidatorService> getValidatorServiceLocator() {
|
34
|
return validatorServiceLocator;
|
35
|
}
|
36
|
|
37
|
public void setValidatorServiceLocator(ServiceLocator<ValidatorService> validatorServiceLocator) {
|
38
|
this.validatorServiceLocator = validatorServiceLocator;
|
39
|
}
|
40
|
|
41
|
private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
|
42
|
|
43
|
@PostConstruct
|
44
|
private void loadRules(){
|
45
|
|
46
|
try {
|
47
|
for (RuleSet ruleSet : getValidationService().getRuleSets()) {
|
48
|
if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
|
49
|
String key = "";
|
50
|
if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
|
51
|
key = Constants.VALIDATION_MODE_DATA;
|
52
|
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
|
53
|
key = Constants.VALIDATION_MODE_LITERATURE;
|
54
|
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
|
55
|
key = Constants.VALIDATION_MODE_CRIS;
|
56
|
|
57
|
if (rulesetMap.containsKey(key))
|
58
|
rulesetMap.get(key).add(ruleSet);
|
59
|
else {
|
60
|
List<RuleSet> ruleSets = new ArrayList<RuleSet>();
|
61
|
ruleSets.add(ruleSet);
|
62
|
rulesetMap.put(key, ruleSets);
|
63
|
}
|
64
|
}
|
65
|
}
|
66
|
} catch (ValidatorServiceException e) {
|
67
|
e.printStackTrace();
|
68
|
}
|
69
|
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public void submitJobForValidation(JobForValidation jobForValidation) {
|
74
|
try {
|
75
|
this.getValidationService().submitValidationJob(jobForValidation);
|
76
|
} catch (ValidatorServiceException e) {
|
77
|
e.printStackTrace();
|
78
|
}
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
public void reSubmitJobForValidation(String jobId) {
|
83
|
|
84
|
JSONObject jsonObject = new JSONObject();
|
85
|
jsonObject.put("jobId",jobId);
|
86
|
jsonObject.put("groupBy","all");
|
87
|
|
88
|
StoredJob job = monitorApi.getJobSummary(jsonObject.toString());
|
89
|
Set<Integer> contentRules = new HashSet<Integer>();
|
90
|
Set<Integer> usageRules = new HashSet<Integer>();
|
91
|
|
92
|
RuleSet ruleSet = null;
|
93
|
for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
|
94
|
for (RuleSet rSet : ruleSets)
|
95
|
if (rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())) {
|
96
|
ruleSet = rSet;
|
97
|
break;
|
98
|
}
|
99
|
}
|
100
|
|
101
|
for (int ruleId : job.getRules()) {
|
102
|
if (ruleSet.getContentRulesIds().contains(ruleId))
|
103
|
contentRules.add(ruleId);
|
104
|
else if (ruleSet.getUsageRulesIds().contains(ruleId))
|
105
|
usageRules.add(ruleId);
|
106
|
}
|
107
|
if (!contentRules.isEmpty())
|
108
|
job.setSelectedContentRules(contentRules);
|
109
|
if (!usageRules.isEmpty())
|
110
|
job.setSelectedUsageRules(usageRules);
|
111
|
this.submitJobForValidation(job);
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public List<RuleSet> getRuleSets() {
|
116
|
return rulesetMap.get(Constants.VALIDATION_MODE_DATA);
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public List<String> getSetsOfRepository(@PathVariable("url") String url) {
|
121
|
try {
|
122
|
return OaiTools.getSetsOfRepo(url);
|
123
|
} catch (Exception e) {
|
124
|
e.printStackTrace();
|
125
|
}
|
126
|
return null;
|
127
|
}
|
128
|
|
129
|
}
|