chenged methods retrieving AggregationInfo

This commit is contained in:
Konstantinos Spyrou 2022-12-20 20:40:37 +02:00
parent 6c2780927d
commit a5a4924c08
4 changed files with 35 additions and 22 deletions

View File

@ -1,7 +1,6 @@
package eu.dnetlib.repo.manager.domain;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import eu.dnetlib.enabling.datasources.common.AggregationInfo;
import java.util.List;

View File

@ -0,0 +1,8 @@
package eu.dnetlib.repo.manager.domain;
// TODO: this class must be replaced by common dnet class (CollectionInfo / TransformationInfo / AggregationInfo)
public class AggregationInfo extends eu.dnetlib.enabling.datasources.common.AggregationInfo {
public AggregationInfo() {
}
}

View File

@ -43,11 +43,11 @@ public interface RepositoryService {
Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException;
List<AggregationInfo> getRepositoryAggregations(String id) throws JSONException;
<T extends AggregationInfo> List<T> getRepositoryAggregations(String id) throws JSONException;
List<AggregationInfo> getRepositoryAggregations(String id, int from, int size) throws JSONException;
<T extends AggregationInfo> List<T> getRepositoryAggregations(String id, int from, int size) throws JSONException;
Map<String, List<AggregationInfo>> getRepositoryAggregationsByYear(String id) throws JSONException;
<T extends AggregationInfo> Map<String, List<T>> getRepositoryAggregationsByYear(String id) throws JSONException;
List<Repository> getRepositoriesByName(String name,
String page,

View File

@ -1,6 +1,10 @@
package eu.dnetlib.repo.manager.service;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -9,6 +13,7 @@ import eu.dnetlib.domain.enabling.Vocabulary;
import eu.dnetlib.domain.functionality.validator.JobForValidation;
import eu.dnetlib.enabling.datasources.common.AggregationInfo;
import eu.dnetlib.repo.manager.domain.*;
import eu.dnetlib.repo.manager.domain.broker.BrowseEntry;
import eu.dnetlib.repo.manager.domain.dto.Role;
import eu.dnetlib.repo.manager.domain.dto.User;
import eu.dnetlib.repo.manager.exception.BrokerException;
@ -60,6 +65,7 @@ public class RepositoryServiceImpl implements RepositoryService {
private final AaiRegistryService registryCalls;
private final AuthoritiesUpdater authoritiesUpdater;
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper;
private final VocabularyLoader vocabularyLoader;
private final PiWikService piWikService;
private final EmailUtils emailUtils;
@ -96,6 +102,7 @@ public class RepositoryServiceImpl implements RepositoryService {
AuthoritiesUpdater authoritiesUpdater,
VocabularyLoader vocabularyLoader,
RestTemplate restTemplate,
ObjectMapper objectMapper,
Converter converter,
@Lazy EmailUtils emailUtils,
@Lazy ValidatorService validatorService,
@ -110,6 +117,7 @@ public class RepositoryServiceImpl implements RepositoryService {
this.emailUtils = emailUtils;
this.validatorService = validatorService;
this.restTemplate = restTemplate;
this.objectMapper = objectMapper;
}
@PostConstruct
@ -161,7 +169,7 @@ public class RepositoryServiceImpl implements RepositoryService {
.build().encode();
return restTemplate.getForObject(uriComponents.toUri(), Country[].class);
}
// FIXME: with the new roles of the users the "requestFilter.setRegisteredby(userEmail)" can no longer be used
// and the "requestFilter.setId(repoId)" should return only one result at a time, thus,
// another way for paging must be implemented.
@ -206,7 +214,6 @@ public class RepositoryServiceImpl implements RepositoryService {
@Override
public List<RepositorySnippet> getRepositoriesSnippets(List<String> ids, int page, int size) throws Exception {
List<RepositorySnippet> resultSet = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
// here page should be 0
UriComponents uriComponents = searchSnipperDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size)));
@ -216,10 +223,10 @@ public class RepositoryServiceImpl implements RepositoryService {
for (String repoId : ids) {
requestFilter.setId(repoId);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),
mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
DatasourceResponse rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class);
resultSet.addAll(objectMapper.readValue(objectMapper.writeValueAsString(rs.getDatasourceInfo()),
objectMapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
}
} catch (Exception e) {
LOGGER.debug("Exception on getRepositoriesSnippetOfUser", e);
@ -412,36 +419,35 @@ public class RepositoryServiceImpl implements RepositoryService {
@Override
public List<AggregationInfo> getRepositoryAggregations(String id) {
public <T extends AggregationInfo> List<T> getRepositoryAggregations(String id) {
LOGGER.debug("Retrieving aggregations for repository with id : " + id);
UriComponents uriComponents = getAggregationHistory(id);
AggregationHistoryResponse rs = restTemplate.getForObject(uriComponents.toUri(), AggregationHistoryResponse.class);
return rs != null ? rs.getAggregationInfo() : null;
return rs != null ? (List<T>) rs.getAggregationInfo() : null;
}
@Override
public List<AggregationInfo> getRepositoryAggregations(String id, int from, int size) {
public <T extends AggregationInfo> List<T> getRepositoryAggregations(String id, int from, int size) {
List<AggregationInfo> res = getRepositoryAggregations(id);
List<T> res = getRepositoryAggregations(id);
return res.subList(from, Math.min(from + size, res.size()));
return res.subList(from, Math.min(from + size, res.size()));
}
@Override
public Map<String, List<AggregationInfo>> getRepositoryAggregationsByYear(String id) {
public <T extends AggregationInfo> Map<String, List<T>> getRepositoryAggregationsByYear(String id) {
LOGGER.debug("Retrieving aggregations (by year) for repository with id : " + id);
List<AggregationInfo> aggregationHistory = getRepositoryAggregations(id);
Map<String, List<AggregationInfo>> aggregationByYear = new HashMap<>();
return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory);
List<T> aggregationHistory = getRepositoryAggregations(id);
return aggregationHistory.isEmpty() ? new HashMap<>() : createYearMap(aggregationHistory);
}
private Map<String, List<AggregationInfo>> createYearMap(List<AggregationInfo> aggregationHistory) {
private <T extends AggregationInfo> Map<String, List<T>> createYearMap(List<T> aggregationHistory) {
aggregationHistory = aggregationHistory.stream()
.sorted(Comparator.comparing(AggregationInfo::getDate).reversed())
.collect(Collectors.toList());