Project

General

Profile

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

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

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

    
31

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

    
35
    @Autowired
36
    private MonitorServiceImpl monitorApi;
37

    
38
    @Autowired
39
    private RepositoryService repositoryService;
40

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

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

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

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

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

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

    
61
    @Autowired
62
    private EmailUtils emailUtils;
63

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

    
78
                    if (rulesetMap.containsKey(key))
79
                        rulesetMap.get(key).add(ruleSet);
80
                    else {
81
                        List<RuleSet> ruleSets = new ArrayList<RuleSet>();
82
                        ruleSets.add(ruleSet);
83
                        rulesetMap.put(key, ruleSets);
84
                    }
85
                }
86
            }
87
        } catch (ValidatorServiceException e) {
88
            LOGGER.error(e);
89
        }
90

    
91
    }
92

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

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

    
119
        RuleSet ruleSet = null;
120
        for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
121
            for (RuleSet rSet : ruleSets)
122
                if (rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())) {
123
                    ruleSet = rSet;
124
                    break;
125
                }
126
        }
127
        if (ruleSet != null){
128
            for (int ruleId : job.getRules()) {
129
                if (ruleSet.getContentRulesIds().contains(ruleId))
130
                    contentRules.add(ruleId);
131
                else if (ruleSet.getUsageRulesIds().contains(ruleId))
132
                    usageRules.add(ruleId);
133
            }
134
        }
135
        if (!contentRules.isEmpty())
136
            job.setSelectedContentRules(contentRules);
137
        if (!usageRules.isEmpty())
138
            job.setSelectedUsageRules(usageRules);
139
        this.submitJobForValidation(job);
140
        return new ResponseEntity<>("OK",HttpStatus.OK);
141
    }
142

    
143
    @Override
144
    public List<RuleSet> getRuleSets(String mode) {
145
        LOGGER.info("Getting rulesets for mode: " + mode);
146
        return rulesetMap.get(mode);
147
    }
148

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

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

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

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

    
205
    @Override
206
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
207
        return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
208
    }
209

    
210
    @Override
211
    public InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException {
212
        try {
213
            LOGGER.debug("Getting interface information with url: " + baseUrl);
214
            InterfaceInformation interfaceInformation = new InterfaceInformation();
215
            interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
216
            if (interfaceInformation.isIdentified())
217
                interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
218

    
219
            return interfaceInformation;
220
        } catch (Exception e) {
221
            LOGGER.error("Error getting interface information with url: " + baseUrl, e);
222
            emailUtils.reportException(e);
223
            throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
224
        }
225
    }
226

    
227
    @Override
228
    public List<StoredJob> getJobsSummary(String repoId, int limit) throws JSONException, ValidatorServiceException {
229
        return getValidationService().getJobSummary(repositoryService.getRepositoryInterface(repoId).stream().map(RepositoryInterface::getBaseUrl).collect(Collectors.toList()),limit);
230
    }
231

    
232

    
233
}
(20-20/20)