2019-02-16 11:04:41 +01:00
|
|
|
package eu.dnetlib.repo.manager.service;
|
|
|
|
|
|
|
|
import eu.dnetlib.api.functionality.ValidatorServiceException;
|
2022-12-01 14:19:29 +01:00
|
|
|
import eu.dnetlib.domain.functionality.validator.JobForValidation;
|
|
|
|
import eu.dnetlib.domain.functionality.validator.Rule;
|
|
|
|
import eu.dnetlib.domain.functionality.validator.RuleSet;
|
|
|
|
import eu.dnetlib.domain.functionality.validator.StoredJob;
|
2019-11-28 12:25:56 +01:00
|
|
|
import eu.dnetlib.repo.manager.domain.Constants;
|
|
|
|
import eu.dnetlib.repo.manager.domain.InterfaceInformation;
|
2022-12-01 14:19:29 +01:00
|
|
|
import eu.dnetlib.repo.manager.domain.RepositoryInterface;
|
2022-08-05 10:48:16 +02:00
|
|
|
import eu.dnetlib.repo.manager.exception.ValidationServiceException;
|
2020-03-18 23:39:25 +01:00
|
|
|
import eu.dnetlib.repo.manager.utils.CrisValidatorUtils;
|
2019-02-16 11:04:41 +01:00
|
|
|
import eu.dnetlib.repo.manager.utils.OaiTools;
|
|
|
|
import gr.uoa.di.driver.util.ServiceLocator;
|
2023-01-11 17:50:31 +01:00
|
|
|
import org.slf4j.Logger;
|
2019-11-20 14:03:40 +01:00
|
|
|
import org.eurocris.openaire.cris.validator.model.Job;
|
|
|
|
import org.eurocris.openaire.cris.validator.service.JobExecutor;
|
|
|
|
import org.eurocris.openaire.cris.validator.service.MapJobDao;
|
2019-02-16 11:04:41 +01:00
|
|
|
import org.json.JSONException;
|
2023-01-11 17:50:31 +01:00
|
|
|
import org.slf4j.LoggerFactory;
|
2019-02-16 11:04:41 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2019-10-01 13:07:47 +02:00
|
|
|
import java.util.stream.Collectors;
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
@Service("validatorService")
|
|
|
|
public class ValidatorServiceImpl implements ValidatorService {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private MonitorServiceImpl monitorApi;
|
|
|
|
|
2019-10-01 13:07:47 +02:00
|
|
|
@Autowired
|
|
|
|
private RepositoryService repositoryService;
|
|
|
|
|
2019-02-16 11:04:41 +01:00
|
|
|
@Resource(name = "validatorServiceLocator")
|
|
|
|
private ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator;
|
|
|
|
|
|
|
|
private eu.dnetlib.api.functionality.ValidatorService getValidationService() {
|
|
|
|
return this.validatorServiceLocator.getService();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> getValidatorServiceLocator() {
|
|
|
|
return validatorServiceLocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setValidatorServiceLocator(ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator) {
|
|
|
|
this.validatorServiceLocator = validatorServiceLocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
|
|
|
|
|
2023-01-11 17:50:31 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ValidatorServiceImpl.class);
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private EmailUtils emailUtils;
|
|
|
|
|
2019-11-20 14:03:40 +01:00
|
|
|
@Autowired
|
|
|
|
private JobExecutor crisValidatorExecutor;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private MapJobDao crisJobs;
|
|
|
|
|
2019-02-16 11:04:41 +01:00
|
|
|
@PostConstruct
|
|
|
|
private void loadRules(){
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("PostConstruct method! Load rules!");
|
2019-02-16 11:04:41 +01:00
|
|
|
try {
|
|
|
|
for (RuleSet ruleSet : getValidationService().getRuleSets()) {
|
|
|
|
if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
|
|
|
|
String key = "";
|
|
|
|
if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
|
|
|
|
key = Constants.VALIDATION_MODE_DATA;
|
|
|
|
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
|
|
|
|
key = Constants.VALIDATION_MODE_LITERATURE;
|
|
|
|
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
|
|
|
|
key = Constants.VALIDATION_MODE_CRIS;
|
2021-04-16 10:16:09 +02:00
|
|
|
else if (ruleSet.getGuidelinesAcronym().matches(".*fair$"))
|
|
|
|
key = Constants.VALIDATION_MODE_FAIR;
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
if (rulesetMap.containsKey(key))
|
|
|
|
rulesetMap.get(key).add(ruleSet);
|
|
|
|
else {
|
|
|
|
List<RuleSet> ruleSets = new ArrayList<RuleSet>();
|
|
|
|
ruleSets.add(ruleSet);
|
|
|
|
rulesetMap.put(key, ruleSets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 14:03:40 +01:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
int ruleId = -1000;
|
|
|
|
Rule rule = new Rule();
|
|
|
|
rule.setType("cris");
|
|
|
|
rule.setName("CRIS Rules");
|
|
|
|
rule.setId(ruleId);
|
|
|
|
rule.setMandatory(true);
|
|
|
|
RuleSet crisRuleSet = new RuleSet();
|
|
|
|
crisRuleSet.setContentRulesIds(Collections.singleton(ruleId));
|
|
|
|
crisRuleSet.setContentRules(Collections.singletonList(rule));
|
|
|
|
crisRuleSet.setUsageRulesIds(Collections.singleton(ruleId));
|
|
|
|
crisRuleSet.setUsageRules(Collections.singletonList(rule));
|
|
|
|
crisRuleSet.setId(-1);
|
|
|
|
crisRuleSet.setDescription("Validates using the CRIS Validator implementation");
|
|
|
|
crisRuleSet.setName("CRIS Validation");
|
|
|
|
|
|
|
|
String crisKey = Constants.VALIDATION_MODE_CRIS;
|
|
|
|
if (rulesetMap.containsKey(crisKey))
|
|
|
|
rulesetMap.get(crisKey).add(crisRuleSet);
|
|
|
|
else {
|
|
|
|
List<RuleSet> ruleSets = new ArrayList<RuleSet>();
|
|
|
|
ruleSets.add(crisRuleSet);
|
|
|
|
rulesetMap.put(crisKey, ruleSets);
|
|
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
2019-02-16 11:04:41 +01:00
|
|
|
} catch (ValidatorServiceException e) {
|
2023-01-11 17:50:31 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-11-24 17:33:21 +01:00
|
|
|
@PreAuthorize("hasAuthority('REGISTERED_USER')")
|
2019-07-25 13:54:21 +02:00
|
|
|
public JobForValidation submitJobForValidation(JobForValidation jobForValidation) throws ValidatorServiceException {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId());
|
2019-02-16 11:04:41 +01:00
|
|
|
try {
|
2021-10-22 13:32:44 +02:00
|
|
|
try {
|
|
|
|
emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(), jobForValidation);
|
|
|
|
} catch (Exception e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("Error sending email ", e);
|
2021-10-22 13:32:44 +02:00
|
|
|
}
|
2019-11-20 14:03:40 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
2019-12-02 12:16:48 +01:00
|
|
|
if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 &&
|
2022-04-20 12:26:28 +02:00
|
|
|
jobForValidation.getSelectedContentRules().contains(-1000)
|
|
|
|
|| jobForValidation.getDesiredCompatibilityLevel().matches("openaire-cris_1.1")) {
|
2019-11-20 14:03:40 +01:00
|
|
|
crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail());
|
|
|
|
} else {
|
|
|
|
this.getValidationService().submitValidationJob(jobForValidation);
|
|
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// this.getValidationService().submitValidationJob(jobForValidation);
|
2022-10-06 12:29:21 +02:00
|
|
|
} catch (Exception e) { // FIXME: replaced exception with log
|
|
|
|
// throw new ValidatorServiceException(e);
|
2023-01-11 17:50:31 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
2021-10-22 13:32:44 +02:00
|
|
|
|
2019-02-16 11:04:41 +01:00
|
|
|
return jobForValidation;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-25 13:54:21 +02:00
|
|
|
public ResponseEntity<Object> reSubmitJobForValidation(String email,
|
|
|
|
String jobId) throws JSONException, ValidatorServiceException {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("Resubmit validation job with id : " + jobId);
|
2019-07-25 14:58:01 +02:00
|
|
|
StoredJob job = monitorApi.getJobSummary(jobId, "all");
|
2019-02-16 11:04:41 +01:00
|
|
|
Set<Integer> contentRules = new HashSet<Integer>();
|
|
|
|
Set<Integer> usageRules = new HashSet<Integer>();
|
|
|
|
|
|
|
|
RuleSet ruleSet = null;
|
|
|
|
for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
|
2020-01-10 15:04:41 +01:00
|
|
|
for (RuleSet rSet : ruleSets) {
|
|
|
|
if (rSet.getName().equalsIgnoreCase("CRIS Validation")) {
|
2020-03-18 23:39:25 +01:00
|
|
|
crisValidatorExecutor.submit(job.getBaseUrl(), job.getUserEmail());
|
|
|
|
return new ResponseEntity<>("OK",HttpStatus.OK);
|
|
|
|
} else if(rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())){
|
2019-02-16 11:04:41 +01:00
|
|
|
ruleSet = rSet;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-10 15:04:41 +01:00
|
|
|
}
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
2019-07-25 14:58:01 +02:00
|
|
|
if (ruleSet != null){
|
|
|
|
for (int ruleId : job.getRules()) {
|
|
|
|
if (ruleSet.getContentRulesIds().contains(ruleId))
|
|
|
|
contentRules.add(ruleId);
|
|
|
|
else if (ruleSet.getUsageRulesIds().contains(ruleId))
|
|
|
|
usageRules.add(ruleId);
|
|
|
|
}
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
if (!contentRules.isEmpty())
|
|
|
|
job.setSelectedContentRules(contentRules);
|
|
|
|
if (!usageRules.isEmpty())
|
|
|
|
job.setSelectedUsageRules(usageRules);
|
|
|
|
this.submitJobForValidation(job);
|
|
|
|
return new ResponseEntity<>("OK",HttpStatus.OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-25 13:54:21 +02:00
|
|
|
public List<RuleSet> getRuleSets(String mode) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.info("Getting rulesets for mode: " + mode);
|
2019-02-16 11:04:41 +01:00
|
|
|
return rulesetMap.get(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-25 13:54:21 +02:00
|
|
|
public List<String> getSetsOfRepository(String url) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("Getting sets of repository with url : " + url);
|
2021-10-22 13:32:44 +02:00
|
|
|
|
|
|
|
List<String> sets = null;
|
|
|
|
|
2019-02-16 11:04:41 +01:00
|
|
|
try {
|
2021-10-22 13:32:44 +02:00
|
|
|
sets = OaiTools.getSetsOfRepo(url);
|
2019-02-16 11:04:41 +01:00
|
|
|
} catch (Exception e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("Exception on getSetsOfRepository" , e);
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
2021-10-22 13:32:44 +02:00
|
|
|
|
|
|
|
return sets;
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-25 13:54:21 +02:00
|
|
|
public boolean identifyRepo(String url) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("Identify repository with url : " + url);
|
2019-02-16 11:04:41 +01:00
|
|
|
try {
|
|
|
|
return OaiTools.identifyRepository(url);
|
|
|
|
} catch (Exception e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("Error while identifying repository with url: " + url, e);
|
2019-02-16 11:04:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-25 13:54:21 +02:00
|
|
|
public RuleSet getRuleSet(String acronym) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("Getting ruleset with acronym : " + acronym);
|
2019-02-16 11:04:41 +01:00
|
|
|
RuleSet ruleSet = null;
|
|
|
|
try {
|
|
|
|
for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
|
|
|
|
for (RuleSet rSet : ruleSets)
|
|
|
|
if (rSet.getGuidelinesAcronym().equals(acronym)) {
|
|
|
|
ruleSet = rSet;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ruleSet;
|
|
|
|
} catch (Exception e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("Error getting ruleset", e);
|
2019-02-16 11:04:41 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-07-21 13:51:18 +02:00
|
|
|
@PreAuthorize("hasAuthority('REGISTERED_USER')")
|
2019-07-25 13:54:21 +02:00
|
|
|
public List<StoredJob> getStoredJobsNew(String user,
|
|
|
|
String jobType,
|
|
|
|
String offset,
|
|
|
|
String limit,
|
|
|
|
String dateFrom,
|
|
|
|
String dateTo,
|
|
|
|
String validationStatus ) throws ValidatorServiceException {
|
2019-11-20 14:03:40 +01:00
|
|
|
// return getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
|
|
|
|
List<StoredJob> jobs = getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
if (jobs == null) {
|
|
|
|
jobs = new ArrayList<>();
|
|
|
|
}
|
|
|
|
List<Job> cj = crisJobs.getJobs(user);
|
|
|
|
for (Job job : cj) {
|
2020-03-18 23:39:25 +01:00
|
|
|
jobs.add(CrisValidatorUtils.convertJobToStoredJob(job));
|
2019-11-20 14:03:40 +01:00
|
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
return jobs;
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws ValidatorServiceException {
|
|
|
|
return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-25 13:54:21 +02:00
|
|
|
public InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException {
|
2019-02-16 11:04:41 +01:00
|
|
|
try {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("Getting interface information with url: " + baseUrl);
|
2019-02-16 11:04:41 +01:00
|
|
|
InterfaceInformation interfaceInformation = new InterfaceInformation();
|
|
|
|
interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
|
|
|
|
if (interfaceInformation.isIdentified())
|
|
|
|
interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
|
|
|
|
|
|
|
|
return interfaceInformation;
|
|
|
|
} catch (Exception e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("Error getting interface information with url: " + baseUrl, e);
|
2019-02-16 11:04:41 +01:00
|
|
|
throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 13:07:47 +02:00
|
|
|
@Override
|
|
|
|
public List<StoredJob> getJobsSummary(String repoId, int limit) throws JSONException, ValidatorServiceException {
|
2022-08-05 10:48:16 +02:00
|
|
|
return getValidationService().getJobSummary(
|
|
|
|
repositoryService.getRepositoryInterface(repoId)
|
|
|
|
.stream()
|
|
|
|
.map(RepositoryInterface::getBaseurl)
|
|
|
|
.collect(Collectors.toList()), limit);
|
2019-10-01 13:07:47 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|