Revision 62272
Added by Antonis Lempesis almost 2 years ago
RepositoryServiceImpl.java | ||
---|---|---|
105 | 105 |
this.restTemplate = restTemplate; |
106 | 106 |
} |
107 | 107 |
|
108 |
private String getAuthenticatedUserEmail() { |
|
109 |
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); |
|
110 |
return authenticationToken.getUserInfo().getEmail(); |
|
111 |
} |
|
112 |
|
|
113 |
|
|
114 | 108 |
@PostConstruct |
115 | 109 |
private void init() { |
116 | 110 |
LOGGER.debug("Initialization method of repository api!"); |
... | ... | |
130 | 124 |
dataSourceClass.get("opendoar").add(key); |
131 | 125 |
} else if (key.contains("datarepository")) { |
132 | 126 |
dataSourceClass.putIfAbsent("re3data", Collections.singletonList("datarepository::unknown")); |
133 |
} else if (key.contains("pubsrepository::journal")) { |
|
134 |
dataSourceClass.putIfAbsent("journal", Collections.singletonList("pubsrepository::journal")); |
|
135 | 127 |
} |
136 | 128 |
} |
137 | 129 |
|
... | ... | |
177 | 169 |
@Override |
178 | 170 |
public List<Repository> getRepositories(List<String> ids, int page, int size) throws JSONException { |
179 | 171 |
List<Repository> repos = new ArrayList<>(); |
180 |
LOGGER.debug("Retreiving repositories with ids : " + String.join(", ", ids));
|
|
172 |
LOGGER.debug("Retrieving repositories with ids : " + String.join(", ", ids));
|
|
181 | 173 |
UriComponents uriComponents = searchDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); |
182 | 174 |
RequestFilter requestFilter = new RequestFilter(); |
183 | 175 |
|
... | ... | |
331 | 323 |
return r; |
332 | 324 |
} |
333 | 325 |
|
334 |
|
|
335 |
private Collection<Repository> getRepositoriesByMode(String mode, List<Repository> rs) { |
|
336 |
|
|
337 |
List<Repository> reps = new ArrayList<>(); |
|
338 |
for (Repository r : rs) { |
|
339 |
if (r.getCollectedFrom() != null && r.getCollectedFrom().equals(mode)) |
|
340 |
reps.add(r); |
|
341 |
|
|
342 |
} |
|
343 |
return reps; |
|
344 |
} |
|
345 |
|
|
346 | 326 |
@Override |
347 | 327 |
public List<Repository> getRepositoriesOfUser(String page, String size) throws JSONException { |
348 | 328 |
String userEmail = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(); |
349 |
LOGGER.debug("Retreiving repositories of authenticated user : " + userEmail);
|
|
329 |
LOGGER.debug("Retrieving repositories of authenticated user : " + userEmail);
|
|
350 | 330 |
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles()); |
351 | 331 |
return getRepositories(new ArrayList<>(repoIds)); |
352 | 332 |
} |
353 | 333 |
|
354 | 334 |
@Override |
355 | 335 |
public List<Repository> getRepositoriesOfUser(String userEmail, String page, String size) throws JSONException { |
356 |
LOGGER.debug("Retreiving repositories of authenticated user : " + userEmail);
|
|
336 |
LOGGER.debug("Retrieving repositories of authenticated user : " + userEmail);
|
|
357 | 337 |
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles(userEmail)); |
358 | 338 |
return getRepositories(new ArrayList<>(repoIds)); |
359 | 339 |
} |
... | ... | |
373 | 353 |
@Override |
374 | 354 |
public RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException { |
375 | 355 |
|
376 |
LOGGER.debug("Retreiving repositories with id : " + id);
|
|
356 |
LOGGER.debug("Retrieving repositories with id : " + id);
|
|
377 | 357 |
RepositorySnippet repo = null; |
378 | 358 |
UriComponents uriComponents = searchSnipperDatasource("0", "100"); |
379 | 359 |
RequestFilter requestFilter = new RequestFilter(); |
... | ... | |
392 | 372 |
@Override |
393 | 373 |
public Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException { |
394 | 374 |
|
395 |
LOGGER.debug("Retreiving repositories with id : " + id);
|
|
375 |
LOGGER.debug("Retrieving repositories with id : " + id);
|
|
396 | 376 |
Repository repo = null; |
397 | 377 |
UriComponents uriComponents = searchDatasource("0", "100"); |
398 | 378 |
RequestFilter requestFilter = new RequestFilter(); |
... | ... | |
410 | 390 |
|
411 | 391 |
|
412 | 392 |
@Override |
413 |
public List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException {
|
|
393 |
public List<AggregationDetails> getRepositoryAggregations(String id) throws JSONException { |
|
414 | 394 |
|
415 |
LOGGER.debug("Retreiving aggregations for repository with id : " + id); |
|
416 |
UriComponents uriComponents = searchDatasource(from + "", size + ""); |
|
417 |
RequestFilter requestFilter = new RequestFilter(); |
|
418 |
requestFilter.setId(id); |
|
395 |
LOGGER.debug("Retrieving aggregations for repository with id : " + id); |
|
396 |
UriComponents uriComponents = getAggregationHistory(id); |
|
419 | 397 |
|
420 |
List<AggregationDetails> aggregationHistory = new ArrayList<>(); |
|
398 |
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); |
|
399 |
JSONArray aggregationInfo = new JSONObject(rs).getJSONArray("aggregationInfo"); |
|
421 | 400 |
|
422 |
long start = System.currentTimeMillis(); |
|
423 |
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); |
|
424 |
long end = System.currentTimeMillis(); |
|
401 |
List<AggregationDetails> aggregationHistory = new ArrayList<>(Converter.getAggregationHistoryFromJson(aggregationInfo)); |
|
425 | 402 |
|
426 |
System.out.println("Aggregations request through rest template took " + (end - start) + "ms"); |
|
427 |
JSONObject repository = new JSONObject(rs); |
|
403 |
return aggregationHistory; |
|
404 |
// return aggregationHistory.size() == 0 ? aggregationHistory : aggregationHistory.stream() |
|
405 |
// .sorted(Comparator.comparing(AggregationDetails::getDate).reversed()) |
|
406 |
// .collect(Collectors.toList()); |
|
407 |
} |
|
428 | 408 |
|
429 |
if (repository.getJSONArray("datasourceInfo").length() == 0)
|
|
430 |
return aggregationHistory;
|
|
409 |
@Override
|
|
410 |
public List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException {
|
|
431 | 411 |
|
432 |
start = System.currentTimeMillis(); |
|
433 |
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); |
|
434 |
end = System.currentTimeMillis(); |
|
435 |
|
|
436 |
System.out.println("Getting aggregations history from json " + (end - start) + "ms"); |
|
437 |
return aggregationHistory.size() == 0 ? aggregationHistory : aggregationHistory.stream() |
|
438 |
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed()) |
|
439 |
.limit(size) |
|
440 |
.collect(Collectors.toList()); |
|
441 |
|
|
412 |
return getRepositoryAggregations(id).subList(from, from + size); |
|
442 | 413 |
} |
443 | 414 |
|
444 | 415 |
@Override |
445 | 416 |
public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException { |
446 |
LOGGER.debug("Retreiving aggregations (by year) for repository with id : " + id); |
|
447 |
UriComponents uriComponents = searchDatasource("0", "100"); |
|
448 |
RequestFilter requestFilter = new RequestFilter(); |
|
449 |
requestFilter.setId(id); |
|
417 |
LOGGER.debug("Retrieving aggregations (by year) for repository with id : " + id); |
|
450 | 418 |
|
451 |
List<AggregationDetails> aggregationHistory = new ArrayList<>();
|
|
419 |
List<AggregationDetails> aggregationHistory = getRepositoryAggregations(id);
|
|
452 | 420 |
Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>(); |
453 |
|
|
454 |
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); |
|
455 |
JSONObject repository = new JSONObject(rs); |
|
456 |
|
|
457 |
if (repository.getJSONArray("datasourceInfo").length() == 0) |
|
458 |
return aggregationByYear; |
|
459 |
|
|
460 |
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); |
|
421 |
|
|
461 | 422 |
return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory); |
462 | 423 |
} |
463 | 424 |
|
464 | 425 |
private Map<String, List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) { |
465 |
Map<String, List<AggregationDetails>> aggregationByYear; |
|
426 |
|
|
466 | 427 |
aggregationHistory = aggregationHistory.stream() |
467 | 428 |
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed()) |
468 | 429 |
.collect(Collectors.toList()); |
... | ... | |
477 | 438 |
String page, |
478 | 439 |
String size) throws JSONException { |
479 | 440 |
|
480 |
LOGGER.debug("Retreiving repositories with official name : " + name);
|
|
441 |
LOGGER.debug("Retrieving repositories with official name : " + name);
|
|
481 | 442 |
UriComponents uriComponents = searchDatasource("0", "100"); |
482 | 443 |
RequestFilter requestFilter = new RequestFilter(); |
483 | 444 |
requestFilter.setOfficialname(name); |
... | ... | |
571 | 532 |
String json_repository = Converter.repositoryObjectToJson(repository); |
572 | 533 |
LOGGER.debug("JSON to add(update) -> " + json_repository); |
573 | 534 |
|
574 |
HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
|
|
535 |
HttpEntity<String> httpEntity = new HttpEntity<>(json_repository, httpHeaders); |
|
575 | 536 |
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); |
576 | 537 |
|
577 | 538 |
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { |
... | ... | |
1000 | 961 |
return null; |
1001 | 962 |
} |
1002 | 963 |
|
964 |
private UriComponents getAggregationHistory(String repoId) { |
|
965 |
return UriComponentsBuilder |
|
966 |
.fromHttpUrl(baseAddress + "/ds/aggregationhistory/") |
|
967 |
.path("/repoId") |
|
968 |
.build().expand(repoId).encode(); |
|
969 |
} |
|
970 |
|
|
1003 | 971 |
private UriComponents searchDatasource(String page, String size) { |
1004 | 972 |
|
1005 | 973 |
return UriComponentsBuilder |
Also available in: Unified diff
getting aggregations uising the correct dsm api method