diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/AggregationHistoryResponse.java b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationHistoryResponse.java index 0d40043..65e932d 100644 --- a/src/main/java/eu/dnetlib/repo/manager/domain/AggregationHistoryResponse.java +++ b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationHistoryResponse.java @@ -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; diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/AggregationInfo.java b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationInfo.java new file mode 100644 index 0000000..2fa6a0e --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationInfo.java @@ -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() { + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java index cdefd89..c7ff284 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java @@ -43,11 +43,11 @@ public interface RepositoryService { Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException; - List getRepositoryAggregations(String id) throws JSONException; + List getRepositoryAggregations(String id) throws JSONException; - List getRepositoryAggregations(String id, int from, int size) throws JSONException; + List getRepositoryAggregations(String id, int from, int size) throws JSONException; - Map> getRepositoryAggregationsByYear(String id) throws JSONException; + Map> getRepositoryAggregationsByYear(String id) throws JSONException; List getRepositoriesByName(String name, String page, diff --git a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java index df7daa8..66a9fb4 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java @@ -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 getRepositoriesSnippets(List ids, int page, int size) throws Exception { List 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 getRepositoryAggregations(String id) { + public List 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) rs.getAggregationInfo() : null; } @Override - public List getRepositoryAggregations(String id, int from, int size) { + public List getRepositoryAggregations(String id, int from, int size) { - List res = getRepositoryAggregations(id); + List 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> getRepositoryAggregationsByYear(String id) { + public Map> getRepositoryAggregationsByYear(String id) { LOGGER.debug("Retrieving aggregations (by year) for repository with id : " + id); - List aggregationHistory = getRepositoryAggregations(id); - Map> aggregationByYear = new HashMap<>(); - - return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory); + List aggregationHistory = getRepositoryAggregations(id); + + return aggregationHistory.isEmpty() ? new HashMap<>() : createYearMap(aggregationHistory); } - private Map> createYearMap(List aggregationHistory) { - + private Map> createYearMap(List aggregationHistory) { + aggregationHistory = aggregationHistory.stream() .sorted(Comparator.comparing(AggregationInfo::getDate).reversed()) .collect(Collectors.toList());