Project

General

Profile

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

    
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.JobForValidation;
5
import eu.dnetlib.domain.functionality.validator.RuleSet;
6
import eu.dnetlib.domain.functionality.validator.StoredJob;
7
import eu.dnetlib.repo.manager.shared.Constants;
8
import eu.dnetlib.repo.manager.shared.InterfaceInformation;
9
import eu.dnetlib.repo.manager.shared.ValidationServiceException;
10
import eu.dnetlib.repo.manager.utils.OaiTools;
11
import gr.uoa.di.driver.util.ServiceLocator;
12
import io.swagger.annotations.ApiParam;
13
import org.apache.log4j.Logger;
14
import org.json.JSONException;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.http.HttpStatus;
17
import org.springframework.http.ResponseEntity;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.security.core.context.SecurityContextHolder;
20
import org.springframework.stereotype.Service;
21
import org.springframework.web.bind.annotation.PathVariable;
22
import org.springframework.web.bind.annotation.RequestBody;
23
import org.springframework.web.bind.annotation.RequestParam;
24

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

    
30

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

    
34
    @Autowired
35
    private MonitorServiceImpl monitorApi;
36

    
37
    @Resource(name = "validatorServiceLocator")
38
    private ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator;
39

    
40
    private eu.dnetlib.api.functionality.ValidatorService getValidationService() {
41
        return this.validatorServiceLocator.getService();
42
    }
43

    
44
    public ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> getValidatorServiceLocator() {
45
        return validatorServiceLocator;
46
    }
47

    
48
    public void setValidatorServiceLocator(ServiceLocator<eu.dnetlib.api.functionality.ValidatorService> validatorServiceLocator) {
49
        this.validatorServiceLocator = validatorServiceLocator;
50
    }
51

    
52
    private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
53

    
54
    private static final Logger LOGGER = Logger
55
            .getLogger(ValidatorServiceImpl.class);
56

    
57
    @Autowired
58
    private EmailUtils emailUtils;
59

    
60
    @PostConstruct
61
    private void loadRules(){
62
        LOGGER.debug("PostConstruct method! Load rules!");
63
        try {
64
            for (RuleSet ruleSet : getValidationService().getRuleSets()) {
65
                if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
66
                    String key = "";
67
                    if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
68
                        key = Constants.VALIDATION_MODE_DATA;
69
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
70
                        key = Constants.VALIDATION_MODE_LITERATURE;
71
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
72
                        key = Constants.VALIDATION_MODE_CRIS;
73

    
74
                    if (rulesetMap.containsKey(key))
75
                        rulesetMap.get(key).add(ruleSet);
76
                    else {
77
                        List<RuleSet> ruleSets = new ArrayList<RuleSet>();
78
                        ruleSets.add(ruleSet);
79
                        rulesetMap.put(key, ruleSets);
80
                    }
81
                }
82
            }
83
        } catch (ValidatorServiceException e) {
84
            e.printStackTrace();
85
        }
86

    
87
    }
88

    
89
    @Override
90
    @PreAuthorize("hasRole('ROLE_USER') and #jobForValidation.userEmail == authentication.userInfo.email")
91
    public JobForValidation submitJobForValidation(JobForValidation jobForValidation) throws ValidatorServiceException {
92
        LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId());
93
        try {
94
            emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(),jobForValidation);
95
            this.getValidationService().submitValidationJob(jobForValidation);
96
        } catch (ValidatorServiceException e) {
97
            LOGGER.debug("Exception on submitJobForValidation" , e);
98
            emailUtils.reportException(e);
99
            throw e;
100
        } catch (Exception e) {
101
            e.printStackTrace();
102
        }
103
        return jobForValidation;
104
    }
105

    
106
    @Override
107
    @PreAuthorize("hasRole('ROLE_USER') and #email == authentication.userInfo.email")
