Project

General

Profile

1 54525 panagiotis
package eu.dnetlib.repo.manager.service;
2 49236 panagiotis
3 51549 panagiotis
import com.fasterxml.jackson.databind.ObjectMapper;
4 56636 antonis.le
import eu.dnetlib.api.functionality.ValidatorServiceException;
5 50051 panagiotis
import eu.dnetlib.domain.data.Repository;
6
import eu.dnetlib.domain.data.RepositoryInterface;
7 49450 panagiotis
import eu.dnetlib.domain.enabling.Vocabulary;
8 56636 antonis.le
import eu.dnetlib.domain.functionality.validator.JobForValidation;
9 57741 ioannis.di
import eu.dnetlib.repo.manager.domain.*;
10 54525 panagiotis
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
11
import eu.dnetlib.repo.manager.utils.Converter;
12 49450 panagiotis
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
13 50570 panagiotis
import org.apache.commons.codec.digest.DigestUtils;
14 49763 panagiotis
import org.apache.log4j.Logger;
15 49236 panagiotis
import org.json.JSONArray;
16 49362 panagiotis
import org.json.JSONException;
17 49236 panagiotis
import org.json.JSONObject;
18 49450 panagiotis
import org.springframework.beans.factory.annotation.Autowired;
19 49236 panagiotis
import org.springframework.beans.factory.annotation.Value;
20 50570 panagiotis
import org.springframework.core.ParameterizedTypeReference;
21 53933 panagiotis
import org.springframework.http.*;
22 49245 panagiotis
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
23 53933 panagiotis
import org.springframework.security.core.Authentication;
24
import org.springframework.security.core.context.SecurityContextHolder;
25 54525 panagiotis
import org.springframework.stereotype.Service;
26 50570 panagiotis
import org.springframework.web.client.RestClientException;
27 49245 panagiotis
import org.springframework.web.client.RestTemplate;
28 49304 panagiotis
import org.springframework.web.util.UriComponents;
29 49245 panagiotis
import org.springframework.web.util.UriComponentsBuilder;
30 50051 panagiotis
31 49304 panagiotis
import javax.annotation.PostConstruct;
32 53113 panagiotis
import java.io.IOException;
33 49960 panagiotis
import java.sql.Timestamp;
34 49450 panagiotis
import java.util.*;
35
import java.util.concurrent.ConcurrentHashMap;
36 52781 panagiotis
import java.util.stream.Collectors;
37 49236 panagiotis
38 54525 panagiotis
@Service("repositoryService")
39 54690 panagiotis
public class RepositoryServiceImpl implements RepositoryService {
40 49236 panagiotis
41 49304 panagiotis
    @Value("${api.baseAddress}")
42 49236 panagiotis
    private String baseAddress;
43
44 56636 antonis.le
    @Value("${services.repo-manager.adminEmail}")
45
    private String adminEmail;
46
47 54840 panagiotis
    @Autowired
48
    RestTemplate restTemplate;
49 49304 panagiotis
50 50051 panagiotis
    private HttpHeaders httpHeaders;
51
52 49450 panagiotis
    private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"};
53
54 54690 panagiotis
    private static final Logger LOGGER = Logger.getLogger(RepositoryServiceImpl.class);
55 49763 panagiotis
56 50570 panagiotis
    @Value("${services.repomanager.usageStatisticsDiagramsBaseURL}")
57
    private String usageStatisticsDiagramsBaseURL;
58
59
    @Value("${services.repomanager.usageStatisticsNumbersBaseURL}")
60
    private String usageStatisticsNumbersBaseURL;
61
62 49450 panagiotis
    @Autowired
63
    private VocabularyLoader vocabularyLoader;
64
65 50219 panagiotis
    @Autowired
66 54690 panagiotis
    private PiWikService piWikService;
67 50219 panagiotis
68 51831 panagiotis
    @Autowired
69
    private EmailUtils emailUtils;
70
71 56636 antonis.le
    @Autowired
72
    ValidatorService validatorService;
73 54702 panagiotis
74 56636 antonis.le
75 56766 ioannis.di
    private Map<String, Vocabulary> vocabularyMap = new ConcurrentHashMap<>();
76 49450 panagiotis
77 49960 panagiotis
    private Map<String, String> countriesMap = new HashMap<>();
78
    private Map<String, String> inverseCountriesMap = new HashMap<>();
79 49450 panagiotis
80 56766 ioannis.di
    private static Map<String,List<String>> dataSourceClass = new HashMap<>();
81 49898 panagiotis
82 56766 ioannis.di
    private static Map<String,String> invertedDataSourceClass = new HashMap<>();
83 51549 panagiotis
84
85
86 49304 panagiotis
    @PostConstruct
87 49960 panagiotis
    private void init() {
88 49770 panagiotis
        LOGGER.debug("Initialization method of repository api!");
89 53933 panagiotis
        LOGGER.debug("Updated version!");
90 49770 panagiotis
91 56766 ioannis.di
        dataSourceClass.put("opendoar",Arrays.asList("pubsrepository::institutional","pubsrepository::thematic","pubsrepository::unknown","pubsrepository::mock"));
92
        dataSourceClass.put("re3data", Collections.singletonList("datarepository::unknown"));
93
        dataSourceClass.put("journal", Collections.singletonList("pubsrepository::journal"));
94
        dataSourceClass.put("aggregator",Arrays.asList("aggregator::pubsrepository::institutional","aggregator::pubsrepository::journals","aggregator::datarepository", "aggregator::pubsrepository::unknown"));
95
96
        invertedDataSourceClass.put("pubsrepository::institutional","opendoar");
97
        invertedDataSourceClass.put("pubsrepository::thematic","opendoar");
98
        invertedDataSourceClass.put("pubsrepository::unknown","opendoar");
99
        invertedDataSourceClass.put("pubsrepository::mock","opendoar");
100
        invertedDataSourceClass.put("datarepository::unknown","re3data");
101
        invertedDataSourceClass.put("pubsrepository::journal","journal");
102
        invertedDataSourceClass.put("aggregator::pubsrepository::institutional","aggregator");
103
        invertedDataSourceClass.put("aggregator::pubsrepository::journals","aggregator");
104
        invertedDataSourceClass.put("aggregator::datarepository","aggregator");
105
        invertedDataSourceClass.put("aggregator::pubsrepository::unknown","aggregator");
106
107
108 55039 panagiotis
        httpHeaders = new HttpHeaders();
109 55066 panagiotis
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
110 49450 panagiotis
111
        for (String vocName : vocabularyNames) {
112
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
113
        }
114
115 49898 panagiotis
        Country[] countries = getCountries();
116 49960 panagiotis
        for (Country c : countries) {
117
            countriesMap.put(c.getName(), c.getCode());
118
            inverseCountriesMap.put(c.getCode(), c.getName());
119 49898 panagiotis
        }
120
121
122 49304 panagiotis
    }
123
124 49236 panagiotis
    @Override
125 49960 panagiotis
    public Country[] getCountries() {
126 49304 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
127
                .fromHttpUrl(baseAddress + "/ds/countries")
128
                .build().encode();
129 49960 panagiotis
        return restTemplate.getForObject(uriComponents.toUri(), Country[].class);
130 49236 panagiotis
    }
131
132
133
    @Override
134 56761 ioannis.di
    public List<RepositorySnippet> getRepositoriesByCountry(String country,
135
                                                            String mode,
136
                                                            Boolean managed) throws JSONException, IOException {
137 49245 panagiotis
138 49790 panagiotis
        LOGGER.debug("Getting repositories by country!");
139 49898 panagiotis
        int page = 0;
140
        int size = 100;
141 53113 panagiotis
        List<RepositorySnippet> resultSet = new ArrayList<>();
142
        ObjectMapper mapper = new ObjectMapper();
143 49898 panagiotis
144 49813 panagiotis
        String filterKey = "UNKNOWN";
145 51525 panagiotis
        if (mode.equalsIgnoreCase("opendoar"))
146 49813 panagiotis
            filterKey = "openaire____::opendoar";
147 51525 panagiotis
        else if (mode.equalsIgnoreCase("re3data"))
148 49813 panagiotis
            filterKey = "openaire____::re3data";
149 49431 panagiotis
150 51525 panagiotis
151 53113 panagiotis
        LOGGER.debug("Country code equals : " + country);
152 49813 panagiotis
        LOGGER.debug("Filter mode equals : " + filterKey);
153 51525 panagiotis
154 53113 panagiotis
        UriComponents uriComponents = searchSnipperDatasource(String.valueOf(page),String.valueOf(size));
155 50631 panagiotis
        RequestFilter requestFilter = new RequestFilter();
156 53113 panagiotis
        requestFilter.setCountry(country);
157
        requestFilter.setCollectedfrom(filterKey);
158 49245 panagiotis
159 51831 panagiotis
        try{
160
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
161
            JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
162
            while (jsonArray.length() > 0 ) {
163 53113 panagiotis
                resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),
164
                        mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
165 51831 panagiotis
                page += 1;
166 53113 panagiotis
                uriComponents = searchSnipperDatasource(String.valueOf(page),String.valueOf(size));
167 51831 panagiotis
                rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
168
                jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
169
            }
170
            return resultSet;
171
        }catch (Exception e){
172
            LOGGER.debug("Exception on getRepositoriesByCountry" , e);
173 53113 panagiotis
//            emailUtils.reportException(e);
174 51831 panagiotis
            throw e;
175 49431 panagiotis
        }
176 56636 antonis.le
    }
