Project

General

Profile

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.RepositoryInterface;
5 58301 spyroukon
import eu.dnetlib.domain.functionality.validator.*;
6 57741 ioannis.di
import eu.dnetlib.repo.manager.domain.Constants;
7
import eu.dnetlib.repo.manager.domain.InterfaceInformation;
8
import eu.dnetlib.repo.manager.domain.ValidationServiceException;
9 58301 spyroukon
import eu.dnetlib.repo.manager.utils.CrisValidatorUtils;
10 54525 panagiotis
import eu.dnetlib.repo.manager.utils.OaiTools;
11 49236 panagiotis
import gr.uoa.di.driver.util.ServiceLocator;
12 49410 panagiotis
import org.apache.log4j.Logger;
13 57648 antonis.le
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 49362 panagiotis
import org.json.JSONException;
17 49236 panagiotis
import org.springframework.beans.factory.annotation.Autowired;
18 52781 panagiotis
import org.springframework.http.HttpStatus;
19
import org.springframework.http.ResponseEntity;
20 51656 panagiotis
import org.springframework.security.access.prepost.PreAuthorize;
21 54149 panagiotis
import org.springframework.security.core.context.SecurityContextHolder;
22 54525 panagiotis
import org.springframework.stereotype.Service;
23 49236 panagiotis
24
import javax.annotation.PostConstruct;
25 49410 panagiotis
import javax.annotation.Resource;
26 54525 panagiotis
import java.util.*;
27
import java.util.concurrent.ConcurrentHashMap;
28 57217 ioannis.di
import java.util.stream.Collectors;
29 49236 panagiotis
30 49410 panagiotis
31 54525 panagiotis
@Service("validatorService")
32 54690 panagiotis
public class ValidatorServiceImpl implements ValidatorService {
33 49236 panagiotis
34
    @Autowired
35 54690 panagiotis
    private MonitorServiceImpl monitorApi;
36 49236 panagiotis
37 57217 ioannis.di
    @Autowired
38
    private RepositoryService repositoryService;
39
40 49410 panagiotis
    @Resource(name = "validatorServiceLocator")
41 54690 panagiotis
    private ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator;
42 49236 panagiotis
43 54690 panagiotis
    private eu.dnetlib.api.functionality.ValidatorService getValidationService() {
44 49236 panagiotis
        return this.validatorServiceLocator.getService();
45
    }
46
47 54690 panagiotis
    public ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> getValidatorServiceLocator() {
48 49236 panagiotis
        return validatorServiceLocator;
49
    }
50
51 54690 panagiotis
    public void setValidatorServiceLocator(ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator) {
52 49236 panagiotis
        this.validatorServiceLocator = validatorServiceLocator;
53
    }
54
55
    private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
56
57 49410 panagiotis
    private static final Logger LOGGER = Logger
58 54690 panagiotis
            .getLogger(ValidatorServiceImpl.class);
59 49410 panagiotis
60 51831 panagiotis
    @Autowired
61
    private EmailUtils emailUtils;
62
63 57648 antonis.le
    @Autowired
64
    private JobExecutor crisValidatorExecutor;
65
66
    @Autowired
67
    private MapJobDao crisJobs;
68
69 49236 panagiotis
    @PostConstruct
70
    private void loadRules(){
71 49813 panagiotis
        LOGGER.debug("PostConstruct method! Load rules!");
72 49236 panagiotis
        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 60873 antonis.le
                    else if (ruleSet.getGuidelinesAcronym().matches(".*fair$"))
83
                        key = Constants.VALIDATION_MODE_FAIR;
84 49236 panagiotis
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 57648 antonis.le
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 49236 panagiotis
        } catch (ValidatorServiceException e) {
124 56766 ioannis.di
            LOGGER.error(e);
125 49236 panagiotis
        }
126
127
    }
128
129
    @Override
130 61441 antonis.le
    @PreAuthorize("hasAuthority('REGISTERED_USER') and #jobForValidation.userEmail == authentication.userInfo.email")