108
    public ResponseEntity<Object> reSubmitJobForValidation(String email,
109
                                                           String jobId) throws JSONException, ValidatorServiceException {
110
        LOGGER.debug("Resubmit validation job with id : " + jobId);
111
        StoredJob job = monitorApi.getJobSummary(jobId,"all");
112
        Set<Integer> contentRules = new HashSet<Integer>();
113
        Set<Integer> usageRules = new HashSet<Integer>();
114

    
115
        RuleSet ruleSet = null;
116
        for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
117
            for (RuleSet rSet : ruleSets)
118
                if (rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())) {
119
                    ruleSet = rSet;
120
                    break;
121
                }
122
        }
123

    
124
        for (int ruleId : job.getRules()) {
125
            if (ruleSet.getContentRulesIds().contains(ruleId))
126
                contentRules.add(ruleId);
127
            else if (ruleSet.getUsageRulesIds().contains(ruleId))
128
                usageRules.add(ruleId);
129
        }
130
        if (!contentRules.isEmpty())
131
            job.setSelectedContentRules(contentRules);
132
        if (!usageRules.isEmpty())
133
            job.setSelectedUsageRules(usageRules);
134
        this.submitJobForValidation(job);
135
        return new ResponseEntity<>("OK",HttpStatus.OK);
136
    }
137

    
138
    @Override
139
    public List<RuleSet> getRuleSets(String mode) {
140
        LOGGER.info("Getting rulesets for mode: " + mode);
141
        return rulesetMap.get(mode);
142
    }
143

    
144
    @Override
145
    public List<String> getSetsOfRepository(String url) {
146
        LOGGER.debug("Getting sets of repository with url : " + url);
147
        try {
148
            return OaiTools.getSetsOfRepo(url);
149
        } catch (Exception e) {
150
            LOGGER.debug("Exception on getSetsOfRepository" , e);
151
            emailUtils.reportException(e);
152
        }
153
        return null;
154
    }
155

    
156
    @Override
157
    public boolean identifyRepo(String url) {
158
        LOGGER.debug("Identify repository with url : " + url);
159
        try {
160
            return OaiTools.identifyRepository(url);
161
        } catch (Exception e) {
162
            LOGGER.error("Error while identifying repository with url: " + url, e);
163
            emailUtils.reportException(e);
164
            return false;
165
        }
166
    }
167

    
168
    @Override
169
    public RuleSet getRuleSet(String acronym) {
170
        LOGGER.debug("Getting ruleset with acronym : " + acronym);
171
        RuleSet ruleSet = null;
172
        try {
173
            for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
174
                for (RuleSet rSet : ruleSets)
175
                    if (rSet.getGuidelinesAcronym().equals(acronym)) {
176
                        ruleSet = rSet;
177
                        break;
178
                    }
179
            }
180
            return ruleSet;
181
        } catch (Exception e) {
182
            LOGGER.error("Error getting ruleset", e);
183
            emailUtils.reportException(e);
184
            return null;
185
        }
186
    }
187

    
188
    @Override
189
    @PreAuthorize("hasRole('ROLE_USER')")
190
    public List<StoredJob> getStoredJobsNew(String user,
191
                                            String jobType,
192
                                            String offset,
193
                                            String limit,
194
                                            String dateFrom,
195
                                            String dateTo,
196
                                            String validationStatus ) throws ValidatorServiceException {
197
        return getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus);
198
    }
199

    
200
    @Override
201
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
202
        return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
203
    }
204

    
205
    @Override
206
    public InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException {
207
        try {
208
            LOGGER.debug("Getting interface information with url: " + baseUrl);
209
            InterfaceInformation interfaceInformation = new InterfaceInformation();
210
            interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
211
            if (interfaceInformation.isIdentified())
212
                interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
213

    
214
            return interfaceInformation;
215
        } catch (Exception e) {
216
            LOGGER.error("Error getting interface information with url: " + baseUrl, e);
217
            emailUtils.reportException(e);
218
            throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
219
        }
220
    }
221

    
222

    
223
}
(20-20/20)