177 50075 panagiotis
178 56636 antonis.le
    public List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName,
179
            String officialName, String requestSortBy, String order, int page, int pageSize) throws Exception {
180 51831 panagiotis
181 56636 antonis.le
        LOGGER.debug("Searching registered repositories");
182
183
        List<RepositorySnippet> resultSet = new ArrayList<>();
184
        ObjectMapper mapper = new ObjectMapper();
185
186
        UriComponents uriComponents = searchRegisteredDatasource(requestSortBy, order, Integer.toString(page), Integer.toString(pageSize));
187
188
        RequestFilter requestFilter = new RequestFilter();
189
        requestFilter.setCountry(country);
190
        requestFilter.setTypology(typology);
191
        requestFilter.setOfficialname(officialName);
192
        requestFilter.setEnglishname(englishName);
193
194
        try {
195
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
196
            JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
197
198
            resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),  mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
199
200
             return resultSet;
201
        }catch (Exception e){
202
            LOGGER.error("Error searching registered datasources" , e);
203
            throw e;
204
        }
205 49236 panagiotis
    }
206
207 52781 panagiotis
    private Repository updateRepositoryInfo(Repository r) throws JSONException {
208 51549 panagiotis
209
        /*
210
        * from datasource class
211
        * we get the datasource type form the inverted map
212
        * */
213
        r.setDatasourceType(getRepositoryType(r.getDatasourceClass()));
214 50219 panagiotis
        r.setInterfaces(this.getRepositoryInterface(r.getId()));
215 54690 panagiotis
        r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId()));
