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
|
try {
|
135
|
emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(), jobForValidation);
|
136
|
} catch (Exception e) {
|
137
|
LOGGER.error("Error sending email ", e);
|
138
|
}
|
139
|
/////////////////////////////////////////////////////////////////////////////////////////
|
140
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
141
|
/////////////////////////////////////////////////////////////////////////////////////////
|
142
|
if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 &&
|
143
|
jobForValidation.getSelectedContentRules().contains(-1000)) {
|
144
|
crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail());
|
145
|
} else {
|
146
|
this.getValidationService().submitValidationJob(jobForValidation);
|
147
|
}
|
148
|
/////////////////////////////////////////////////////////////////////////////////////////
|
149
|
/////////////////////////////////////////////////////////////////////////////////////////
|
150
|
// this.getValidationService().submitValidationJob(jobForValidation);
|
151
|
} catch (Exception e) {
|
152
|
throw new ValidatorServiceException(e);
|
153
|
}
|
154
|
|
155
|
return jobForValidation;
|
156
|
}
|
157
|
|
158
|
@Override
|
159
|
@PreAuthorize("hasAuthority('REGISTERED_USER') and #email == authentication.userInfo.email")
|
160
|
public ResponseEntity<Object> reSubmitJobForValidation(String email,
|
161
|
String jobId) throws JSONException, ValidatorServiceException {
|
162
|
LOGGER.debug("Resubmit validation job with id : " + jobId);
|
163
|
StoredJob job = monitorApi.getJobSummary(jobId, "all");
|
164
|
Set<Integer> contentRules = new HashSet<Integer>();
|
165
|
Set<Integer> usageRules = new HashSet<Integer>();
|
166
|
|
167
|
RuleSet ruleSet = null;
|
168
|
for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
|
169
|
for (RuleSet rSet : ruleSets) {
|
170
|
if (rSet.getName().equalsIgnoreCase("CRIS Validation")) {
|
171
|
crisValidatorExecutor.submit(job.getBaseUrl(), job.getUserEmail());
|
172
|
return new ResponseEntity<>("OK",HttpStatus.OK);
|
173
|
} else if(rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())){
|
174
|
ruleSet = rSet;
|
175
|
break;
|
176
|
}
|
177
|
}
|
178
|
}
|
179
|
if (ruleSet != null){
|
180
|
for (int ruleId : job.getRules()) {
|
181
|
if (ruleSet.getContentRulesIds().contains(ruleId))
|
182
|
contentRules.add(ruleId);
|
183
|
else if (ruleSet.getUsageRulesIds().contains(ruleId))
|
184
|
usageRules.add(ruleId);
|
185
|
}
|
186
|
}
|
187
|
if (!contentRules.isEmpty())
|
188
|
job.setSelectedContentRules(contentRules);
|
189
|
if (!usageRules.isEmpty())
|
190
|
job.setSelectedUsageRules(usageRules);
|
191
|
this.submitJobForValidation(job);
|
192
|
return new ResponseEntity<>("OK",HttpStatus.OK);
|
193
|
}
|
194
|
|
195
|
@Override
|
196
|
public List<RuleSet> getRuleSets(String mode) {
|
197
|
LOGGER.info("Getting rulesets for mode: " + mode);
|
198
|
return rulesetMap.get(mode);
|
199
|
}
|
200
|
|
201
|
@Override
|
202
|
public List<String> getSetsOfRepository(String url) {
|
203
|
LOGGER.debug("Getting sets of repository with url : " + url);
|
204
|
|
205
|
List<String> sets = null;
|
206
|
|
207
|
try {
|
208
|
sets = OaiTools.getSetsOfRepo(url);
|
209
|
} catch (Exception e) {
|
210
|
LOGGER.error("Exception on getSetsOfRepository" , e);
|
211
|
}
|
212
|
|
213
|
return sets;
|
214
|
}
|
215
|
|
216
|
@Override
|
217
|
public boolean identifyRepo(String url) {
|
218
|
LOGGER.debug("Identify repository with url : " + url);
|
219
|
try {
|
220
|
return OaiTools.identifyRepository(url);
|
221
|
} catch (Exception e) {
|
222
|
LOGGER.error("Error while identifying repository with url: " + url, e);
|
223
|
return false;
|
224
|
}
|
225
|
}
|
226
|
|
227
|
@Override
|
228
|
public RuleSet getRuleSet(String acronym) {
|
229
|
LOGGER.debug("Getting ruleset with acronym : " + acronym);
|
230
|
RuleSet ruleSet = null;
|
231
|
try {
|
232
|
for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
|
233
|
for (RuleSet rSet : ruleSets)
|
234
|
if (rSet.getGuidelinesAcronym().equals(acronym)) {
|
235
|
ruleSet = rSet;
|
236
|
break;
|
237
|
}
|
238
|
}
|
239
|
return ruleSet;
|
240
|
} catch (Exception e) {
|
241
|
LOGGER.error("Error getting ruleset", e);
|
242
|
return null;
|
243
|
}
|
244
|
}
|
245
|
|
246
|
@Override
|
247
|
@PreAuthorize("hasAuthority('REGISTERED_USER')")
|
248
|
public List<StoredJob> getStoredJobsNew(String user,
|
249
|
String jobType,
|
250
|
String offset,
|
251
|
String limit,
|
252
|
String dateFrom,
|
253
|
String dateTo,
|
254
|
String validationStatus ) throws ValidatorServiceException {
|
255
|
// return getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
|
256
|
List<StoredJob> jobs = getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
|
257
|
/////////////////////////////////////////////////////////////////////////////////////////
|
258
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
259
|
/////////////////////////////////////////////////////////////////////////////////////////
|
260
|
if (jobs == null) {
|
261
|
jobs = new ArrayList<>();
|
262
|
}
|
263
|
List<Job> cj = crisJobs.getJobs(user);
|
264
|
for (Job job : cj) {
|
265
|
jobs.add(CrisValidatorUtils.convertJobToStoredJob(job));
|
266
|
}
|
267
|
/////////////////////////////////////////////////////////////////////////////////////////
|
268
|
/////////////////////////////////////////////////////////////////////////////////////////
|
269
|
return jobs;
|
270
|
}
|
271
|
|
272
|
@Override
|
273
|
public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws ValidatorServiceException {
|
274
|
return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
|
275
|
}
|
276
|
|
277
|
@Override
|
278
|
public InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException {
|
279
|
try {
|
280
|
LOGGER.debug("Getting interface information with url: " + baseUrl);
|
281
|
InterfaceInformation interfaceInformation = new InterfaceInformation();
|
282
|
interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
|
283
|
if (interfaceInformation.isIdentified())
|
284
|
interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
|
285
|
|
286
|
return interfaceInformation;
|
287
|
} catch (Exception e) {
|
288
|
LOGGER.error("Error getting interface information with url: " + baseUrl, e);
|
289
|
throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
|
290
|
}
|
291
|
}
|
292
|
|
293
|
@Override
|
294
|
public List<StoredJob> getJobsSummary(String repoId, int limit) throws JSONException, ValidatorServiceException {
|
295
|
return getValidationService().getJobSummary(repositoryService.getRepositoryInterface(repoId).stream().map(RepositoryInterface::getBaseUrl).collect(Collectors.toList()),limit);
|
296
|
}
|
297
|
|
298
|
}
|