Project

General

Profile

1 41049 nikon.gasp
package eu.dnetlib.repo.manager.server.services;
2 40260 stefania.m
3 49974 panagiotis
import com.fasterxml.jackson.databind.DeserializationConfig;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
6 40575 nikon.gasp
import com.unboundid.util.Base64;
7 40336 nikon.gasp
import eu.dnetlib.domain.data.Repository;
8 40706 nikon.gasp
import eu.dnetlib.domain.data.RepositoryInterface;
9 40575 nikon.gasp
import eu.dnetlib.domain.enabling.Vocabulary;
10 49691 antonis.le
import eu.dnetlib.domain.functionality.UserProfile;
11 41617 stefania.m
import eu.dnetlib.gwt.server.service.SpringGwtRemoteServiceServlet;
12 41093 stefania.m
import eu.dnetlib.repo.manager.client.services.RepositoryService;
13 41336 nikon.gasp
import eu.dnetlib.repo.manager.server.utils.EmailUtils;
14 40600 nikon.gasp
import eu.dnetlib.repo.manager.server.utils.LocalVocabularies;
15 49909 panagiotis
import eu.dnetlib.domain.data.PiwikInfo;
16 49751 panagiotis
import eu.dnetlib.repo.manager.service.controllers.RepositoryApi;
17 40611 nikon.gasp
import eu.dnetlib.repo.manager.shared.*;
18 41336 nikon.gasp
import eu.dnetlib.repos.RepoApi;
19 40516 nikon.gasp
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
20 45462 stefania.m
import org.apache.commons.lang.StringEscapeUtils;
21 40543 nikon.gasp
import org.apache.commons.lang.WordUtils;
22 40336 nikon.gasp
import org.apache.log4j.Logger;
23 49751 panagiotis
import org.json.JSONException;
24 49964 panagiotis
import org.json.JSONObject;
25 40336 nikon.gasp
import org.springframework.beans.factory.annotation.Autowired;
26 40548 nikon.gasp
import org.springframework.beans.factory.annotation.Value;
27 45462 stefania.m
import org.springframework.dao.EmptyResultDataAccessException;
28 40861 nikon.gasp
import org.springframework.scheduling.annotation.Scheduled;
29 40464 nikon.gasp
import org.springframework.stereotype.Service;
30 40260 stefania.m
31 40861 nikon.gasp
import javax.annotation.PostConstruct;
32 40575 nikon.gasp
import java.text.Normalizer;
33 40543 nikon.gasp
import java.util.*;
34 40861 nikon.gasp
import java.util.concurrent.ConcurrentHashMap;
35 40336 nikon.gasp
36 40260 stefania.m
/**
37 40464 nikon.gasp
 * Created by nikonas on 12/8/15.
38 40260 stefania.m
 */