216 50219 panagiotis
        r.setCountryName(getCountryName(r.getCountryCode()));
217 52781 panagiotis
        return r;
218 50219 panagiotis
    }
219 49790 panagiotis
220 50219 panagiotis
221 49790 panagiotis
    private Collection<Repository> getRepositoriesByMode(String mode, List<Repository> rs) {
222 49431 panagiotis
223 49790 panagiotis
        List<Repository> reps = new ArrayList<>();
224 49960 panagiotis
        for (Repository r : rs) {
225 50631 panagiotis
            if (r.getCollectedFrom() != null && r.getCollectedFrom().equals(mode))
226 49790 panagiotis
                reps.add(r);
227 51525 panagiotis
228 49813 panagiotis
        }
229 49431 panagiotis
        return reps;
230
    }
231
232 49236 panagiotis
    @Override
233 56761 ioannis.di
    public List<Repository> getRepositoriesOfUser(String userEmail,
234
                                                  String page,
235
                                                  String size) throws JSONException {
236 49397 panagiotis
237 49975 panagiotis
        LOGGER.debug("Retreiving repositories of user : " + userEmail );
238 50631 panagiotis
        UriComponents uriComponents = searchDatasource(page,size);
239 50614 panagiotis
        RequestFilter requestFilter = new RequestFilter();
240
        requestFilter.setRegisteredby(userEmail);
241 50845 panagiotis
242 51831 panagiotis
        try{
243
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
244 49988 panagiotis
245 51831 panagiotis
            List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs));
246
            for (Repository r : repos)
247 57741 ioannis.di
                r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId()));
248 51831 panagiotis
            return repos;
249
        }catch (Exception e){
250
            LOGGER.debug("Exception on getRepositoriesOfUser" , e);
251
            emailUtils.reportException(e);
252
            throw e;
253
        }
254 49236 panagiotis
    }
255
256
    @Override
257 57874 ioannis.di
    public List<RepositorySnippet> getRepositoriesSnippetOfUser(String userEmail, String page, String size) throws IOException, JSONException {
258
        List<RepositorySnippet> resultSet = new ArrayList<>();
259
        ObjectMapper mapper = new ObjectMapper();
260
261
        UriComponents uriComponents = searchSnipperDatasource(page,size);
262
        RequestFilter requestFilter = new RequestFilter();
263
        requestFilter.setRegisteredby(userEmail);
264
265
        try{
266
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
267
            JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
268
            while (jsonArray.length() > 0 ) {
269
                resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),
270
                        mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
271
                page += 1;
272
                uriComponents = searchSnipperDatasource(page,size);
273
                rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
274
                jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
275
            }
276
            resultSet.parallelStream().forEach(repositorySnippet -> {
277
                repositorySnippet.setPiwikInfo(piWikService.getPiwikSiteForRepo(repositorySnippet.getId()));
278
            });
279
            return resultSet;
280
        }catch (Exception e){
281
            LOGGER.debug("Exception on getRepositoriesByCountry" , e);
282
            throw e;
283
        }
284
    }
285
286
    @Override
287 56761 ioannis.di
    public Repository getRepositoryById(String id) throws JSONException,ResourceNotFoundException {
288 49397 panagiotis
289 50845 panagiotis
        LOGGER.debug("Retreiving repositories with id : " + id );
290 50945 panagiotis
        Repository repo = null;
291 50845 panagiotis
        UriComponents uriComponents = searchDatasource("0","100");
292
        RequestFilter requestFilter = new RequestFilter();
293
        requestFilter.setId(id);
294 51831 panagiotis
295
        try{
296
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
297
            JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
298 52781 panagiotis
299
            if(jsonArray.length() == 0)
300
                throw new ResourceNotFoundException();
301
302
            repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0));
303
            return updateRepositoryInfo(repo);
304
        }catch (JSONException e){
305 51831 panagiotis
            LOGGER.debug("Exception on getRepositoryById" , e);
306
            emailUtils.reportException(e);
307
            throw e;
308
        }
309
310 49236 panagiotis
    }
311
312 49988 panagiotis
313 49236 panagiotis
    @Override
314 57176 ioannis.di
    public List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException {
315 49763 panagiotis
316 50845 panagiotis
        LOGGER.debug("Retreiving aggregations for repository with id : " + id );
317 57176 ioannis.di
        UriComponents uriComponents = searchDatasource(from+"",size+"");
318 50845 panagiotis
        RequestFilter requestFilter = new RequestFilter();
319
        requestFilter.setId(id);
320 49935 panagiotis
321 52781 panagiotis
        List<AggregationDetails> aggregationHistory = new ArrayList<>();
322
323 49960 panagiotis
        try {
324 57549 ioannis.di
            long start = System.currentTimeMillis();
325 51831 panagiotis
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
326 57549 ioannis.di
            long end = System.currentTimeMillis();
327
328
            System.out.println("Aggregations request through rest template took " + (end-start)+"ms");
329 51831 panagiotis
            JSONObject repository = new JSONObject(rs);
330 54745 panagiotis
331
            if(repository.getJSONArray("datasourceInfo").length() == 0)
332
                return aggregationHistory;
333 57549 ioannis.di
334
            start = System.currentTimeMillis();
335 52781 panagiotis
            aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
336 57549 ioannis.di
            end = System.currentTimeMillis();
337
338
            System.out.println("Getting aggregations history from json " + (end-start)+"ms");
339 52781 panagiotis
            return aggregationHistory.size() == 0? aggregationHistory : aggregationHistory.stream()
340
                                                    .sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
341 57155 ioannis.di
                                                    .limit(size)
342 52781 panagiotis
                                                    .collect(Collectors.toList());
343
        } catch (JSONException e) {
344
            LOGGER.debug("Exception on getRepositoryAggregations" , e);
345
            emailUtils.reportException(e);
346
            throw e;
347
        }
348 51831 panagiotis
349 52781 panagiotis
    }
