Project

General

Profile

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