Project

General

Profile

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

    
3
import eu.dnetlib.domain.data.Repository;
4
import eu.dnetlib.repo.manager.client.RepositoryService;
5
import eu.dnetlib.repo.manager.server.utils.EmailUtils;
6
import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
7
import eu.dnetlib.repo.manager.shared.Tuple;
8
import eu.dnetlib.repos.RepoApi;
9
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
10
import org.apache.commons.lang.WordUtils;
11
import org.apache.log4j.Logger;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.stereotype.Service;
15

    
16
import java.util.*;
17

    
18
/**
19
 * Created by nikonas on 12/8/15.
20
 */
21
@SuppressWarnings("serial")
22
@Service("repositoryService")
23
public class RepositoryServiceImpl extends SpringGwtRemoteServiceServlet implements RepositoryService {
24

    
25
    private static final Logger LOGGER = Logger
26
            .getLogger(RepositoryServiceImpl.class);
27

    
28
    @Autowired
29
    private RepoApi repoAPI;
30

    
31
    @Autowired
32
    private EmailUtils emailUtils;
33

    
34
    @Autowired
35
    private VocabularyLoader vocabularyLoader;
36

    
37
    @Value("${services.repo-manager.repository.testing.mode}")
38
    private boolean testingMode;
39

    
40
    @Override
41
    public Tuple<List<Repository>, List<Repository>> getRepositoriesByCountry(String country, String mode, boolean includeUnknownCountries) throws RepositoryServiceException {
42
        try {
43
            if (testingMode)
44
                return this.getRepositoriesByCountryTesting(country, mode, includeUnknownCountries);
45
            LOGGER.debug("Getting repositories of country: " + country + " with type: " + mode + " and includeUnknownCountries: " + includeUnknownCountries);
46

    
47
            Tuple<List<Repository>, List<Repository>> retTuple = new Tuple<List<Repository>, List<Repository>>();
48
            List<Repository> reposList = this.repoAPI.getRepositoriesPerCountry(mode).get(country);
49
            if (reposList == null) {
50
                retTuple.setFirst(new ArrayList<Repository>());
51
                if (!includeUnknownCountries) {
52
                    throw new RepositoryServiceException("registration.noReposForThisCountry", RepositoryServiceException.ErrorCode.NO_REPOS_FOR_THIS_COUNTRY);
53
                }
54
            } else {
55
                retTuple.setFirst(reposList);
56
            }
57

    
58
            if(includeUnknownCountries) {
59
                List<Repository> withoutCountryList = this.repoAPI.getRepositoriesPerCountry(mode).get("Without Country");
60
                List<Repository> unknownCountryList = this.repoAPI.getRepositoriesPerCountry(mode).get("UNKNOWN");
61
                List<Repository> totalList = new ArrayList<Repository>();
62
                if (withoutCountryList != null)
63
                    totalList.addAll(withoutCountryList);
64
                if (unknownCountryList != null)
65
                    totalList.addAll(unknownCountryList);
66
                retTuple.setSecond(totalList);
67
            }
68

    
69
            return retTuple;
70

    
71
        } catch (Exception e) {
72
            LOGGER.error("Error while getting repositories of country: " + country + " with type: " + mode+ " and includeUnknownCountries: " + includeUnknownCountries, e);
73
            if (e instanceof RepositoryServiceException) {
74
                throw (RepositoryServiceException) e;
75
            } else {
76
                emailUtils.reportException(e);
77
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
78
            }
79
        }
80
    }
81

    
82
    @Override
83
    public Repository getRepository(String repoId, String officialName) throws RepositoryServiceException {
84
        try {
85
            LOGGER.debug("Getting repository with id: " + repoId + " and name: " + officialName);
86

    
87
            Repository repo = this.repoAPI.getRepository(officialName, repoId);
88
            if (repo == null) {
89
                throw new RepositoryServiceException("registration.repositoryNotExists", RepositoryServiceException.ErrorCode.REPOSITORY_NOT_EXISTS);
90
            }
91
            return repo;
92

    
93
        } catch (Exception e) {
94
            LOGGER.error("Error while getting repository with id: " + repoId + " and name: " + officialName, e);
95
            if (e instanceof RepositoryServiceException) {
96
                throw (RepositoryServiceException) e;
97
            } else {
98
                emailUtils.reportException(e);
99
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
100
            }
101
        }
102
    }
103

    
104
    @Override
105
    public Map<String, String> getCountries(Boolean existingOnly, String mode) throws RepositoryServiceException {
106
        try {
107
            LOGGER.debug("Getting countries");
108
            List<String> countries = new ArrayList<String>();
109

    
110
            Map<String, String> countriesMap = new TreeMap<String, String>();
111

    
112
            if (existingOnly) {
113
                LOGGER.debug("using the repositories map");
114
                countries.addAll(this.repoAPI.getRepositoriesByCountry(mode).keySet());
115
            } else {
116
                LOGGER.debug("using \"dnet:countries\" vocabulary");
117
                countries.addAll(vocabularyLoader.getVocabulary("dnet:countries", Locale.ENGLISH, Locale.ROOT).getEnglishNames());
118
            }
119

    
120
            for (String country : countries) {
121
                countriesMap.put(country, WordUtils.capitalizeFully(country));
122
            }
123

    
124
            return countriesMap;
125

    
126
        } catch (Exception e) {
127
            LOGGER.error("Error while getting getting countries", e);
128
            if (e instanceof RepositoryServiceException) {
129
                throw (RepositoryServiceException) e;
130
            } else {
131
                emailUtils.reportException(e);
132
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
133
            }
134
        }
135
    }
136

    
137
    @Override
138
    public Map<String, String> getCountries() throws RepositoryServiceException {
139
        return this.getCountries(false,null);
140
    }
141

    
142
    @Override
143
    public void storeRepository(Repository repo, String mode) throws RepositoryServiceException {
144

    
145

    
146
     /*   try {
147
            LOGGER.debug("Getting repository with id: " + repoId + " and name: " + officialName);
148

    
149
            Repository repo = this.repoAPI.getRepository(officialName, repoId);
150
            if (repo == null) {
151
                throw new RepositoryServiceException("registration.repositoryNotExists", RepositoryServiceException.ErrorCode.REPOSITORY_NOT_EXISTS);
152
            }
153
            return repo;
154

    
155
        } catch (Exception e) {
156
            LOGGER.error("Error while getting repository with id: " + repoId + " and name: " + officialName);
157
            if (e instanceof RepositoryServiceException) {
158
                throw (RepositoryServiceException) e;
159
            } else {
160
                emailUtils.reportException(e);
161
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
162
            }
163
        }*/
164
    }
165

    
166

    
167
    private Tuple<List<Repository>, List<Repository>> getRepositoriesByCountryTesting(String country, String mode, boolean includeUnknownCountries) throws RepositoryServiceException {
168
        try {
169
            LOGGER.debug("Getting testing repositories of country: " + country + " with type: " + mode + " and includeUnknownCountries: " + includeUnknownCountries);
170

    
171
            Tuple<List<Repository>, List<Repository>> retTuple = new Tuple<List<Repository>, List<Repository>>();
172
            List<Repository> reposList = new ArrayList<Repository>();
173
            reposList.add(this.repoAPI.getRepository("opendoar____::1356"));
174
            reposList.add(this.repoAPI.getRepository("opendoar____::2678"));
175
            reposList.add(this.repoAPI.getRepository("opendoar____::2980"));
176
            reposList.add(this.repoAPI.getRepository("opendoar____::1347"));
177

    
178
            retTuple.setFirst(reposList);
179

    
180
            if(includeUnknownCountries) {
181
                List<Repository> totalList = new ArrayList<Repository>();
182
                totalList.add(this.repoAPI.getRepository("opendoar____::3000"));
183
                totalList.add(this.repoAPI.getRepository("opendoar____::1027"));
184
                totalList.add(this.repoAPI.getRepository("opendoar____::1096"));
185

    
186
                retTuple.setSecond(totalList);
187
            }
188

    
189
            return retTuple;
190

    
191
        } catch (Exception e) {
192
            LOGGER.error("Error while getting testing repositories of country: " + country + " with type: " + mode+ " and includeUnknownCountries: " + includeUnknownCountries, e);
193
            if (e instanceof RepositoryServiceException) {
194
                throw (RepositoryServiceException) e;
195
            } else {
196
                emailUtils.reportException(e);
197
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
198
            }
199
        }
200
    }
201

    
202
    /*    @Override
203
    public List<Repository> getRepositoriesByCountry(String country, String mode) throws RepositoryServiceException {
204
        try {
205
            LOGGER.debug("Getting repositories of country: " + country + " with type: " + mode);
206

    
207
            List<Repository> reposList = this.repoAPI.getRepositoriesPerCountry(mode).get(country);
208
            if (reposList == null) {
209
                throw new RepositoryServiceException("registration.noReposForThisCountry", RepositoryServiceException.ErrorCode.NO_REPOS_FOR_THIS_COUNTRY);
210
            }
211
            return reposList;
212

    
213
        } catch (Exception e) {
214
            LOGGER.error("Error while getting repositories of country: " + country + " with type: " + mode, e);
215
            if (e instanceof RepositoryServiceException) {
216
                throw (RepositoryServiceException) e;
217
            } else {
218
                emailUtils.reportException(e);
219
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
220
            }
221
        }
222
    }*/
223
}
(2-2/4)