350
351
    @Override
352 56761 ioannis.di
    public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException {
353 52781 panagiotis
        LOGGER.debug("Retreiving aggregations (by year) for repository with id : " + id );
354
        UriComponents uriComponents = searchDatasource("0","100");
355
        RequestFilter requestFilter = new RequestFilter();
356
        requestFilter.setId(id);
357
358
        List<AggregationDetails> aggregationHistory = new ArrayList<>();
359
        Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>();
360
        try {
361
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
362
            JSONObject repository = new JSONObject(rs);
363 54745 panagiotis
364
            if(repository.getJSONArray("datasourceInfo").length() == 0)
365
                return aggregationByYear;
366
367 52781 panagiotis
            aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
368
            return aggregationHistory.size() == 0? aggregationByYear:createYearMap(aggregationHistory);
369
370 49960 panagiotis
        } catch (JSONException e) {
371 51831 panagiotis
            LOGGER.debug("Exception on getRepositoryAggregations" , e);
372
            emailUtils.reportException(e);
373 49935 panagiotis
            throw e;
374
        }
375 52781 panagiotis
    }
376 49935 panagiotis
377 52781 panagiotis
    private Map<String,List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) {
378
        Map<String, List<AggregationDetails>> aggregationByYear;
379
        aggregationHistory = aggregationHistory.stream()
380
                            .sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
381
                            .collect(Collectors.toList());
382
383
       return aggregationHistory.stream()
384
                            .collect(Collectors.groupingBy(AggregationDetails::getYear));
385 49763 panagiotis
    }
386
387 52781 panagiotis
388 49763 panagiotis
    @Override
389 56761 ioannis.di
    public List<Repository> getRepositoriesByName(String name,
390
                                                  String page,
391
                                                  String size) throws JSONException {
392 49397 panagiotis
393 50845 panagiotis
        LOGGER.debug("Retreiving  repositories with official name : " + name );
394
        UriComponents uriComponents = searchDatasource("0","100");
395
        RequestFilter requestFilter = new RequestFilter();
396
        requestFilter.setOfficialname(name);
397
398 51831 panagiotis
        try{
399
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
400
            List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs));
401
            for (Repository r : repos)
402 52781 panagiotis
                updateRepositoryInfo(r);
403 51831 panagiotis
            return repos;
404
        }catch (Exception e){
405
            LOGGER.debug("Exception on getRepositoriesByName" , e);
406
            emailUtils.reportException(e);
407
            throw e;
408
        }
409
410 49236 panagiotis
    }
411
412
    @Override
413 56761 ioannis.di
    public List<RepositoryInterface> getRepositoryInterface(String id) throws JSONException {
414 49397 panagiotis
415
        UriComponents uriComponents = UriComponentsBuilder
416 49813 panagiotis
                .fromHttpUrl(baseAddress + "/ds/api/")
417 51330 panagiotis
                .path("/{id}")
418 49397 panagiotis
                .build().expand(id).encode();
419
420 51831 panagiotis
        try{
421
            String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
422
            return Converter.jsonToRepositoryInterfaceList(new JSONObject(rs));
423
        }catch (Exception e ){
424
            LOGGER.debug("Exception on getRepositoryInterface" , e);
425
            emailUtils.reportException(e);
426
            throw e;
427
        }
428
429 49236 panagiotis
    }
430
431
    @Override
432 56761 ioannis.di
    public Repository addRepository(String datatype,
433
                                    Repository repository) throws Exception {
434 49898 panagiotis
435 49960 panagiotis
        LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId());
436 54690 panagiotis
437
        repository.setCountryCode(countriesMap.get(repository.getCountryName()));
438
        repository.setActivationId(UUID.randomUUID().toString());
439
        repository.setCollectedFrom("infrastruct_::openaire");
440
441
        if (datatype.equals("journal")) {
442
            repository.setId("openaire____::issn" + repository.getIssn());
443
            repository.setNamespacePrefix("issn" + repository.getIssn());
444
            this.storeRepository(repository, SecurityContextHolder.getContext().getAuthentication());
445
        }else if (datatype.equals("aggregator")) {
446
            repository.setId("openaire____::" + com.unboundid.util.Base64.encode(repository.getOfficialName()));
447
            repository.setNamespacePrefix(DigestUtils.md5Hex(repository.getOfficialName()).substring(0,12));
448
            this.storeRepository(repository, SecurityContextHolder.getContext().getAuthentication());
449
        }else {
450
            this.latentUpdate(repository, SecurityContextHolder.getContext().getAuthentication());
451
        }
452
453 50945 panagiotis
        return repository;
454 49963 panagiotis
    }
455 49960 panagiotis
456 54690 panagiotis
    /* update method acting as add -> send email with registration topic/body*/
