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 gr.uoa.di.driver.util.ServiceLocator;
7
import eu.dnetlib.domain.functionality.validator.JobForValidation;
8
import eu.dnetlib.domain.functionality.validator.RuleSet;
9
import eu.dnetlib.repo.manager.shared.Constants;
10

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

    
20
import javax.annotation.PostConstruct;
21
import javax.annotation.Resource;
22

    
23

    
24
@Component
25
public class ValidatorApiImpl implements ValidatorApi{
26

    
27
    @Autowired
28
    private MonitorApiImpl monitorApi;
29

    
30
    @Resource(name = "validatorServiceLocator")
31
    private ServiceLocator<ValidatorService> validatorServiceLocator;
32

    
33
    private ValidatorService getValidationService() {
34
        return this.validatorServiceLocator.getService();
35
    }
36

    
37
    public ServiceLocator<ValidatorService> getValidatorServiceLocator() {
38
        return validatorServiceLocator;
39
    }
40

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

    
45
    private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
46

    
47
    private static final Logger LOGGER = Logger
48
            .getLogger(ValidatorApiImpl.class);
49

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

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

    
77
    }
78

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

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

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

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

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

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

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

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

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

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

    
176

    
177
}
(10-10/10)