Project

General

Profile

1
package eu.dnetlib.repo.manager.service;
2

    
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.data.RepositoryInterface;
5
import eu.dnetlib.domain.functionality.validator.*;
6
import eu.dnetlib.repo.manager.domain.Constants;
7
import eu.dnetlib.repo.manager.domain.InterfaceInformation;
8
import eu.dnetlib.repo.manager.domain.ValidationServiceException;
9
import eu.dnetlib.repo.manager.utils.CrisValidatorUtils;
10
import eu.dnetlib.repo.manager.utils.OaiTools;
11
import gr.uoa.di.driver.util.ServiceLocator;
12
import org.apache.log4j.Logger;
13
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
import org.json.JSONException;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.http.HttpStatus;
19
import org.springframework.http.ResponseEntity;
20
import org.springframework.security.access.prepost.PreAuthorize;
21
import org.springframework.security.core.context.SecurityContextHolder;
22
import org.springframework.stereotype.Service;
23

    
24
import javax.annotation.PostConstruct;
25
import javax.annotation.Resource;
26
import java.util.*;
27
import java.util.concurrent.ConcurrentHashMap;
28
import java.util.stream.Collectors;
29

    
30

    
31
@Service("validatorService")
32
public class ValidatorServiceImpl implements ValidatorService {
33

    
34
    @Autowired
35
    private MonitorServiceImpl monitorApi;
36

    
37
    @Autowired
38
    private RepositoryService repositoryService;
39

    
40
    @Resource(name = "validatorServiceLocator")
41
    private ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator;
42

    
43
    private eu.dnetlib.api.functionality.ValidatorService getValidationService() {
44
        return this.validatorServiceLocator.getService();
45
    }
46

    
47
    public ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> getValidatorServiceLocator() {
48
        return validatorServiceLocator;
49
    }
50

    
51
    public void setValidatorServiceLocator(ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator) {
52
        this.validatorServiceLocator = validatorServiceLocator;
53
    }
54

    
55
    private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
56

    
57
    private static final Logger LOGGER = Logger
58
            .getLogger(ValidatorServiceImpl.class);
59

    
60
    @Autowired
61
    private EmailUtils emailUtils;
62

    
63
    @Autowired
64
    private JobExecutor crisValidatorExecutor;
65

    
66
    @Autowired
67
    private MapJobDao crisJobs;
68

    
69
    @PostConstruct
70
    private void loadRules(){
71
        LOGGER.debug("PostConstruct method! Load rules!");
72
        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
                    else if (ruleSet.getGuidelinesAcronym().matches(".*fair$"))
83
                        key = Constants.VALIDATION_MODE_FAIR;
84

    
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

    
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
        } catch (ValidatorServiceException e) {
124
            LOGGER.error(e);
125
        }
126

    
127
    }
128

    
129
    @Override
130
    @PreAuthorize("hasAuthority('REGISTERED_USER') and #jobForValidation.userEmail == authentication.userInfo.email")
131
    public JobForValidation submitJobForValidation(JobForValidation jobForValidation) throws ValidatorServiceException {
132
        LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId());
133
        try {
134
            try {
135
                emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(), jobForValidation);
136
            } catch (Exception e) {
137
                LOGGER.error("Error sending email ", e);
138
            }
139
            /////////////////////////////////////////////////////////////////////////////////////////
140
            // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
141
            /////////////////////////////////////////////////////////////////////////////////////////
142
            if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 &&
143
                    jobForValidation.getSelectedContentRules().contains(-1000)
144
                    || jobForValidation.getDesiredCompatibilityLevel().matches("openaire-cris_1.1")) {
145
                crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail());
146
            } else {
147
                this.getValidationService().submitValidationJob(jobForValidation);
148
            }
149
            /////////////////////////////////////////////////////////////////////////////////////////
150
            /////////////////////////////////////////////////////////////////////////////////////////
151
//            this.getValidationService().submitValidationJob(jobForValidation);
152
        } catch (Exception e) {
153
            throw new ValidatorServiceException(e);
154
        }
155

    
156
        return jobForValidation;
157
    }
158

    
159
    @Override
160
    @PreAuthorize("hasAuthority('REGISTERED_USER') and #email == authentication.userInfo.email")
161
    public ResponseEntity<Object> reSubmitJobForValidation(String email,
162
                                                           String jobId) throws JSONException, ValidatorServiceException {
163
        LOGGER.debug("Resubmit validation job with id : " + jobId);
164
        StoredJob job = monitorApi.getJobSummary(jobId, "all");
165
        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
            for (RuleSet rSet : ruleSets) {
171
                if (rSet.getName().equalsIgnoreCase("CRIS Validation")) {
172
                    crisValidatorExecutor.submit(job.getBaseUrl(), job.getUserEmail());
173
                    return new ResponseEntity<>("OK",HttpStatus.OK);
174
                } else if(rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())){
175
                    ruleSet = rSet;
176
                    break;
177
                }
178
            }
179
        }
180
        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
        }
188
        if (!contentRules.isEmpty())
189
            job.setSelectedContentRules(contentRules);
190
        if (!usageRules.isEmpty())
191
            job.setSelectedUsageRules(usageRules);
192
        this.submitJobForValidation(job);
193
        return new ResponseEntity<>("OK",HttpStatus.OK);
194
    }
195

    
196
    @Override
197
    public List<RuleSet> getRuleSets(String mode) {
198
        LOGGER.info("Getting rulesets for mode: " + mode);
199
        return rulesetMap.get(mode);
200
    }
201

    
202
    @Override
203
    public List<String> getSetsOfRepository(String url) {
204
        LOGGER.debug("Getting sets of repository with url : " + url);
205

    
206
        List<String> sets = null;
207

    
208
        try {
209
            sets = OaiTools.getSetsOfRepo(url);
210
        } catch (Exception e) {
211
            LOGGER.error("Exception on getSetsOfRepository" , e);
212
        }
213

    
214
        return sets;
215
    }
216

    
217
    @Override
218
    public boolean identifyRepo(String url) {
219
        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
    public RuleSet getRuleSet(String acronym) {
230
        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
    @Override
248
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
249
    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
//        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
            jobs.add(CrisValidatorUtils.convertJobToStoredJob(job));
267
        }
268
        /////////////////////////////////////////////////////////////////////////////////////////
269
        /////////////////////////////////////////////////////////////////////////////////////////
270
        return jobs;
271
    }
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
    @Override
279
    public InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException {
280
        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

    
287
            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
    @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

    
299
}
(20-20/20)