131 56761 ioannis.di
    public JobForValidation submitJobForValidation(JobForValidation jobForValidation) throws ValidatorServiceException {
132 49813 panagiotis
        LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId());
133 49236 panagiotis
        try {
134 61696 antonis.le
            try {
135
                emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(), jobForValidation);
136
            } catch (Exception e) {
137
                LOGGER.error("Error sending email ", e);
138
            }
139 57648 antonis.le
            /////////////////////////////////////////////////////////////////////////////////////////
140
            // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
141
            /////////////////////////////////////////////////////////////////////////////////////////
142 57776 ioannis.di
            if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 &&
143 62093 spyroukon
                    jobForValidation.getSelectedContentRules().contains(-1000)
144
                    || jobForValidation.getDesiredCompatibilityLevel().matches("openaire-cris_1.1")) {
145 57648 antonis.le
                crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail());
146
            } else {
147
                this.getValidationService().submitValidationJob(jobForValidation);
148
            }
149
            /////////////////////////////////////////////////////////////////////////////////////////
150
            /////////////////////////////////////////////////////////////////////////////////////////
151
//            this.getValidationService().submitValidationJob(jobForValidation);
152 54149 panagiotis
        } catch (Exception e) {
153 61696 antonis.le
            throw new ValidatorServiceException(e);
154 49236 panagiotis
        }
155 61696 antonis.le
156 52781 panagiotis
        return jobForValidation;
157 49236 panagiotis
    }
158
159
    @Override
160 61441 antonis.le
    @PreAuthorize("hasAuthority('REGISTERED_USER') and #email == authentication.userInfo.email")
161 56761 ioannis.di
    public ResponseEntity<Object> reSubmitJobForValidation(String email,
162
                                                           String jobId) throws JSONException, ValidatorServiceException {
163 49813 panagiotis
        LOGGER.debug("Resubmit validation job with id : " + jobId);
164 56766 ioannis.di
        StoredJob job = monitorApi.getJobSummary(jobId, "all");
165 49236 panagiotis
        Set<Integer> contentRules = new HashSet<Integer>();
166
        Set<Integer> usageRules = new HashSet<Integer>();
167
168
        RuleSet ruleSet = null;
169
        for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
170 57968 ioannis.di
            for (RuleSet rSet : ruleSets) {
171
                if (rSet.getName().equalsIgnoreCase("CRIS Validation")) {
172 58301 spyroukon
                    crisValidatorExecutor.submit(job.getBaseUrl(), job.getUserEmail());
173
                    return new ResponseEntity<>("OK",HttpStatus.OK);
174
                } else if(rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())){
175 49236 panagiotis
                    ruleSet = rSet;
176
                    break;
177
                }
178 57968 ioannis.di
            }
179 49236 panagiotis
        }
180 56766 ioannis.di
        if (ruleSet != null){
181
            for (int ruleId : job.getRules()) {
182
                if (ruleSet.getContentRulesIds().contains(ruleId))
183
                    contentRules.add(ruleId);
184
                else if (ruleSet.getUsageRulesIds().contains(ruleId))
185
                    usageRules.add(ruleId);
186
            }
187 49236 panagiotis
        }
188
        if (!contentRules.isEmpty())
189
            job.setSelectedContentRules(contentRules);
190
        if (!usageRules.isEmpty())
191
            job.setSelectedUsageRules(usageRules);
192
        this.submitJobForValidation(job);
193 52781 panagiotis
        return new ResponseEntity<>("OK",HttpStatus.OK);
194 49236 panagiotis
    }
195
196
    @Override
197 56761 ioannis.di
    public List<RuleSet> getRuleSets(String mode) {
198 49813 panagiotis
        LOGGER.info("Getting rulesets for mode: " + mode);
199 49410 panagiotis
        return rulesetMap.get(mode);
200 49236 panagiotis
    }
201
202
    @Override
