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.http.HttpStatus;
|
21
|
import org.springframework.http.ResponseEntity;
|
22
|
import org.springframework.security.access.prepost.PreAuthorize;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
import org.springframework.web.bind.annotation.PathVariable;
|
25
|
import org.springframework.web.bind.annotation.RequestBody;
|
26
|
import org.springframework.web.bind.annotation.RequestParam;
|
27
|
|
28
|
import javax.annotation.PostConstruct;
|
29
|
import javax.annotation.Resource;
|
30
|
|
31
|
|
32
|
@Component
|
33
|
public class ValidatorApiImpl implements ValidatorApi{
|
34
|
|
35
|
@Autowired
|
36
|
private MonitorApiImpl monitorApi;
|
37
|
|
38
|
@Resource(name = "validatorServiceLocator")
|
39
|
private ServiceLocator<ValidatorService> validatorServiceLocator;
|
40
|
|
41
|
private ValidatorService getValidationService() {
|
42
|
return this.validatorServiceLocator.getService();
|
43
|
}
|
44
|
|
45
|
public ServiceLocator<ValidatorService> getValidatorServiceLocator() {
|
46
|
return validatorServiceLocator;
|
47
|
}
|
48
|
|
49
|
public void setValidatorServiceLocator(ServiceLocator<ValidatorService> validatorServiceLocator) {
|
50
|
this.validatorServiceLocator = validatorServiceLocator;
|
51
|
}
|
52
|
|
53
|
private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
|
54
|
|
55
|
private static final Logger LOGGER = Logger
|
56
|
.getLogger(ValidatorApiImpl.class);
|
57
|
|
58
|
@Autowired
|
59
|
private EmailUtils emailUtils;
|
60
|
|
61
|
@PostConstruct
|
62
|
private void loadRules(){
|
63
|
LOGGER.debug("PostConstruct method! Load rules!");
|
64
|
try {
|
65
|
for (RuleSet ruleSet : getValidationService().getRuleSets()) {
|
66
|
if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
|
67
|
String key = "";
|
68
|
if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
|
69
|
key = Constants.VALIDATION_MODE_DATA;
|
70
|
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
|
71
|
key = Constants.VALIDATION_MODE_LITERATURE;
|
72
|
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
|
73
|
key = Constants.VALIDATION_MODE_CRIS;
|
74
|
|
75
|
if (rulesetMap.containsKey(key))
|
76
|
rulesetMap.get(key).add(ruleSet);
|
77
|
else {
|
78
|
List<RuleSet> ruleSets = new ArrayList<RuleSet>();
|
79
|
ruleSets.add(ruleSet);
|
80
|
rulesetMap.put(key, ruleSets);
|
81
|
}
|
82
|
}
|
83
|
}
|
84
|
} catch (ValidatorServiceException e) {
|
85
|
e.printStackTrace();
|
86
|
}
|
87
|
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
@PreAuthorize("hasRole('ROLE_USER') and #jobForValidation.userEmail == authentication.userInfo.email")
|
92
|
public JobForValidation submitJobForValidation(@RequestBody JobForValidation jobForValidation) throws ValidatorServiceException {
|
93
|
LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId());
|
94
|
try {
|
95
|
this.getValidationService().submitValidationJob(jobForValidation);
|
96
|
} catch (ValidatorServiceException e) {
|
97
|
LOGGER.debug("Exception on submitJobForValidation" , e);
|
98
|
emailUtils.reportException(e);
|
99
|
throw e;
|
100
|
}
|
101
|
return jobForValidation;
|
102
|
}
|
103
|
|
104
|
@Override
|
105
|
@PreAuthorize("hasRole('ROLE_USER') and #email == authentication.userInfo.email")
|
106
|
public ResponseEntity<Object> reSubmitJobForValidation(@PathVariable("email") String email,
|
107
|
@PathVariable("jobId") String jobId) throws JSONException, ValidatorServiceException {
|
108
|
LOGGER.debug("Resubmit validation job with id : " + jobId);
|
109
|
StoredJob job = monitorApi.getJobSummary(jobId,"all");
|
110
|
Set<Integer> contentRules = new HashSet<Integer>();
|
111
|
Set<Integer> usageRules = new HashSet<Integer>();
|
112
|
|
113
|
RuleSet ruleSet = null;
|
114
|
for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
|
115
|
for (RuleSet rSet : ruleSets)
|
116
|
if (rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())) {
|
117
|
ruleSet = rSet;
|
118
|
break;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
for (int ruleId : job.getRules()) {
|
123
|
if (ruleSet.getContentRulesIds().contains(ruleId))
|
124
|
contentRules.add(ruleId);
|
125
|
else if (ruleSet.getUsageRulesIds().contains(ruleId))
|
126
|
usageRules.add(ruleId);
|
127
|
}
|
128
|
if (!contentRules.isEmpty())
|
129
|
job.setSelectedContentRules(contentRules);
|
130
|
if (!usageRules.isEmpty())
|
131
|
job.setSelectedUsageRules(usageRules);
|
132
|
this.submitJobForValidation(job);
|
133
|
return new ResponseEntity<>("OK",HttpStatus.OK);
|
134
|
}
|
135
|
|
136
|
@Override
|
137
|
public List<RuleSet> getRuleSets(@PathVariable("mode") String mode) {
|
138
|
LOGGER.info("Getting rulesets for mode: " + mode);
|
139
|
return rulesetMap.get(mode);
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public List<String> getSetsOfRepository(@RequestParam(value = "url", required = true) String url) {
|
144
|
LOGGER.debug("Getting sets of repository with url : " + url);
|
145
|
try {
|
146
|
return OaiTools.getSetsOfRepo(url);
|
147
|
} catch (Exception e) {
|
148
|
LOGGER.debug("Exception on getSetsOfRepository" , e);
|
149
|
emailUtils.reportException(e);
|
150
|
}
|
151
|
return null;
|
152
|
}
|
153
|
|
154
|
@Override
|
155
|
public boolean identifyRepo(@RequestParam(value = "url", required = true) String url) {
|
156
|
LOGGER.debug("Identify repository with url : " + url);
|
157
|
try {
|
158
|
return OaiTools.identifyRepository(url);
|
159
|
} catch (Exception e) {
|
160
|
LOGGER.error("Error while identifying repository with url: " + url, e);
|
161
|
emailUtils.reportException(e);
|
162
|
return false;
|
163
|
}
|
164
|
}
|
165
|
|
166
|
@Override
|
167
|
public RuleSet getRuleSet(@PathVariable("acronym") String acronym) {
|
168
|
LOGGER.debug("Getting ruleset with acronym : " + acronym);
|
169
|
RuleSet ruleSet = null;
|
170
|
try {
|
171
|
for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
|
172
|
for (RuleSet rSet : ruleSets)
|
173
|
if (rSet.getGuidelinesAcronym().equals(acronym)) {
|
174
|
ruleSet = rSet;
|
175
|
break;
|
176
|
}
|
177
|
}
|
178
|
return ruleSet;
|
179
|
} catch (Exception e) {
|
180
|
LOGGER.error("Error getting ruleset", e);
|
181
|
emailUtils.reportException(e);
|
182
|
return null;
|
183
|
}
|
184
|
}
|
185
|
|
186
|
@Override
|
187
|
@PreAuthorize("hasRole('ROLE_USER')")
|
188
|
public List<StoredJob> getStoredJobsNew(@RequestParam("user") @ApiParam(value = "User email", required = true) String user,
|
189
|
@RequestParam(value = "jobType", required = false)
|
190
|
@ApiParam(value = "Equals to filter job type on validation history page") String jobType,
|
191
|
@RequestParam("offset") @ApiParam(value = "Page number", required = true) String offset,
|
192
|
@RequestParam(value = "limit", required = false,defaultValue = "10") @ApiParam(value = "Null value") String limit,
|
193
|
@RequestParam(value = "dateFrom", required = false) @ApiParam(value = "Null value") String dateFrom,
|
194
|
@RequestParam(value = "dateTo", required = false) @ApiParam(value = "Null value") String dateTo,
|
195
|
@RequestParam("validationStatus") @ApiParam(value = "Equals to filter validation jobs", required = true) String validationStatus
|
196
|
) throws ValidatorServiceException {
|
197
|
return getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
|
198
|
}
|
199
|
|
200
|
@Override
|
201
|
public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws ValidatorServiceException {
|
202
|
return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
|
203
|
}
|
204
|
|
205
|
@Override
|
206
|
public InterfaceInformation getInterfaceInformation(@RequestParam(value = "baseUrl", required = true) String baseUrl) throws ValidationServiceException {
|
207
|
try {
|
208
|
LOGGER.debug("Getting interface information with url: " + baseUrl);
|
209
|
InterfaceInformation interfaceInformation = new InterfaceInformation();
|
210
|
interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
|
211
|
if (interfaceInformation.isIdentified())
|
212
|
interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
|
213
|
|
214
|
return interfaceInformation;
|
215
|
} catch (Exception e) {
|
216
|
LOGGER.error("Error getting interface information with url: " + baseUrl, e);
|
217
|
emailUtils.reportException(e);
|
218
|
throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
|
219
|
}
|
220
|
}
|
221
|
|
222
|
|
223
|
}
|