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