package eu.dnetlib.repo.manager.service; import eu.dnetlib.api.functionality.ValidatorServiceException; import eu.dnetlib.domain.functionality.validator.JobForValidation; import eu.dnetlib.domain.functionality.validator.RuleSet; import eu.dnetlib.domain.functionality.validator.StoredJob; import eu.dnetlib.repo.manager.shared.Constants; import eu.dnetlib.repo.manager.shared.InterfaceInformation; import eu.dnetlib.repo.manager.shared.ValidationServiceException; import eu.dnetlib.repo.manager.utils.OaiTools; import gr.uoa.di.driver.util.ServiceLocator; import io.swagger.annotations.ApiParam; import org.apache.log4j.Logger; import org.json.JSONException; 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 org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @Service("validatorService") public class ValidatorServiceImpl implements ValidatorService { @Autowired private MonitorServiceImpl monitorApi; @Resource(name = "validatorServiceLocator") private ServiceLocator validatorServiceLocator; private eu.dnetlib.api.functionality.ValidatorService getValidationService() { return this.validatorServiceLocator.getService(); } public ServiceLocator getValidatorServiceLocator() { return validatorServiceLocator; } public void setValidatorServiceLocator(ServiceLocator validatorServiceLocator) { this.validatorServiceLocator = validatorServiceLocator; } private Map> rulesetMap = new ConcurrentHashMap>(); private static final Logger LOGGER = Logger .getLogger(ValidatorServiceImpl.class); @Autowired private EmailUtils emailUtils; @PostConstruct private void loadRules(){ LOGGER.debug("PostConstruct method! Load rules!"); 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; if (rulesetMap.containsKey(key)) rulesetMap.get(key).add(ruleSet); else { List ruleSets = new ArrayList(); ruleSets.add(ruleSet); rulesetMap.put(key, ruleSets); } } } } catch (ValidatorServiceException e) { e.printStackTrace(); } } @Override @PreAuthorize("hasRole('ROLE_USER') and #jobForValidation.userEmail == authentication.userInfo.email") public JobForValidation submitJobForValidation(@RequestBody JobForValidation jobForValidation) throws ValidatorServiceException { LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId()); try { emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(),jobForValidation); this.getValidationService().submitValidationJob(jobForValidation); } catch (ValidatorServiceException e) { LOGGER.debug("Exception on submitJobForValidation" , e); emailUtils.reportException(e); throw e; } catch (Exception e) { e.printStackTrace(); } return jobForValidation; } @Override @PreAuthorize("hasRole('ROLE_USER') and #email == authentication.userInfo.email") public ResponseEntity reSubmitJobForValidation(@PathVariable("email") String email, @PathVariable("jobId") String jobId) throws JSONException, ValidatorServiceException { LOGGER.debug("Resubmit validation job with id : " + jobId); StoredJob job = monitorApi.getJobSummary(jobId,"all"); Set contentRules = new HashSet(); Set usageRules = new HashSet(); RuleSet ruleSet = null; for (List ruleSets : this.rulesetMap.values()) { for (RuleSet rSet : ruleSets) if (rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())) { ruleSet = rSet; break; } } for (int ruleId : job.getRules()) { if (ruleSet.getContentRulesIds().contains(ruleId)) contentRules.add(ruleId); else if (ruleSet.getUsageRulesIds().contains(ruleId)) usageRules.add(ruleId); } if (!contentRules.isEmpty()) job.setSelectedContentRules(contentRules); if (!usageRules.isEmpty()) job.setSelectedUsageRules(usageRules); this.submitJobForValidation(job); return new ResponseEntity<>("OK",HttpStatus.OK); } @Override public List getRuleSets(@PathVariable("mode") String mode) { LOGGER.info("Getting rulesets for mode: " + mode); return rulesetMap.get(mode); } @Override public List getSetsOfRepository(@RequestParam(value = "url", required = true) String url) { LOGGER.debug("Getting sets of repository with url : " + url); try { return OaiTools.getSetsOfRepo(url); } catch (Exception e) { LOGGER.debug("Exception on getSetsOfRepository" , e); emailUtils.reportException(e); } return null; } @Override public boolean identifyRepo(@RequestParam(value = "url", required = true) String url) { LOGGER.debug("Identify repository with url : " + url); try { return OaiTools.identifyRepository(url); } catch (Exception e) { LOGGER.error("Error while identifying repository with url: " + url, e); emailUtils.reportException(e); return false; } } @Override public RuleSet getRuleSet(@PathVariable("acronym") String acronym) { LOGGER.debug("Getting ruleset with acronym : " + acronym); RuleSet ruleSet = null; try { for (List ruleSets : this.rulesetMap.values()) { for (RuleSet rSet : ruleSets) if (rSet.getGuidelinesAcronym().equals(acronym)) { ruleSet = rSet; break; } } return ruleSet; } catch (Exception e) { LOGGER.error("Error getting ruleset", e); emailUtils.reportException(e); return null; } } @Override @PreAuthorize("hasRole('ROLE_USER')") public List getStoredJobsNew(@RequestParam("user") @ApiParam(value = "User email", required = true) String user, @RequestParam(value = "jobType", required = false) @ApiParam(value = "Equals to filter job type on validation history page") String jobType, @RequestParam("offset") @ApiParam(value = "Page number", required = true) String offset, @RequestParam(value = "limit", required = false,defaultValue = "10") @ApiParam(value = "Null value") String limit, @RequestParam(value = "dateFrom", required = false) @ApiParam(value = "Null value") String dateFrom, @RequestParam(value = "dateTo", required = false) @ApiParam(value = "Null value") String dateTo, @RequestParam("validationStatus") @ApiParam(value = "Equals to filter validation jobs", required = true) String validationStatus ) throws ValidatorServiceException { return getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus); } @Override public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws ValidatorServiceException { return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus); } @Override public InterfaceInformation getInterfaceInformation(@RequestParam(value = "baseUrl", required = true) String baseUrl) throws ValidationServiceException { try { LOGGER.debug("Getting interface information with url: " + baseUrl); InterfaceInformation interfaceInformation = new InterfaceInformation(); interfaceInformation.setIdentified(this.identifyRepo(baseUrl)); if (interfaceInformation.isIdentified()) interfaceInformation.setSets(this.getSetsOfRepository(baseUrl)); return interfaceInformation; } catch (Exception e) { LOGGER.error("Error getting interface information with url: " + baseUrl, e); emailUtils.reportException(e); throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR); } } }