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.Qualifier;
18
import org.springframework.beans.factory.annotation.Value;
19
import org.springframework.stereotype.Component;
20
import org.springframework.web.bind.annotation.PathVariable;
21

    
22
import javax.annotation.PostConstruct;
23

    
24
public class ValidatorApiImpl implements ValidatorApi{
25

    
26
    @Autowired
27
    private MonitorApiImpl monitorApi;
28

    
29
    /*@Autowired
30
    @Qualifier("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
    @PostConstruct
48
    private void loadRules(){
49

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

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

    
74
    }
75

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

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

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

    
92
        StoredJob job = monitorApi.getJobSummary(jsonObject.toString());
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() {
120
        return rulesetMap.get(Constants.VALIDATION_MODE_DATA);
121
    }
122

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

    
133
}
(11-11/11)