Project

General

Profile

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

    
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.StoredJob;
5
import eu.dnetlib.repo.manager.service.utils.OaiTools;
6
import eu.dnetlib.repo.manager.shared.InterfaceInformation;
7
import eu.dnetlib.repo.manager.shared.ValidationServiceException;
8
import gr.uoa.di.driver.util.ServiceLocator;
9
import eu.dnetlib.domain.functionality.validator.JobForValidation;
10
import eu.dnetlib.domain.functionality.validator.RuleSet;
11
import eu.dnetlib.repo.manager.shared.Constants;
12

    
13
import java.util.*;
14
import java.util.concurrent.ConcurrentHashMap;
15
import eu.dnetlib.api.functionality.ValidatorService;
16
import org.apache.log4j.Logger;
17
import org.json.JSONException;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.stereotype.Component;
20
import org.springframework.web.bind.annotation.PathVariable;
21

    
22
import javax.annotation.PostConstruct;
23
import javax.annotation.Resource;
24

    
25

    
26
@Component
27
public class ValidatorApiImpl implements ValidatorApi{
28

    
29
    @Autowired
30
    private MonitorApiImpl monitorApi;
31

    
32
    @Resource(name = "validatorServiceLocator")
33
    private ServiceLocator<ValidatorService> validatorServiceLocator;
34

    
35
    private ValidatorService getValidationService() {
36
        return this.validatorServiceLocator.getService();
37
    }
38

    
39
    public ServiceLocator<ValidatorService> getValidatorServiceLocator() {
40
        return validatorServiceLocator;
41
    }
42

    
43
    public void setValidatorServiceLocator(ServiceLocator<ValidatorService> validatorServiceLocator) {
44
        this.validatorServiceLocator = validatorServiceLocator;
45
    }
46

    
47
    private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
48

    
49
    private static final Logger LOGGER = Logger
50
            .getLogger(ValidatorApiImpl.class);
51

    
52
    @PostConstruct
53
    private void loadRules(){
54
        LOGGER.debug("PostConstruct method! Load rules!");
55
        try {
56
            for (RuleSet ruleSet : getValidationService().getRuleSets()) {
57
                if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
58
                    String key = "";
59
                    if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
60
                        key = Constants.VALIDATION_MODE_DATA;
61
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
62
                        key = Constants.VALIDATION_MODE_LITERATURE;
63
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
64
                        key = Constants.VALIDATION_MODE_CRIS;
65

    
66
                    if (rulesetMap.containsKey(key))
67
                        rulesetMap.get(key).add(ruleSet);
68
                    else {
69
                        List<RuleSet> ruleSets = new ArrayList<RuleSet>();
70
                        ruleSets.add(ruleSet);
71
                        rulesetMap.put(key, ruleSets);
72
                    }
73
                }
74
            }
75
        } catch (ValidatorServiceException e) {
76
            e.printStackTrace();
77
        }
78

    
79
    }
80

    
81
    @Override
82
    public void submitJobForValidation(JobForValidation jobForValidation) {
83
        LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId());
84
        try {
85
            this.getValidationService().submitValidationJob(jobForValidation);
86
        } catch (ValidatorServiceException e) {
87
            e.printStackTrace();
88
        }
89
    }
90

    
91
    @Override
92
    public void reSubmitJobForValidation(String jobId) throws JSONException {
93
        LOGGER.debug("Resubmit validation job with id : " + jobId);
94
        StoredJob job = monitorApi.getJobSummary(jobId,"all");
95
        Set<Integer> contentRules = new HashSet<Integer>();
96
        Set<Integer> usageRules = new HashSet<Integer>();
97

    
98
        RuleSet ruleSet = null;
99
        for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
100
            for (RuleSet rSet : ruleSets)
101
                if (rSet.getGuidelinesAcronym().equals(job.getDesiredCompatibilityLevel())) {
102
                    ruleSet = rSet;
103
                    break;
104
                }
105
        }
106

    
107
        for (int ruleId : job.getRules()) {
108
            if (ruleSet.getContentRulesIds().contains(ruleId))
109
                contentRules.add(ruleId);
110
            else if (ruleSet.getUsageRulesIds().contains(ruleId))
111
                usageRules.add(ruleId);
112
        }
113
        if (!contentRules.isEmpty())
114
            job.setSelectedContentRules(contentRules);
115
        if (!usageRules.isEmpty())
116
            job.setSelectedUsageRules(usageRules);
117
        this.submitJobForValidation(job);
118
    }
119

    
120
    @Override
121
    public List<RuleSet> getRuleSets(@PathVariable("mode") String mode) {
122
        LOGGER.info("Getting rulesets for mode: " + mode);
123
        return rulesetMap.get(mode);
124
    }
125

    
126
    @Override
127
    public List<String> getSetsOfRepository(@PathVariable("url") String url) {
128
        LOGGER.debug("Getting sets of repository with url : " + url);
129
        try {
130
            return OaiTools.getSetsOfRepo(url);
131
        } catch (Exception e) {
132
            e.printStackTrace();
133
        }
134
        return null;
135
    }
136

    
137
    @Override
138
    public boolean identifyRepo(@PathVariable("url") String url) {
139
        LOGGER.debug("Identify repository with url : " + url);
140
        try {
141
            return OaiTools.identifyRepository(url);
142
        } catch (Exception e) {
143
            LOGGER.error("Error while identifying repository with url: " + url, e);
144
            return false;
145
        }
146
    }
147

    
148
    @Override
149
    public RuleSet getRuleSet(@PathVariable("acronym") String acronym) {
150
        LOGGER.debug("Getting ruleset with acronym : " + acronym);
151
        RuleSet ruleSet = null;
152
        try {
153
            for (List<RuleSet> ruleSets : this.rulesetMap.values()) {
154
                for (RuleSet rSet : ruleSets)
155
                    if (rSet.getGuidelinesAcronym().equals(acronym)) {
156
                        ruleSet = rSet;
157
                        break;
158
                    }
159
            }
160
            return ruleSet;
161
        } catch (Exception e) {
162
            LOGGER.error("Error getting ruleset", e);
163
            return null;
164
        }
165
    }
166

    
167
    @Override
168
    public List<StoredJob> getStoredJobsNew(String user, String jobType, Integer offset, Integer limit, String dateFrom,
169
                                            String dateTo, String validationStatus) throws ValidatorServiceException {
170
        return getValidationService().getStoredJobsNew(user, jobType, offset, limit, dateFrom, dateTo, validationStatus);
171
    }
172

    
173
    @Override
174
    public int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws  ValidatorServiceException {
175
        return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
176
    }
177

    
178
    @Override
179
    public InterfaceInformation getInterfaceInformation(@PathVariable("baseUrl") String baseUrl) throws ValidationServiceException {
180
        try {
181
            LOGGER.debug("Getting interface information with url: " + baseUrl);
182
            InterfaceInformation interfaceInformation = new InterfaceInformation();
183
            interfaceInformation.setIdentified(this.identifyRepo(baseUrl));
184
            if (interfaceInformation.isIdentified())
185
                interfaceInformation.setSets(this.getSetsOfRepository(baseUrl));
186

    
187
            return interfaceInformation;
188
        } catch (Exception e) {
189
            LOGGER.error("Error getting interface information with url: " + baseUrl, e);
190
//            emailUtils.reportException(e);
191
            throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR);
192
        }
193
    }
194

    
195

    
196
}
(12-12/12)