203 56761 ioannis.di
    public List<String> getSetsOfRepository(String url) {
204 49813 panagiotis
        LOGGER.debug("Getting sets of repository with url : " + url);
205 61696 antonis.le
206
        List<String> sets = null;
207
208 49236 panagiotis
        try {
209 61696 antonis.le
            sets = OaiTools.getSetsOfRepo(url);
210 49236 panagiotis
        } catch (Exception e) {
211 61696 antonis.le
            LOGGER.error("Exception on getSetsOfRepository" , e);
212 49236 panagiotis
        }
213 61696 antonis.le
214
        return sets;
215 49236 panagiotis
    }
216
217 49813 panagiotis
    @Override
218 56761 ioannis.di
    public boolean identifyRepo(String url) {
219 49813 panagiotis
        LOGGER.debug("Identify repository with url : " + url);
220
        try {
221
            return OaiTools.identifyRepository(url);
222
        } catch (Exception e) {
223
            LOGGER.error("Error while identifying repository with url: " + url, e);
224
            return false;
225
        }
226
    }
227
228
    @Override
229 56761 ioannis.di
    public RuleSet getRuleSet(String acronym) {
230 49813 panagiotis
        LOGGER.debug("Getting ruleset with acronym : " + acronym);
231
        RuleSet ruleSet = null;
232
        try {
233
            for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
234
                for (RuleSet rSet : ruleSets)
235
                    if (rSet.getGuidelinesAcronym().equals(acronym)) {
236
                        ruleSet = rSet;
237
                        break;
238
                    }
239
            }
240
            return ruleSet;
241
        } catch (Exception e) {
242
            LOGGER.error("Error getting ruleset", e);
243
            return null;
244
        }
245
    }
246
247 50304 panagiotis
    @Override
248 61441 antonis.le
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
249 56761 ioannis.di
    public List<StoredJob> getStoredJobsNew(String user,
250
                                            String jobType,
251
                                            String offset,
252
                                            String limit,
253
                                            String dateFrom,
254
                                            String dateTo,
255
                                            String validationStatus ) throws ValidatorServiceException {
256 57648 antonis.le
//        return getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
257
        List<StoredJob> jobs = getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
258
        /////////////////////////////////////////////////////////////////////////////////////////
259
        // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
260
        /////////////////////////////////////////////////////////////////////////////////////////
261
        if (jobs == null) {
262
            jobs = new ArrayList<>();
263
        }
264
        List<Job> cj = crisJobs.getJobs(user);
265
        for (Job job : cj) {
266 58301 spyroukon
            jobs.add(CrisValidatorUtils.convertJobToStoredJob(job));
267 57648 antonis.le
        }
268
        /////////////////////////////////////////////////////////////////////////////////////////
269
        /////////////////////////////////////////////////////////////////////////////////////////
270
        return jobs;
271 50304 panagiotis
    }
272
273
    @Override
274
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
275
        return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
276
    }
277
278 50570 panagiotis
    @Override
279 56761 ioannis.di
    public InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException {
280 50570 panagiotis
        try {
281
            LOGGER.debug("Getting interface information with url: " + baseUrl);
282
            InterfaceInformation interfaceInformation = new InterfaceInformation();
283
            interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
284
            if (interfaceInformation.isIdentified())
285
                interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
286 50304 panagiotis
287 50570 panagiotis
            return interfaceInformation;
288
        } catch (Exception e) {
289
            LOGGER.error("Error getting interface information with url: " + baseUrl, e);
290
            throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
291
        }
292
    }
293
294 57217 ioannis.di
    @Override
295
    public List<StoredJob> getJobsSummary(String repoId, int limit) throws JSONException, ValidatorServiceException {
296
        return getValidationService().getJobSummary(repositoryService.getRepositoryInterface(repoId).stream().map(RepositoryInterface::getBaseUrl).collect(Collectors.toList()),limit);
297
    }
298 50570 panagiotis
299 49236 panagiotis
}