39
@SuppressWarnings("serial")
40 40464 nikon.gasp
@Service("repositoryService")
41
public class RepositoryServiceImpl extends SpringGwtRemoteServiceServlet implements RepositoryService {
42 40336 nikon.gasp
43
    private static final Logger LOGGER = Logger
44
            .getLogger(RepositoryServiceImpl.class);
45
46
    @Autowired
47 41336 nikon.gasp
    private RepoApi repoAPI;
48 40336 nikon.gasp
49 40516 nikon.gasp
    @Autowired
50
    private EmailUtils emailUtils;
51 40336 nikon.gasp
52 41336 nikon.gasp
    private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"};
53 40861 nikon.gasp
54 40516 nikon.gasp
    @Autowired
55
    private VocabularyLoader vocabularyLoader;
56
57 40861 nikon.gasp
    private Map<String, Vocabulary> vocabularyMap = new ConcurrentHashMap<String, Vocabulary>();
58
59 40548 nikon.gasp
    @Value("${services.repo-manager.repository.testing.mode}")
60
    private boolean testingMode;
61
62 45462 stefania.m
    @Autowired
63
    private PiwikDAO piwikDAO;
64
65 46279 stefania.m
    @Value("${services.repomanager.analyticsURL}")
66
    private String analyticsURL;
67
68 45462 stefania.m
    private static final String PIWIK_SCRIPT = StringEscapeUtils.escapeHtml("<!-- Piwik -->\n" +
69
            "<script type=\"text/javascript\">\n" +
70
            "\tvar _paq = _paq || [];\n" +
71
            "\t_paq.push(['enableLinkTracking']);\n" +
72
            "\t(function() {\n" +
73
            "\t\tvar u=\"//analytics.openaire.eu/\";\n" +
74
            "\t\t_paq.push(['setTrackerUrl', u+'piwik.php']);\n" +
75
            "\t\t_paq.push(['setSiteId', $$$]);\n" +
76
            "\t\t<% if(handle != null){%>\n" +
77
            "\t\t\t_paq.push(['setCustomVariable', 1, 'oaipmhID',\"oai:<%= baseUrl %>:<%=handle %>\", 'page']);\n" +
78
            "\t\t\t_paq.push(['trackPageView']);\n" +
79
            "\t\t<}>\n" +
80
            "\t\tvar d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];\n" +
81
            "\t\tg.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);\n" +
82
            "\t})();\n" +
83
            "</script>\n" +
84
            "<noscript>\n" +
85
            "\t<p>\n" +
86
            "\t\t<img src=\"//analytics.openaire.eu/piwik.php?idsite=47\" style=\"border:0;\" alt=\"\" />\n" +
87
            "\t</p>\n" +
88
            "</noscript>\n" +
89
            "<!— End Piwik Code —>");
90
91 49751 panagiotis
92
    @Autowired
93
    private RepositoryApi repositoryApi;
94
95
96
97 40861 nikon.gasp
    @PostConstruct
98
    public void init() {
99
        this.loadVocabularies();
100
    }
101
102 40336 nikon.gasp
    @Override
103 40546 nikon.gasp
    public Tuple<List<Repository>, List<Repository>> getRepositoriesByCountry(String country, String mode, boolean includeUnknownCountries) throws RepositoryServiceException {
104 40336 nikon.gasp
        try {
105 40548 nikon.gasp
            if (testingMode)
106
                return this.getRepositoriesByCountryTesting(country, mode, includeUnknownCountries);
107 40546 nikon.gasp
            LOGGER.debug("Getting repositories of country: " + country + " with type: " + mode + " and includeUnknownCountries: " + includeUnknownCountries);
108 40336 nikon.gasp
109 40546 nikon.gasp
            Tuple<List<Repository>, List<Repository>> retTuple = new Tuple<List<Repository>, List<Repository>>();
110 49812 panagiotis
//            List<Repository> reposList = this.repoAPI.getRepositoriesPerCountry(mode).get(country);
111
112 49791 panagiotis
            List<Repository> reposList = repositoryApi.getRepositoriesByCountry(country,mode);
113 40336 nikon.gasp
            if (reposList == null) {
114 40546 nikon.gasp
                retTuple.setFirst(new ArrayList<Repository>());
115 40775 stefania.m
//                if (!includeUnknownCountries) {
116
//                    throw new RepositoryServiceException("registration.noReposForThisCountry", RepositoryServiceException.ErrorCode.NO_REPOS_FOR_THIS_COUNTRY);
117
//                }
118 40546 nikon.gasp
            } else {
119
                retTuple.setFirst(reposList);
120 40336 nikon.gasp
            }
121
122 41049 nikon.gasp
            if (includeUnknownCountries) {
123 40546 nikon.gasp
                List<Repository> withoutCountryList = this.repoAPI.getRepositoriesPerCountry(mode).get("Without Country");
124
                List<Repository> unknownCountryList = this.repoAPI.getRepositoriesPerCountry(mode).get("UNKNOWN");
125
                List<Repository> totalList = new ArrayList<Repository>();
126
                if (withoutCountryList != null)
127
                    totalList.addAll(withoutCountryList);
128
                if (unknownCountryList != null)
129
                    totalList.addAll(unknownCountryList);
130
                retTuple.setSecond(totalList);
131
            }
132
133
            return retTuple;
134
135 40336 nikon.gasp
        } catch (Exception e) {
136 41049 nikon.gasp
            LOGGER.error("Error while getting repositories of country: " + country + " with type: " + mode + " and includeUnknownCountries: " + includeUnknownCountries, e);
137 40336 nikon.gasp
            if (e instanceof RepositoryServiceException) {
138
                throw (RepositoryServiceException) e;
139
            } else {
140 40543 nikon.gasp
                emailUtils.reportException(e);
141 40336 nikon.gasp
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
142
            }
143
        }
144
    }
145
146
    @Override
147 40649 nikon.gasp
    public List<Repository> getRepositoriesByCountry(String country, String mode) throws RepositoryServiceException {
148
        return this.getRepositoriesByCountry(country, mode, false).getFirst();
149
    }
150
151
    @Override
152 40801 nikon.gasp
    public DatasourcesCollection getRepositoriesOfUser(String userEmail, boolean includeShared, boolean includeByOthers) throws RepositoryServiceException {
153 40336 nikon.gasp
154 49751 panagiotis
        DatasourcesCollection retDatasources = new DatasourcesCollection();
155
        try {
156
            LOGGER.debug("Getting repositories of user: " + userEmail + " . IncludeShared: "
157
                    + includeShared + " . IncludeByOthers: " + includeByOthers);
158 49909 panagiotis
            int page = 0;
159
            String size = "50";
160 49751 panagiotis
            List<Repository> resultSet = repositoryApi.getRepositoriesOfUser(userEmail,String.valueOf(page),size);
161
            while(resultSet.size() > 0 ){
162
                retDatasources.getDatasourcesOfUser().addAll(resultSet);
163
                page++;
164
                resultSet = repositoryApi.getRepositoriesOfUser(userEmail,String.valueOf(page),size);
165
            }
166
        } catch (JSONException e) {
167
            LOGGER.error("Error while getting repositories of user: " + userEmail + " . IncludeShared: " + includeShared + " . IncludeByOthers: " + includeByOthers, e);
168 40801 nikon.gasp
        }
169 49751 panagiotis
        return retDatasources;
170 40801 nikon.gasp
    }
171
172
    @Override
173 41136 nikon.gasp
    public List<String> getRepositoryUrlsOfUser(String userEmail, boolean includeShared, boolean includeByOthers) throws RepositoryServiceException {
174
        try {
175
            LOGGER.debug("Getting repositories(urls) of user: " + userEmail + " . IncludeShared: " + includeShared + " . IncludeByOthers: " + includeByOthers);
176
            List<String> retRepos = new ArrayList<String>();
177
178 49909 panagiotis
            int page = 0;
179
            String size = "50";
180 49751 panagiotis
            List<String> resultSet = repositoryApi.getUrlsOfUserRepos(userEmail,String.valueOf(page),size);
181
            while(resultSet.size() > 0 ){
182
                retRepos.addAll(resultSet);
183
                page++;
184
                resultSet = repositoryApi.getUrlsOfUserRepos(userEmail,String.valueOf(page),size);
185
            }
186 41136 nikon.gasp
            return retRepos;
187
188
        } catch (Exception e) {
189
            LOGGER.error("Error while getting repositories(urls) of user: " + userEmail + " . IncludeShared: " + includeShared + " . IncludeByOthers: " + includeByOthers, e);
190 49751 panagiotis
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
191 41136 nikon.gasp
        }
192
    }
193
194
    @Override
195 40801 nikon.gasp
    public Repository getRepository(String repoId) throws RepositoryServiceException {
196
        try {
197
            LOGGER.debug("Getting repository with id: " + repoId);
198 49751 panagiotis
            Repository repo = repositoryApi.getRepositoryById(repoId);
199
200 40834 nikon.gasp
            if (repo != null) {
201
                for (RepositoryInterface iFace : repo.getInterfaces()) {
202
                    if (!iFace.getContentDescription().equals("file::hybrid") && iFace.getAccessProtocol().equalsIgnoreCase("oai")) {
203
                        iFace.setComplianceName(getComplianceName(iFace.getCompliance()));
204
                        if (iFace.getCompliance().equals("notCompatible"))
205
                            iFace.setComplianceName("not compatible");
206
                    }
207
                }
208
            } else
209 40336 nikon.gasp
                throw new RepositoryServiceException("registration.repositoryNotExists", RepositoryServiceException.ErrorCode.REPOSITORY_NOT_EXISTS);
210
            return repo;
211
212
        } catch (Exception e) {
213 40801 nikon.gasp
            LOGGER.error("Error while getting repository with id: " + repoId, e);
214 40336 nikon.gasp
            if (e instanceof RepositoryServiceException) {
215
                throw (RepositoryServiceException) e;
216
            } else {
217 40543 nikon.gasp
                emailUtils.reportException(e);
218 40336 nikon.gasp
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
219
            }
220
        }
221
    }
222
223
    @Override
224 40543 nikon.gasp
    public Map<String, String> getCountries(Boolean existingOnly, String mode) throws RepositoryServiceException {
225 40516 nikon.gasp
        try {
226
            LOGGER.debug("Getting countries");
227
            List<String> countries = new ArrayList<String>();
228 40543 nikon.gasp
229
            Map<String, String> countriesMap = new TreeMap<String, String>();
230
231 49791 panagiotis
            if (existingOnly) {
232 40516 nikon.gasp
                LOGGER.debug("using the repositories map");
233
                countries.addAll(this.repoAPI.getRepositoriesByCountry(mode).keySet());
234
            } else {
235
                LOGGER.debug("using \"dnet:countries\" vocabulary");
236 40861 nikon.gasp
                countries.addAll(this.getVocabulary("dnet:countries").getEnglishNames());
237 49791 panagiotis
            }
238
//            countries.addAll(repositoryApi.getDnetCountries());
239 40543 nikon.gasp
            for (String country : countries) {
240
                countriesMap.put(country, WordUtils.capitalizeFully(country));
241
            }
242
243
            return countriesMap;
244
245 40516 nikon.gasp
        } catch (Exception e) {
246 40546 nikon.gasp
            LOGGER.error("Error while getting getting countries", e);
247 49791 panagiotis
            if (e instanceof RepositoryServiceException) {
248 40516 nikon.gasp
                throw (RepositoryServiceException) e;
249 49791 panagiotis
            } else {
250 49783 panagiotis
              //  emailUtils.reportException(e);
251 40516 nikon.gasp
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
252 49791 panagiotis
            }
253 40516 nikon.gasp
        }
254
    }
255
256
    @Override
257 40543 nikon.gasp
    public Map<String, String> getCountries() throws RepositoryServiceException {
258 41049 nikon.gasp
        return this.getCountries(false, null);
259 40516 nikon.gasp
    }
260
261
    @Override
262 40552 nikon.gasp
    public List<Timezone> getTimezones() throws RepositoryServiceException {
263
        try {
264
            LOGGER.debug("Getting timezones from file");
265 49791 panagiotis
//            return repositoryApi.getTimezones();
266
            return LocalVocabularies.timezones;
267 40552 nikon.gasp
        } catch (Exception e) {
268
            LOGGER.error("Error while getting timezones from file", e);
269
            emailUtils.reportException(e);
270
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
271
        }
272
273
    }
274
275
    @Override
276
    public List<String> getTypologies() throws RepositoryServiceException {
277
        try {
278
            LOGGER.debug("Getting typologies from file");
279 49791 panagiotis
           // return repositoryApi.getTypologies();
280
            return LocalVocabularies.typologies;
281 41049 nikon.gasp
        } catch (Exception e) {
282 40552 nikon.gasp
            LOGGER.error("Error while getting typologies from file", e);
283
            emailUtils.reportException(e);
284
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
285
        }
286
    }
287
288
    @Override
289 40593 nikon.gasp
    public Map<String, String> getDatasourceClasses(String mode) throws RepositoryServiceException {
290 49783 panagiotis
        return repositoryApi.getDatasourceClasses(mode);
291 40575 nikon.gasp
    }
292
293
    @Override
294 40611 nikon.gasp
    public Map<String, String> getCompatibilityClasses(String mode) throws RepositoryServiceException {
295 49783 panagiotis
        return repositoryApi.getCompatibilityClasses(mode);
296 40611 nikon.gasp
    }
297
298
    @Override
299 40336 nikon.gasp
    public void storeRepository(Repository repo, String mode) throws RepositoryServiceException {
300
301 40575 nikon.gasp
        try {
302
            LOGGER.debug("Storing repository with name: " + repo.getOfficialName());
303 49964 panagiotis
            //List<RepositoryInterface> interfacesToRegister = new ArrayList<RepositoryInterface>();
304
            JSONObject params = new JSONObject();
305 49974 panagiotis
            params.put("datatype", mode);
306
            ObjectMapper mapper = new ObjectMapper();
307
            String json_repo = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(repo);
308
            params.put("repository", json_repo);
309 49964 panagiotis
            repositoryApi.addRepository(params.toString());
310 49909 panagiotis
            LOGGER.debug("Repository with name: " + repo.getOfficialName() + " stored!");
311 49974 panagiotis
        }catch (Exception e) {
312
                LOGGER.error("Error while storing repository with name: " + repo.getOfficialName(), e);
313
                throw new RepositoryServiceException("Error while storing repository", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
314
            }
315 40575 nikon.gasp
        }
316 40546 nikon.gasp
317 40575 nikon.gasp
    @Override
318
    public void updateRepositoryInformation(Repository repo) throws RepositoryServiceException {
319
        try {
320
            LOGGER.debug("Updating information of repo: " + repo.getOfficialName());
321 49812 panagiotis
            //this.repoAPI.updateRepositoryInformation(repo);
322
            repositoryApi.updateEnglishName(repo.getId(),repo.getEnglishName());
323
            repositoryApi.updateLatitude(repo.getId(), String.valueOf(repo.getLatitude()));
324
            repositoryApi.updateLongitude(repo.getId(), String.valueOf(repo.getLongitude()));
325
            repositoryApi.updateOfficialName(repo.getId(),repo.getOfficialName());
326 49930 panagiotis
            repositoryApi.updateLogoUrl(repo.getId(),repo.getLogoUrl());
327
            repositoryApi.updateTimezone(repo.getId(),String.valueOf(repo.getTimezone()));
328 49989 panagiotis
            repositoryApi.updatePlatform(repo.getId(),repo.getTypology());
329 49930 panagiotis
330 40575 nikon.gasp
        } catch (Exception e) {
331
            LOGGER.error("Error while updating information of repo: " + repo.getOfficialName(), e);
332
            if (e instanceof RepositoryServiceException) {
333
                throw (RepositoryServiceException) e;
334
            } else {
335
                emailUtils.reportException(e);
336
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
337
            }
338
        }
339
    }
340
341 40579 nikon.gasp
    @Override
342 41079 nikon.gasp
    public RepositoryInterface updateInterface(RepositoryInterface iFace, String repoId, String datatype) throws RepositoryServiceException {
343 41049 nikon.gasp
        try {
344
            LOGGER.debug("updating interface with id: " + iFace.getId());
345 41089 nikon.gasp
            RepositoryInterface retIface = null;
346
            retIface = this.repoAPI.updateRepositoryInterfaceWithoutChecks(repoId, iFace, datatype);
347
            retIface.setComplianceName(this.getComplianceName(retIface.getCompliance()));
348 41005 nikon.gasp
349 41089 nikon.gasp
            return retIface;
350 41049 nikon.gasp
        } catch (Exception e) {
351
            LOGGER.error("error updating interface with id: " + iFace.getId(), e);
352
            if (e instanceof RepositoryServiceException) {
353
                throw (RepositoryServiceException) e;
354
            } else {
355
                emailUtils.reportException(e);
356
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
357
            }
358
        }
359 41005 nikon.gasp
    }
360
361
    @Override
362 41079 nikon.gasp
    public RepositoryInterface insertInterface(RepositoryInterface iFace, String repoId, String datatype) throws RepositoryServiceException {
363 41049 nikon.gasp
        try {
364
            LOGGER.debug("inserting interface with id: " + iFace.getId());
365 41089 nikon.gasp
            RepositoryInterface retIface = null;
366 49812 panagiotis
            //retIface = this.repoAPI.insertRepositoryInterfaceWithoutChecks(repoId, iFace, datatype);
367
368 49964 panagiotis
            JSONObject params = new JSONObject();
369
            params.put("datatype",datatype);
370
            params.put("repoId",repoId);
371
            params.put("iFace",iFace);
372
            retIface = repositoryApi.addRepositoryInterface(params.toString());
373 41089 nikon.gasp
            retIface.setComplianceName(this.getComplianceName(retIface.getCompliance()));
374
            return retIface;
375 41049 nikon.gasp
376
        } catch (Exception e) {
377
            LOGGER.error("error updating interface with id: " + iFace.getId(), e);
378
            if (e instanceof RepositoryServiceException) {
379
                throw (RepositoryServiceException) e;
380
            } else {
381
                emailUtils.reportException(e);
382
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
383
            }
384
        }
385
    }
386
387
    @Override
388 41087 nikon.gasp
    public void deleteInterface(String repoId, RepositoryInterface iFace, String datatype) throws RepositoryServiceException {
389 41049 nikon.gasp
        List<RepositoryInterface> iFaces = new ArrayList<RepositoryInterface>();
390
        iFaces.add(iFace);
391 41087 nikon.gasp
        this.deleteInterfaces(repoId, iFaces, datatype);
392 41049 nikon.gasp
    }
393
394
    @Override
395 41087 nikon.gasp
    public void deleteInterfaces(String repoId, List<RepositoryInterface> iFaces, String datatype) throws RepositoryServiceException {
396 41005 nikon.gasp
        try {
397
            LOGGER.debug("deleting interfaces of repo: " + repoId);
398 49812 panagiotis
            //this.repoAPI.deleteRepositoryInterfacesWithoutChecks(repoId, iFaces, datatype);
399 41005 nikon.gasp
400 49812 panagiotis
            for(RepositoryInterface iFace : iFaces) {
401
                LOGGER.info("deleting repository interface with url/set/id: " + iFace.getBaseUrl() + "/"
402
                        + iFace.getAccessSet() + "/" + iFace.getId());
403
                repositoryApi.deleteRepositoryInterface(iFace.getId());
404
            }
405
406 41005 nikon.gasp
        } catch (Exception e) {
407
            LOGGER.error("deleting interfaces of repo: " + repoId, e);
408
            if (e instanceof RepositoryServiceException) {
409
                throw (RepositoryServiceException) e;
410
            } else {
411
                emailUtils.reportException(e);
412
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
413
            }
414
        }
415
416
    }
417
418
    @Override
419 40580 nikon.gasp
    public DatasourceVocabularies getDatasourceVocabularies(String mode) throws RepositoryServiceException {
420 40579 nikon.gasp
        try {
421
            LOGGER.debug("Getting vocabularies for datasource with type: " + mode);
422 40580 nikon.gasp
            DatasourceVocabularies vocs = new DatasourceVocabularies();
423 40579 nikon.gasp
            vocs.setCountries(this.getCountries());
424
            vocs.setDatasourceClasses(this.getDatasourceClasses(mode));
425
            vocs.setTimezones(this.getTimezones());
426
            vocs.setTypologies(this.getTypologies());
427 40827 nikon.gasp
            vocs.setCompatibilityLevels(this.getCompatibilityClasses(mode));
428 40579 nikon.gasp
429
            return vocs;
430
431
        } catch (Exception e) {
432
            LOGGER.error("Error while getting vocabularies for datasource with type: \" + mode", e);
433
            emailUtils.reportException(e);
434
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
435
        }
436
    }
437
438 40548 nikon.gasp
    private Tuple<List<Repository>, List<Repository>> getRepositoriesByCountryTesting(String country, String mode, boolean includeUnknownCountries) throws RepositoryServiceException {
439
        try {
440
            LOGGER.debug("Getting testing repositories of country: " + country + " with type: " + mode + " and includeUnknownCountries: " + includeUnknownCountries);
441
442
            Tuple<List<Repository>, List<Repository>> retTuple = new Tuple<List<Repository>, List<Repository>>();
443
            List<Repository> reposList = new ArrayList<Repository>();
444
            reposList.add(this.repoAPI.getRepository("opendoar____::1356"));
445
            reposList.add(this.repoAPI.getRepository("opendoar____::2678"));
446
            reposList.add(this.repoAPI.getRepository("opendoar____::2980"));
447
            reposList.add(this.repoAPI.getRepository("opendoar____::1347"));
448 40698 stefania.m
            reposList.add(this.repoAPI.getRepository("opendoar____::2984"));
449 40548 nikon.gasp
450
            retTuple.setFirst(reposList);
451
452 41049 nikon.gasp
            if (includeUnknownCountries) {
453 40548 nikon.gasp
                List<Repository> totalList = new ArrayList<Repository>();
454
                totalList.add(this.repoAPI.getRepository("opendoar____::3000"));
455
                totalList.add(this.repoAPI.getRepository("opendoar____::1027"));
456
                totalList.add(this.repoAPI.getRepository("opendoar____::1096"));
457
458
                retTuple.setSecond(totalList);
459
            }
460
461
            return retTuple;
462
463
        } catch (Exception e) {
464 41049 nikon.gasp
            LOGGER.error("Error while getting testing repositories of country: " + country + " with type: " + mode + " and includeUnknownCountries: " + includeUnknownCountries, e);
465 40548 nikon.gasp
            if (e instanceof RepositoryServiceException) {
466
                throw (RepositoryServiceException) e;
467
            } else {
468
                emailUtils.reportException(e);
469
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
470
            }
471
        }
472
    }
473
474 41638 nikon.gasp
    @Override
475
    public String getLatestUpdateDateOfList(String mode) throws RepositoryServiceException {
476
        try {
477
            LOGGER.debug("Getting latest update date of list: " + mode);
478
            return this.repoAPI.getListLatestUpdate(mode).split("T")[0];
479
480
        } catch (Exception e) {
481
            LOGGER.error("Error while getting latest update date of list: " + mode, e);
482
            emailUtils.reportException(e);
483
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
484
        }
485
    }
486
487 45462 stefania.m
    @Override
488
    public PiwikInfo getPiwikSiteForRepository(String repoId) throws RepositoryServiceException {
489
        try {
490 49869 panagiotis
            LOGGER.debug("Repo id -> " + repoId);
491 45462 stefania.m
            return this.piwikDAO.getPiwikSiteForRepo(repoId);
492
        } catch (EmptyResultDataAccessException e) {
493
            return null;
494
        }
495
    }
496
497
    @Override
498 49691 antonis.le
    public void enableMetricsForRepository(Repository repository, UserProfile requestor) throws RepositoryServiceException {
499 45462 stefania.m
500 46279 stefania.m
        String URL = analyticsURL + "siteName=" + repository.getOfficialName() + "&url=" + repository.getWebsiteUrl();
501 45462 stefania.m
502 46279 stefania.m
        //TODO uncomment when we can communitcate with the piwik (create site)
503
//        try {
504
//            Map<String, Object> map = new ObjectMapper().readValue(URL, Map.class);
505
//
506
//            String siteId = (String) map.get("value");
507 45462 stefania.m
508 49691 antonis.le
            String siteId = new Random(new Date().getTime()).nextInt(200) + "";
509
            String authenticationToken = new Random(new Date().getTime()).nextInt(1000000000) + "";
510
511 46279 stefania.m
            PiwikInfo piwikInfo = new PiwikInfo();
512
            piwikInfo.setRepositoryId(repository.getId());
513 49691 antonis.le
            piwikInfo.setRepositoryName(repository.getOfficialName());
514
            piwikInfo.setCountry(repository.getCountryName());
515 46279 stefania.m
            piwikInfo.setSiteId(siteId);
516 49691 antonis.le
            piwikInfo.setAuthenticationToken(authenticationToken);
517
            piwikInfo.setRequestorEmail(requestor.getEmail());
518
            piwikInfo.setRequestorName(requestor.getFirstname() + " " + requestor.getLastname());
519 49909 panagiotis
            piwikInfo.setValidated(false);
520 45462 stefania.m
521 46279 stefania.m
            this.piwikDAO.savePiwikInfo(piwikInfo);
522
523 49691 antonis.le
            //TODO send this in an email as well to the usage stats admin list
524
525 46279 stefania.m
//        } catch (IOException e) {
526
//            LOGGER.error("Error while creating piwik site", e);
527
//            emailUtils.reportException(e);
528
//            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
529
//        }
530 45462 stefania.m
    }
531
532
    @Override
533
    public String getPiwikScriptForRepository(String repoId) throws RepositoryServiceException {
534
        try {
535
            PiwikInfo piwikInfo = this.piwikDAO.getPiwikSiteForRepo(repoId);
536
537
            String piwikScript = PIWIK_SCRIPT.replace("$$$", piwikInfo.getSiteId());
538
            return piwikScript;
539
540
        } catch (EmptyResultDataAccessException e) {
541
            return null;
542
        }
543
    }
544
545 49691 antonis.le
    @Override
546
    public List<PiwikInfo> getPiwikSitesForRepositories() throws RepositoryServiceException {
547
        try {
548
549
            List<PiwikInfo> piwikInfos = new ArrayList<>();
550
            piwikInfos = this.piwikDAO.getPiwikSitesForRepos();
551
552
            return piwikInfos;
553
554
        } catch (EmptyResultDataAccessException e) {
555
            LOGGER.error("Error while getting list of piwik sites: ", e);
556
            emailUtils.reportException(e);
557
            throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
558
        }
559
    }
560
561
    @Override
562
    public void approvePiwikSite(String repositoryId) throws RepositoryServiceException {
563
        try {
564
565
            this.piwikDAO.approvePiwikSite(repositoryId);
566
567
        } catch (EmptyResultDataAccessException e) {
568
            LOGGER.error("Error while approving piwik site: ", e);
569
            emailUtils.reportException(e);
570
            throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
571
        }
572
    }
573
574 40575 nikon.gasp
    private String getCountryCode(String countryName) {
575 40861 nikon.gasp
        Vocabulary countries = this.getVocabulary("dnet:countries");
576 40575 nikon.gasp
577
        return countries.getEncoding(countryName);
578
    }
579
580
    private String getDatasourceClassCode(String datasourceClassName) {
581 40861 nikon.gasp
        Vocabulary datasourceClasses = this.getVocabulary("dnet:datasource_typologies");
582 40575 nikon.gasp
583
        return datasourceClasses.getEncoding(datasourceClassName);
584
    }
585
586 40834 nikon.gasp
    private String getComplianceName(String complianceCode) {
587 40861 nikon.gasp
        Vocabulary compatibilityLevels = this.getVocabulary("dnet:compatibilityLevel");
588 40834 nikon.gasp
589
        return compatibilityLevels.getEnglishName(complianceCode);
590
    }
591
592 40861 nikon.gasp
    private Vocabulary getVocabulary(String vocName) {
593
594
        if (!vocabularyMap.containsKey(vocName)) {
595 41049 nikon.gasp
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
596 40861 nikon.gasp
        }
597
        return vocabularyMap.get(vocName);
598
    }
599
600 41049 nikon.gasp
    @Scheduled(fixedRate = 3600000)
601 40861 nikon.gasp
    private void loadVocabularies() {
602
        LOGGER.debug("loading vocabularies");
603
        for (String vocName : vocabularyNames) {
604 41049 nikon.gasp
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
605 40861 nikon.gasp
        }
606
    }
607
608 40260 stefania.m
}