457
    private Repository latentUpdate(Repository repository, Authentication authentication) throws Exception {
458
        UriComponents uriComponents = UriComponentsBuilder
459
                .fromHttpUrl(baseAddress + "/ds/update/")
460
                .build()
461
                .encode();
462
463
        try {
464
            String json_repository = Converter.repositoryObjectToJson(repository);
465
            LOGGER.debug("JSON to add(update) -> " + json_repository);
466
467
            HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
468
            ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(),HttpMethod.POST, httpEntity, ResponseEntity.class);
469
470 54924 panagiotis
            if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
471 54690 panagiotis
                emailUtils.sendUserRegistrationEmail(repository, authentication);
472 56636 antonis.le
                emailUtils.sendAdminRegistrationEmail(repository, authentication);
473
            } else
474 54690 panagiotis
                LOGGER.debug(responseEntity.getBody().toString());
475
476
            return repository;
477
        } catch (Exception e) {
478
            LOGGER.debug("Exception on updateRepository" , e);
479
            emailUtils.reportException(e);
480
            throw e;
481
        }
482
483
484
    }
485
486 51239 panagiotis
    @Override
487 56761 ioannis.di
    public Repository updateRepository(Repository repository,Authentication authentication) throws Exception {
488 51330 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
489 51239 panagiotis
                .fromHttpUrl(baseAddress + "/ds/update/")
490
                .build()
491 51330 panagiotis
                .encode();
492 51586 panagiotis
493 51831 panagiotis
        try {
494 54078 panagiotis
            String json_repository = Converter.repositoryObjectToJson(repository);
495 53933 panagiotis
496
            LOGGER.debug("JSON to update -> " + json_repository);
497
498
            HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
499 56636 antonis.le
            ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(),HttpMethod.POST, httpEntity
500
                    , ResponseEntity.class);
501 53933 panagiotis
502 56636 antonis.le
            if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
503 57912 stefania.m
                emailUtils.sendUserUpdateRepositoryInfoEmail(repository, authentication);
504
                emailUtils.sendAdminUpdateRepositoryInfoEmail(repository, authentication);
505 56636 antonis.le
            } else
506 53933 panagiotis
                LOGGER.debug(responseEntity.getBody().toString());
507
508 51831 panagiotis
            return repository;
509 53933 panagiotis
        } catch (Exception e) {
510 51831 panagiotis
            LOGGER.debug("Exception on updateRepository" , e);
511
            emailUtils.reportException(e);
512
            throw e;
513
        }
514 50075 panagiotis
    }
515
516 53933 panagiotis
    private void storeRepository(Repository repository, Authentication authentication) throws Exception {
517 49960 panagiotis
518 49963 panagiotis
        Date utilDate = new Date();
519
        Timestamp date = new Timestamp(utilDate.getTime());
520
        repository.setDateOfCollection(date);
521
        repository.setAggregator("OPENAIRE");
522 51525 panagiotis
        repository.setCountryCode(countriesMap.get(repository.getCountryName()));
523 49963 panagiotis
524 49397 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
525
                .fromHttpUrl(baseAddress + "/ds/add/")
526
                .build()
527
                .encode();
528 50051 panagiotis
        String json_repository = Converter.repositoryObjectToJson(repository);
529
        HttpEntity<String> httpEntity = new HttpEntity <String> (json_repository,httpHeaders);
530 54078 panagiotis
        ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(),HttpMethod.POST, httpEntity, ResponseEntity.class);
531 53933 panagiotis
532 56636 antonis.le
        if(responseEntity.getStatusCode().equals(HttpStatus.OK)) {
533
            emailUtils.sendUserRegistrationEmail(repository, authentication);
534
            emailUtils.sendAdminRegistrationEmail(repository, authentication);
535
        } else {
536 53933 panagiotis
            LOGGER.debug(responseEntity.getBody().toString());
537
        }
538 49236 panagiotis
    }
539
540
    @Override
541 56761 ioannis.di
    public void deleteRepositoryInterface(String id ,
542
                                          String registeredBy){
543 49397 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
544 49813 panagiotis
                .fromHttpUrl(baseAddress + "/ds/api/")
545 51525 panagiotis
                .path("/{id}")
546 49813 panagiotis
                .build().expand(id).encode();
547 51525 panagiotis
        LOGGER.debug(uriComponents.toUri());
548 49813 panagiotis
        restTemplate.delete(uriComponents.toUri());
549
    }
550
551
    @Override
