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