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.service.utils.Constants;
10

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

    
21
import javax.annotation.PostConstruct;
22

    
23
@Component(value = "validationService")
24
public class ValidatorApiImpl implements ValidatorApi{
25

    
26
    @Autowired
27
    private MonitorApiImpl monitorApi;
28

    
29
    @Autowired
30
    private ServiceLocator<ValidatorService> validatorServiceLocator;
31

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

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

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

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

    
46
    @PostConstruct
47
    private void loadRules(){
48

    
49
        try {
50
            for (RuleSet ruleSet : getValidationService().getRuleSets()) {
51
                if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
52
                    String key = "";
53
                    if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
54
                        key = Constants.VALIDATION_MODE_DATA;
55
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
56
                        key = Constants.VALIDATION_MODE_LITERATURE;
57
                    else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
58
                        key = Constants.VALIDATION_MODE_CRIS;
59

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

    
73
    }
74

    
75
    @Override
76
    public void submitJobForValidation(JobForValidation jobForValidation) {
77
        try {
78
            this.getValidationService().submitValidationJob(jobForValidation);
79
        } catch (ValidatorServiceException e) {
80
            e.printStackTrace();
81
        }
82
    }
83

    
84
    @Override
85
    public void reSubmitJobForValidation(String jobId) throws JSONException {
86

    
87
        JSONObject jsonObject = new JSONObject();
88
        jsonObject.put("jobId",jobId);
89
        jsonObject.put("groupBy","all");
90

    
91
        StoredJob job = monitorApi.getJobSummary(jsonObject.toString());
92
        Set<Integer> contentRules = new HashSet<Integer>();
93
        Set<Integer> usageRules = new HashSet<Integer>();
94

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

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

    
117
    @Override
118
    public List<RuleSet> getRuleSets() {
119
        return rulesetMap.get(Constants.VALIDATION_MODE_DATA);
120
    }
121

    
122
    @Override
123
    public List<String> getSetsOfRepository(@PathVariable("url") String url) {
124
        try {
125
            return OaiTools.getSetsOfRepo(url);
126
        } catch (Exception e) {
127
            e.printStackTrace();
128
        }
129
        return null;
130
    }
131

    
132
}
(11-11/11)