552 56761 ioannis.di
    public RepositoryInterface addRepositoryInterface(String datatype,
553
                                                      String repoId,
554
                                                      String registeredBy,
555 57912 stefania.m
                                                      RepositoryInterface repositoryInterface) throws Exception {
556 49963 panagiotis
        try {
557 51525 panagiotis
            Repository e = this.getRepositoryById(repoId);
558
            repositoryInterface = createRepositoryInterface(e,repositoryInterface,datatype);
559
            String json_interface = Converter.repositoryInterfaceObjectToJson(e,repositoryInterface);
560 49963 panagiotis
561
            UriComponents uriComponents = UriComponentsBuilder
562
                    .fromHttpUrl(baseAddress + "/ds/api/add/")
563
                    .build()
564
                    .encode();
565
566 56761 ioannis.di
            HttpEntity<String> httpEntity = new HttpEntity <> (json_interface,httpHeaders);
567
568 50051 panagiotis
569 57912 stefania.m
            ResponseEntity responseEntity = restTemplate.postForObject(uriComponents.toUri(),httpEntity,ResponseEntity.class);
570
571
            if(responseEntity.getStatusCode().equals(HttpStatus.OK)) {
572
                emailUtils.sendAdminRegisterInterfaceEmail(e, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
573
                emailUtils.sendUserRegisterInterfaceEmail(e, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
574
575
                submitInterfaceValidation(e, registeredBy, repositoryInterface, false);
576
                return repositoryInterface;
577
            } else {
578
                LOGGER.debug(responseEntity.getBody().toString());
579
                throw new Exception("Registering" + baseAddress + " returned " + responseEntity.getStatusCode());
580
            }
581
582 56761 ioannis.di
        } catch (JSONException | ValidatorServiceException e) {
583 51831 panagiotis
            LOGGER.debug("Exception on addRepositoryInterface" , e);
584
            emailUtils.reportException(e);
585
            throw e;
586 49963 panagiotis
        }
587
    }
588
589 56636 antonis.le
    @Override
590 56761 ioannis.di
    public RepositoryInterface updateRepositoryInterface(String repoId,
591
                                                         String registeredBy,
592
                                                         RepositoryInterface repositoryInterface) throws Exception {
593 56636 antonis.le
594
        this.updateBaseUrl(repoId,repositoryInterface.getId(),repositoryInterface.getBaseUrl());
595
        this.updateCompliance(repoId,repositoryInterface.getId(),repositoryInterface.getCompliance());
596
        this.updateValidationSet(repoId,repositoryInterface.getId(),repositoryInterface.getAccessSet());
597 57912 stefania.m
598
        Repository e = this.getRepositoryById(repoId);
599
        emailUtils.sendAdminUpdateInterfaceEmail(e, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
600
        emailUtils.sendUserUpdateInterfaceEmail(e, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
601 56961 ioannis.di
        submitInterfaceValidation(getRepositoryById(repoId),registeredBy,repositoryInterface,true);
602 57912 stefania.m
603 56636 antonis.le
        return repositoryInterface;
604
    }
605
606 56961 ioannis.di
    private void submitInterfaceValidation(Repository repo, String userEmail, RepositoryInterface iFace, boolean updateExisting) throws ValidatorServiceException {
607 56636 antonis.le
        JobForValidation job = new JobForValidation();
608
609
        job.setActivationId(UUID.randomUUID().toString());
610
        job.setAdminEmails(Collections.singletonList(this.adminEmail));
611
        job.setBaseUrl(iFace.getBaseUrl());
612
        job.setDatasourceId(repo.getId());
613
        job.setDesiredCompatibilityLevel(iFace.getDesiredCompatibilityLevel());
614
        job.setInterfaceId(iFace.getId());
615
        job.setOfficialName(repo.getOfficialName());
616 56761 ioannis.di
        job.setRepoType(repo.getDatasourceType());
617 56636 antonis.le
        job.setUserEmail(userEmail);
618 56761 ioannis.di
        job.setValidationSet((iFace.getAccessSet().isEmpty() ? "none" : iFace.getAccessSet()));
619 56636 antonis.le
        job.setRecords(-1);
620 56961 ioannis.di
        job.setRegistration(!updateExisting);
621
        job.setUpdateExisting(updateExisting);
622 56636 antonis.le
623
        this.validatorService.submitJobForValidation(job);
624
    }
625
626 51525 panagiotis
    private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) {
627
628
        iFace.setContentDescription("metadata");
629
        iFace.setCompliance("UNKNOWN");
630
631
        if (datatype.equals("re3data"))
632
            iFace.setAccessFormat("oai_datacite");
633
        else
634
            iFace.setAccessFormat("oai_dc");
635
636
637
        if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty())
638
            iFace.setTypology(repo.getDatasourceClass());
639
        else if (datatype.equalsIgnoreCase("journal"))
640
            iFace.setTypology("pubsrepository::journal");
641
        else if (datatype.equalsIgnoreCase("aggregator"))
642
            iFace.setTypology("aggregator::pubsrepository::unknown");
643
        else if (datatype.equalsIgnoreCase("opendoar"))
644
            iFace.setTypology("pubsrepository::unknown");
645
        else if (datatype.equalsIgnoreCase("re3data"))
646
            iFace.setTypology("datarepository::unknown");
647
648
        iFace.setRemovable(true);
649
        iFace.setAccessProtocol("oai");
650
        iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']");
651
        iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8));
652 56761 ioannis.di
        if (iFace.getAccessSet() == null || iFace.getAccessSet().isEmpty()) {
653 51525 panagiotis
            LOGGER.debug("set is empty: " + iFace.getAccessSet());
654
            iFace.removeAccessSet();
655 56761 ioannis.di
            iFace.setAccessSet("none");
656 51525 panagiotis
        }
657
        return iFace;
658
    }
659
660 49236 panagiotis
    @Override
661
    public List<String> getDnetCountries() {
662 49770 panagiotis
        LOGGER.debug("Getting dnet-countries!");
663 49236 panagiotis
        return Converter.readFile("countries.txt");
664
    }
665
666
    @Override
667
    public List<String> getTypologies() {
668
        return Converter.readFile("typologies.txt");
669
    }
670
671
    @Override
672 49763 panagiotis
    public List<Timezone> getTimezones() {
673
        List<String> timezones =  Converter.readFile("timezones.txt");
674
        return Converter.toTimezones(timezones);
675 49236 panagiotis
    }
676
677
    @Override
