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