678 56761 ioannis.di
    public List<String> getUrlsOfUserRepos(String userEmail,
679
                                           String page,
680
                                           String size){
681 49763 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
682
                .fromHttpUrl(baseAddress + "/api/baseurl/")
683
                .path("/{page}/{size}")
684
                .build().expand(page,size).encode();
685 50614 panagiotis
686 51831 panagiotis
        try{
687
            RequestFilter requestFilter = new RequestFilter();
688
            requestFilter.setRegisteredby(userEmail);
689
            return Arrays.asList(restTemplate.postForObject(uriComponents.toUri(),requestFilter, String[].class));
690
        }catch (Exception e){
691
            LOGGER.debug("Exception on addRepositoryInterface" , e);
692
            emailUtils.reportException(e);
693
            throw e;
694
        }
695 49236 panagiotis
    }
696
697 49450 panagiotis
    @Override
698 56761 ioannis.di
    public List<String> getDatasourceVocabularies(String mode) {
699 49450 panagiotis
700
        List<String> resultSet = new ArrayList<>();
701
        for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
702
            if (mode.equalsIgnoreCase("aggregator")) {
703
                if (entry.getKey().contains("aggregator"))
704
                    resultSet.add(entry.getValue());
705
            } else if (mode.equalsIgnoreCase("journal")) {
706
                if (entry.getKey().contains("journal"))
707
                    resultSet.add(entry.getValue());
708
            } else if (mode.equalsIgnoreCase("opendoar")) {
709
                if (entry.getKey().contains("pubsrepository"))
710
                    resultSet.add(entry.getValue());
711
            } else if (mode.equalsIgnoreCase("re3data")) {
712
                if (entry.getKey().contains("datarepository"))
713
                    resultSet.add(entry.getValue());
714
            }
715
        }
716
717
718
        return resultSet;
719
    }
720
721
    private Vocabulary getVocabulary(String vocName) {
722
723
        if (!vocabularyMap.containsKey(vocName)) {
724
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
725
        }
726
        return vocabularyMap.get(vocName);
727
    }
728
729 49790 panagiotis
730
    @Override
731 56761 ioannis.di
    public Map<String, String> getCompatibilityClasses(String mode)  {
732 49790 panagiotis
733
        LOGGER.debug("Getting compatibility classes for mode: " + mode);
734
        Map<String, String> retMap = new HashMap<String, String>();
735
736
        Map<String, String> compatibilityClasses = this.getVocabulary("dnet:compatibilityLevel").getAsMap();
737
        boolean foundData = false;
738
        for (Map.Entry<String, String> entry : compatibilityClasses.entrySet()) {
739
            if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL))
740
                return compatibilityClasses;
741
            else if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA)) {
742
                if (entry.getKey().matches("^openaire[1-9].0_data$")) {
743
                    retMap.put(entry.getKey(), entry.getValue());
744
                    foundData = true;
745
                }
746
            } else {
747
                if (entry.getKey().matches("^openaire[1-9].0$") || entry.getKey().equals("driver"))
748
                    retMap.put(entry.getKey(), entry.getValue());
749
            }
750
        }
751
752
        //TODO TO BE REMOVED WHEN VOCABULARIES ARE UPDATED
753
        if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA) && !foundData)
754
            retMap.put("openaire2.0_data", "OpenAIRE Data (funded, referenced datasets)");
755
756
        return retMap;
757
    }
758
759
    @Override
760 56761 ioannis.di
    public Map<String, String> getDatasourceClasses(String mode)  {
761 49790 panagiotis
762
        LOGGER.debug("Getting datasource classes for mode: " + mode);
763
764
        Map<String, String> retMap = new HashMap<String, String>();
765
766
        for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
767
            if (mode.equalsIgnoreCase("aggregator")) {
768
                if (entry.getKey().contains("aggregator"))
769
                    retMap.put(entry.getKey(), entry.getValue());
770
            } else if (mode.equalsIgnoreCase("journal")) {
771
                if (entry.getKey().contains("journal"))
772
                    retMap.put(entry.getKey(), entry.getValue());
773
            } else if (mode.equalsIgnoreCase("opendoar")) {
774
                if (entry.getKey().contains("pubsrepository"))
775
                    retMap.put(entry.getKey(), entry.getValue());
776
            } else if (mode.equalsIgnoreCase("re3data")) {
777
                if (entry.getKey().contains("datarepository"))
778
                    retMap.put(entry.getKey(), entry.getValue());
779
            }
780
        }
781 51549 panagiotis
        return filterResults(retMap,mode);
782 49790 panagiotis
783
    }
784
785 51549 panagiotis
    private Map<String,String> filterResults(Map<String, String> map,String mode) {
786
787
        HashMap<String,String> filteredMap = new HashMap<>();
788
        for(String key:map.keySet())
789
            if(dataSourceClass.get(mode).contains(key))
790
                filteredMap.put(key,map.get(key));
791
792
        return filteredMap;
793
    }
794
795 49898 panagiotis
    @Override
796
    public String getCountryName(String countryCode) {
797
        return inverseCountriesMap.get(countryCode);
798
    }
799
800 50570 panagiotis
    @Override
801 56761 ioannis.di
    public MetricsInfo getMetricsInfoForRepository(String repoId) throws RepositoryServiceException {
802 50570 panagiotis
        try {
803
804
            MetricsInfo metricsInfo = new MetricsInfo();
805
            metricsInfo.setDiagramsBaseURL(this.usageStatisticsDiagramsBaseURL);
806
            metricsInfo.setMetricsNumbers(getMetricsNumbers(getOpenAIREId(repoId)));
807
            return metricsInfo;
808
809
        } catch (Exception e) {
810
            LOGGER.error("Error while getting metrics info for repository: ", e);
811 51831 panagiotis
            emailUtils.reportException(e);
812 50570 panagiotis
            throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
813
        }
814
    }
815
816 51330 panagiotis
    @Override
817 56761 ioannis.di
    public Map<String, String> getListLatestUpdate(String mode) throws JSONException {
818 51330 panagiotis
        if(mode.equals("opendoar"))
819
            return Collections.singletonMap("lastCollectionDate", getRepositoryInterface("openaire____::"+mode).get(0).getLastCollectionDate());
820
        else
821
            /*
822
            * first api of re3data has null value on collection date
823
            * */
824
            return Collections.singletonMap("lastCollectionDate", getRepositoryInterface("openaire____::"+mode).get(1).getLastCollectionDate());
825
    }
826
827 54702 panagiotis
    private void updateValidationSet(String repositoryId, String repositoryInterfaceId, String validationSet) throws Exception {
828 51549 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
829
                .fromHttpUrl(baseAddress + "/ds/api/oaiset")
830
                .queryParam("dsId",repositoryId)
831
                .queryParam("apiId",repositoryInterfaceId)
832
                .queryParam("oaiSet",validationSet)
833
                .build().encode();
834 54702 panagiotis
       restTemplate.exchange(uriComponents.toUri(),HttpMethod.POST, null, ResponseEntity.class);
835
836 51549 panagiotis
    }
837 51525 panagiotis
838 51549 panagiotis
839 51525 panagiotis
    private void updateBaseUrl(String repositoryId, String repositoryInterfaceId, String baseUrl) {
840
        UriComponents uriComponents = UriComponentsBuilder
841
                .fromHttpUrl(baseAddress + "/ds/api/baseurl")
842
                .queryParam("dsId",repositoryId)
843
                .queryParam("apiId",repositoryInterfaceId)
844
                .queryParam("baseUrl",baseUrl)
845
                .build().encode();
846
        restTemplate.postForObject(uriComponents.toUri(),null,String.class);
847
    }
848
849
    private void updateCompliance(String repositoryId, String repositoryInterfaceId,String compliance) {
850
        UriComponents uriComponents = UriComponentsBuilder
851
                .fromHttpUrl(baseAddress + "/ds/api/compliance")
852
                .queryParam("dsId",repositoryId)
853
                .queryParam("apiId",repositoryInterfaceId)
854
                .queryParam("compliance",compliance)
855
                .build().encode();
856
        restTemplate.postForObject(uriComponents.toUri(),null,String.class);
857
    }
858
859 50570 panagiotis
    private MetricsNumbers getMetricsNumbers(String openAIREID) throws BrokerException {
860
861
        //build the uri params
862
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usageStatisticsNumbersBaseURL + openAIREID + "/clicks");
863
864
        //create new template engine
865
        RestTemplate template = new RestTemplate();
866
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
867
        ResponseEntity<MetricsNumbers> resp;
868
        try {
869
            //communicate with endpoint
870
            resp = template.exchange(
871
                    builder.build().encode().toUri(),
872
                    HttpMethod.GET,
873
                    null,
874
                    new ParameterizedTypeReference<MetricsNumbers>() {
875
                    });
876
        } catch (RestClientException e) {
877 51831 panagiotis
            LOGGER.debug("Exception on getMetricsNumbers" , e);
878
            emailUtils.reportException(e);
879 50570 panagiotis
            throw e;
880
        }
881
882
        return resp.getBody();
883
    }
884
885
    private String getOpenAIREId(String repoId) {
886
887
        if (repoId != null && repoId.contains("::")) {
888
            return repoId.split("::")[0] + "::" + DigestUtils.md5Hex(repoId.split("::")[1]);
889
        }
890
891
        return null;
892
    }
893
894 50631 panagiotis
    private UriComponents searchDatasource(String page,String size){
895 50570 panagiotis
896 50631 panagiotis
        return UriComponentsBuilder
897
                .fromHttpUrl(baseAddress + "/ds/search/")
898
                .path("/{page}/{size}/")
899 53113 panagiotis
                .queryParam("requestSortBy","officialname")
900 50631 panagiotis
                .queryParam("order","ASCENDING")
901
                .build().expand(page, size).encode();
902
    }
903
904 53113 panagiotis
    private UriComponents searchSnipperDatasource(String page,String size){
905
906
        return UriComponentsBuilder
907
                .fromHttpUrl(baseAddress + "/ds/searchsnippet/")
908
                .path("/{page}/{size}/")
909
                .queryParam("requestSortBy","officialname")
910
                .queryParam("order","ASCENDING")
911
                .build().expand(page, size).encode();
912
    }
913
914 56636 antonis.le
    private UriComponents searchRegisteredDatasource(String requestSortBy, String order, String page,String size){
915
916
        return UriComponentsBuilder
917
                .fromHttpUrl(baseAddress + "/ds/searchregistered/")
918
                .path("/{page}/{size}/")
919
                .queryParam("requestSortBy",requestSortBy)
920
                .queryParam("order",order)
921
                .build().expand(page, size).encode();
922
    }
923
924 51549 panagiotis
    private String getRepositoryType(String typology){
925
        return invertedDataSourceClass.get(typology);
926
    }
927 50631 panagiotis
928 51549 panagiotis
929 49236 panagiotis
}