From caa1f1244ae9b6ac0f6aa9ad3609d8d90ee0c5b2 Mon Sep 17 00:00:00 2001 From: Konstantinos Spyrou Date: Mon, 23 Jan 2023 17:45:35 +0200 Subject: [PATCH] refactoring --- .../manager/service/RepositoryService.java | 4 +- .../service/RepositoryServiceImpl.java | 253 ++++++++---------- .../dnetlib/repo/manager/utils/Converter.java | 67 +---- 3 files changed, 122 insertions(+), 202 deletions(-) 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 ff760f9..4235064 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java @@ -50,7 +50,7 @@ public interface RepositoryService { String officialName, String requestSortBy, String order, int page, int pageSize) throws Exception; - int getTotalRegisteredRepositories(); + Integer getTotalRegisteredRepositories(); List getRepositoryInterface(String id) throws JSONException; @@ -84,7 +84,7 @@ public interface RepositoryService { MetricsInfo getMetricsInfoForRepository(String repoId) throws RepositoryServiceException; - Map getListLatestUpdate(String mode) throws JSONException; + Map getListLatestUpdate(String mode); RepositoryInterface updateRepositoryInterface(String repoId, String comment, RepositoryInterface repositoryInterface, String desiredCompatibilityLevel) throws Exception; 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 a62efd3..4ce000e 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java @@ -1,5 +1,6 @@ package eu.dnetlib.repo.manager.service; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -10,7 +11,6 @@ import eu.dnetlib.domain.functionality.validator.JobForValidation; import eu.dnetlib.repo.manager.domain.*; import eu.dnetlib.repo.manager.domain.dto.Role; import eu.dnetlib.repo.manager.domain.dto.User; -import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.exception.RepositoryServiceException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService; @@ -21,9 +21,7 @@ import eu.dnetlib.repo.manager.utils.Converter; import eu.dnetlib.repo.manager.utils.DateUtils; import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader; import org.apache.commons.codec.digest.DigestUtils; -import org.json.JSONArray; import org.json.JSONException; -import org.json.JSONObject; import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -76,8 +74,6 @@ public class RepositoryServiceImpl implements RepositoryService { @Value("${services.provide.usageStatisticsNumbersBaseURL}") private String usageStatisticsNumbersBaseURL; - private final Converter converter; - private static final Map> dataSourceClass = new HashMap<>(); private static final Map invertedDataSourceClass = new HashMap<>(); @@ -96,7 +92,7 @@ public class RepositoryServiceImpl implements RepositoryService { VocabularyLoader vocabularyLoader, RestTemplate restTemplate, ObjectMapper objectMapper, - Converter converter, +// Converter converter, @Lazy EmailUtils emailUtils, @Lazy ValidatorService validatorService, @Lazy PiWikService piWikService, @@ -107,7 +103,6 @@ public class RepositoryServiceImpl implements RepositoryService { this.authoritiesUpdater = authoritiesUpdater; this.vocabularyLoader = vocabularyLoader; this.piWikService = piWikService; - this.converter = converter; this.emailUtils = emailUtils; this.validatorService = validatorService; this.restTemplate = restTemplate; @@ -174,21 +169,18 @@ public class RepositoryServiceImpl implements RepositoryService { // and the "requestFilter.setId(repoId)" should return only one result at a time, thus, // another way for paging must be implemented. @Override - public List getRepositories(List ids, int page, int size) throws JSONException { - List repos = new ArrayList<>(); + public List getRepositories(List ids, int page, int size) { logger.debug("Retrieving repositories with ids : {}", String.join(", ", ids)); - UriComponents uriComponents = searchDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); + UriComponents uriComponents = searchDatasourceUri(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); RequestFilter requestFilter = new RequestFilter(); + List repos = new ArrayList<>(); + for (String repoId : ids) { requestFilter.setId(repoId); - List rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, List.class); - -// repos.addAll(converter.toRepositoryList(new JSONObject(rs))); + repos.addAll(searchDatasource(uriComponents, requestFilter, Repository.class).getResults()); } - // TODO - "repos" is EMPTY!! - for (Repository r : repos) r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId())); return repos; @@ -207,31 +199,30 @@ public class RepositoryServiceImpl implements RepositoryService { // another way for paging must be implemented. @Override public List getRepositoriesSnippets(List ids, int page, int size) throws Exception { - List resultSet = new ArrayList<>(); + List resultSet; + List datasourceDetailsList = new ArrayList<>(); // here page should be 0 - UriComponents uriComponents = searchSnippetDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); + UriComponents uriComponents = searchDatasourceSnippetUri(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); RequestFilter requestFilter = new RequestFilter(); - try { - for (String repoId : ids) { - requestFilter.setId(repoId); + for (String repoId : ids) { + requestFilter.setId(repoId); - DatasourceResponse rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class); - if (rs == null) { - logger.error("The \"DatasourceResponse\" is null!"); - return null; - } - - resultSet.addAll(objectMapper.readValue(objectMapper.writeValueAsString(rs.getDatasourceInfo()), - objectMapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); + DatasourceResponse rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class); + if (rs == null) { + logger.error("The \"DatasourceResponse\" is null!"); + } else { + datasourceDetailsList.addAll(rs.getDatasourceInfo()); } - } catch (Exception e) { - logger.debug("Exception on getRepositoriesSnippetOfUser", e); - throw e; } - logger.debug("resultSet: {}", resultSet); + resultSet = objectMapper.readValue(objectMapper.writeValueAsString(datasourceDetailsList), + objectMapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)); + + if (logger.isDebugEnabled()) { + logger.debug("resultSet: {}", objectMapper.writeValueAsString(resultSet)); + } resultSet.parallelStream().forEach(repositorySnippet -> { repositorySnippet.setPiwikInfo(piWikService.getPiwikSiteForRepo(repositorySnippet.getId())); }); @@ -242,13 +233,10 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public List getRepositoriesByCountry(String country, String mode, - Boolean managed) throws JSONException, IOException { - + Boolean managed) throws IOException { logger.debug("Getting repositories by country!"); int page = 0; int size = 100; - List resultSet = new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); String filterKey = "UNKNOWN"; if (mode.equalsIgnoreCase("repository")) @@ -259,17 +247,12 @@ public class RepositoryServiceImpl implements RepositoryService { logger.debug("Country code equals : {} | Filter mode equals : {}", country, filterKey); - UriComponents uriComponents = searchSnippetDatasource(String.valueOf(page), String.valueOf(size)); + UriComponents uriComponents = searchDatasourceSnippetUri(String.valueOf(page), String.valueOf(size)); RequestFilter requestFilter = new RequestFilter(); requestFilter.setCountry(country); requestFilter.setEoscDatasourceType(filterKey); - Map rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, Map.class); - if (rs != null) { - resultSet.addAll(mapper.readValue(mapper.writeValueAsString(rs.get("datasourceInfo")), - mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); - } - return resultSet; + return searchDatasource(uriComponents, requestFilter, RepositorySnippet.class).getResults(); } public List searchRegisteredRepositories(String country, String typology, String englishName, @@ -277,10 +260,7 @@ public class RepositoryServiceImpl implements RepositoryService { logger.debug("Searching registered repositories"); - Paging snippets = null; - ObjectMapper mapper = new ObjectMapper(); - - UriComponents uriComponents = searchRegisteredDatasource(requestSortBy, order, Integer.toString(page), Integer.toString(pageSize)); + UriComponents uriComponents = searchRegisteredDatasourceUri(requestSortBy, order, Integer.toString(page), Integer.toString(pageSize)); RequestFilter requestFilter = new RequestFilter(); requestFilter.setCountry(country); @@ -288,27 +268,12 @@ public class RepositoryServiceImpl implements RepositoryService { requestFilter.setOfficialname(officialName); requestFilter.setEnglishname(englishName); - try { - Map rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, Map.class); - if (rs == null) { - logger.error("DSM response is null : [url={}]", uriComponents.toUri()); - } else { - Header header = mapper.readValue(mapper.writeValueAsString(rs.get("header")), Header.class); - snippets = Paging.of(header, - mapper.readValue( - mapper.writeValueAsString(rs.get("datasourceInfo")), - mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); - } - - } catch (Exception e) { - logger.error("Error searching registered datasources", e); - throw e; - } + Paging snippets = searchDatasource(uriComponents, requestFilter, RepositorySnippet.class); return snippets != null ? snippets.getResults() : null; // TODO: return paging when ui is compatible } @Override - public int getTotalRegisteredRepositories() throws NullPointerException { + public Integer getTotalRegisteredRepositories() throws NullPointerException { UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/countregistered") .queryParam("fromDate", "1900-01-01") @@ -317,7 +282,7 @@ public class RepositoryServiceImpl implements RepositoryService { return restTemplate.getForObject(uriComponents.toUri(), Integer.class); } - private Repository updateRepositoryInfo(Repository r) throws JSONException { + private Repository updateRepositoryInfo(Repository r) { r.setInterfaces(this.getRepositoryInterface(r.getId())); r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId())); return r; @@ -363,57 +328,35 @@ public class RepositoryServiceImpl implements RepositoryService { } @Override - public RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException { + public RepositorySnippet getRepositorySnippetById(String id) throws ResourceNotFoundException { logger.debug("Retrieving repositories with id : {}", id); - RepositorySnippet repo; - UriComponents uriComponents = searchSnippetDatasource("0", "100"); + UriComponents uriComponents = searchDatasourceSnippetUri("0", "100"); RequestFilter requestFilter = new RequestFilter(); requestFilter.setId(id); - String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); - if (rs == null) { - logger.error("The result is null!"); - return null; - } + List repositories = searchDatasource(uriComponents, requestFilter, RepositorySnippet.class).getResults(); - JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); - - if (jsonArray.length() == 0) + if (repositories.isEmpty()) throw new ResourceNotFoundException(); - repo = converter.toRepositorySnippet(jsonArray.getJSONObject(0)); - return repo; + return repositories.get(0); } @Override - public Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException { + public Repository getRepositoryById(String id) throws ResourceNotFoundException { logger.debug("Retrieving repositories with id : {}", id); - Repository repo; - UriComponents uriComponents = searchDatasource("0", "100"); + + UriComponents uriComponents = searchDatasourceUri("0", "100"); RequestFilter requestFilter = new RequestFilter(); requestFilter.setId(id); -// String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); -// JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); - - DatasourceResponse response; - response = restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class); - if (response == null) { - logger.error("The response is null!"); - return null; - } - - List datasources = response.getDatasourceInfo(); - if (datasources.size() == 0) + List datasources = searchDatasource(uriComponents, requestFilter, Repository.class).getResults(); + if (datasources.isEmpty()) throw new ResourceNotFoundException(); -// repo = converter.toRepository(jsonArray.getJSONObject(0)); -// return updateRepositoryInfo(repo); - - return updateRepositoryInfo(converter.toRepository(datasources.get(0))); - + return updateRepositoryInfo(datasources.get(0)); } @@ -423,12 +366,11 @@ public class RepositoryServiceImpl implements RepositoryService { String size) throws JSONException { logger.debug("Retrieving repositories with official name : {}", name); - UriComponents uriComponents = searchDatasource("0", "100"); + UriComponents uriComponents = searchDatasourceUri("0", "100"); RequestFilter requestFilter = new RequestFilter(); requestFilter.setOfficialname(name); - String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); - List repos = converter.toRepositoryList(new JSONObject(rs)); + List repos = searchDatasource(uriComponents, requestFilter, Repository.class).getResults(); for (Repository r : repos) updateRepositoryInfo(r); return repos; @@ -436,33 +378,27 @@ public class RepositoryServiceImpl implements RepositoryService { } @Override - public List getRepositoryInterface(String id) throws JSONException { + public List getRepositoryInterface(String id) { UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/api/") .path("/{id}") .build().expand(id).encode(); -// String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); - ApiDetailsResponse rs = restTemplate.getForObject(uriComponents.toUri(), ApiDetailsResponse.class); - if (rs == null) { - logger.error("The ApiDetailsResponse was null!"); - return null; - } + List rs = searchApi(uriComponents, null, RepositoryInterface.class).getResults(); // TODO STOP FILTERING OUT "sword", "rest" AND FIX UI! - List res = new ArrayList<>(); - - for (ApiDetails det : rs.getApi()) { + List repositoryInterfaces = new ArrayList<>(); + for (RepositoryInterface det : rs) { String protocol = det.getProtocol(); if (!protocol.equals("sword") && !protocol.equals("rest") && !protocol.equals("ftp")) { - res.add(det); + repositoryInterfaces.add(det); } } - return converter.toRepositoryInterfaceList(res); + return repositoryInterfaces; } @Override @@ -528,17 +464,13 @@ public class RepositoryServiceImpl implements RepositoryService { } /* update method acting as add -> send email with registration topic/body*/ - private Repository latentUpdate(Repository repository, Authentication authentication) throws Exception { + private Repository latentUpdate(Repository repository, Authentication authentication) { UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/update/") .build() .encode(); - // FIXME: problematic -// String json_repository = converter.toJson(repository); -// logger.debug("JSON to add(update) -> " + json_repository); - - HttpEntity httpEntity = new HttpEntity<>(repository, httpHeaders); // TODO: check if it works (Repository contains extra fields) + HttpEntity httpEntity = new HttpEntity<>(repository, httpHeaders); ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { @@ -560,10 +492,6 @@ public class RepositoryServiceImpl implements RepositoryService { .build() .encode(); - // FIXME: problematic -// String json_repository = converter.toJson(repository); -// logger.debug("JSON to update -> " + json_repository); - HttpEntity httpEntity = new HttpEntity<>(repository, httpHeaders); ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); @@ -591,7 +519,6 @@ public class RepositoryServiceImpl implements RepositoryService { .fromHttpUrl(baseAddress + "/ds/add/") .build() .encode(); -// String json_repository = converter.toJson(repository); HttpEntity httpEntity = new HttpEntity<>(repository, httpHeaders); ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); @@ -624,8 +551,7 @@ public class RepositoryServiceImpl implements RepositoryService { String desiredCompatibilityLevel) throws Exception { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Repository e = this.getRepositoryById(repoId); - repositoryInterface = createRepositoryInterface(e, repositoryInterface, datatype); -// String json_interface = converter.toJson(e, repositoryInterface); + repositoryInterface = fillInterfaceFields(e, repositoryInterface, datatype); UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/api/add/") @@ -633,9 +559,11 @@ public class RepositoryServiceImpl implements RepositoryService { .encode(); HttpEntity httpEntity = new HttpEntity<>(repositoryInterface, httpHeaders); - restTemplate.postForObject(uriComponents.toUri(), httpEntity, String.class); + // Explicitly update validation set (updating the interface does not allow updating the set value) + this.updateValidationSet(repoId, repositoryInterface.getId(), repositoryInterface.getAccessSet()); + emailUtils.sendAdminRegisterInterfaceEmail(e, comment, repositoryInterface, authentication); emailUtils.sendUserRegisterInterfaceEmail(e, comment, repositoryInterface, authentication); @@ -709,12 +637,11 @@ public class RepositoryServiceImpl implements RepositoryService { this.validatorService.submitJobForValidation(job); } - private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) { + private RepositoryInterface fillInterfaceFields(Repository repo, RepositoryInterface iFace, String datatype) { iFace.setDatasource(repo.getId()); iFace.setContentdescription("metadata"); - // TODO: double check me logger.warn("Compatibility level: {}", iFace.getCompatibility()); if (iFace.getCompatibility() == null || iFace.getCompatibility().equals("")) { iFace.setCompatibility("UNKNOWN"); @@ -726,7 +653,7 @@ public class RepositoryServiceImpl implements RepositoryService { iFace.setAccessFormat("oai_dc"); - // FIXME: this will probably not work + // TODO: is this the correct functionality? if (repo.getEoscDatasourceType() != null && !repo.getEoscDatasourceType().isEmpty()) iFace.setTypology(repo.getEoscDatasourceType()); else if (datatype.equalsIgnoreCase("journal")) @@ -752,18 +679,18 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public List getDnetCountries() { logger.debug("Getting dnet-countries!"); - return converter.readFile("countries.txt"); + return Converter.readFile("countries.txt"); } @Override public List getTypologies() { - return converter.readFile("typologies.txt"); + return Converter.readFile("typologies.txt"); } @Override public List getTimezones() { - List timezones = converter.readFile("timezones.txt"); - return converter.toTimezones(timezones); + List timezones = Converter.readFile("timezones.txt"); + return Converter.toTimezones(timezones); } @Override @@ -894,13 +821,13 @@ public class RepositoryServiceImpl implements RepositoryService { } @Override - public Map getListLatestUpdate(String mode) throws JSONException { + public Map getListLatestUpdate(String mode) { Map dates = new HashMap<>(); if (mode.equals("repository")) { dates.put("opendoar", DateUtils.toString(getRepositoryInterface("openaire____::opendoar").get(0).getLastCollectionDate())); dates.put("fairsharing", DateUtils.toString(getRepositoryInterface("openaire____::fairsharing").get(0).getLastCollectionDate())); // create re3data last collection date -// dates.put("re3data", converter.toString(getRepositoryInterface("openaire____::re3data").get(1).getLastCollectionDate())); +// dates.put("re3data", Converter.toString(getRepositoryInterface("openaire____::re3data").get(1).getLastCollectionDate())); List re3interfaces = getRepositoryInterface("openaire____::re3data"); String re3Date = null; for (RepositoryInterface interf : re3interfaces) { @@ -955,7 +882,7 @@ public class RepositoryServiceImpl implements RepositoryService { restTemplate.postForObject(uriComponents.toUri(), null, String.class); } - private MetricsNumbers getMetricsNumbers(String openAIREID) throws BrokerException { + private MetricsNumbers getMetricsNumbers(String openAIREID) { //build the uri params UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usageStatisticsNumbersBaseURL + openAIREID + "/clicks"); @@ -986,7 +913,7 @@ public class RepositoryServiceImpl implements RepositoryService { } - private UriComponents searchDatasource(String page, String size) { + private UriComponents searchDatasourceUri(String page, String size) { return UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/searchdetails/") @@ -996,7 +923,7 @@ public class RepositoryServiceImpl implements RepositoryService { .build().expand(page, size).encode(); } - private UriComponents searchSnippetDatasource(String page, String size) { + private UriComponents searchDatasourceSnippetUri(String page, String size) { return UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/searchsnippet/") @@ -1006,7 +933,7 @@ public class RepositoryServiceImpl implements RepositoryService { .build().expand(page, size).encode(); } - private UriComponents searchRegisteredDatasource(String requestSortBy, String order, String page, String size) { + private UriComponents searchRegisteredDatasourceUri(String requestSortBy, String order, String page, String size) { return UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/searchregistered/") @@ -1016,6 +943,52 @@ public class RepositoryServiceImpl implements RepositoryService { .build().expand(page, size).encode(); } + private Paging searchDatasource(UriComponents uriComponents, RequestFilter requestFilter, Class clazz) { + Paging repositories = new Paging<>(); + ResponseEntity rs; + rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, new HttpEntity<>(requestFilter), Map.class); + if (!rs.getStatusCode().is2xxSuccessful()) { + logger.error("Api call not successful. Code: {} | Body: {}", rs.getStatusCode(), rs.getBody()); + } + if (rs.getBody() == null) { + logger.error("DSM response is null : [url={}]", uriComponents.toUri()); + } else { + try { + Header header = objectMapper.readValue(objectMapper.writeValueAsString(rs.getBody().get("header")), Header.class); + repositories = Paging.of(header, + objectMapper.readValue( + objectMapper.writeValueAsString(rs.getBody().get("datasourceInfo")), + objectMapper.getTypeFactory().constructCollectionType(List.class, clazz))); + } catch (JsonProcessingException e) { + logger.error("Error in objectMapper", e); + } + } + return repositories; + } + + private Paging searchApi(UriComponents uriComponents, RequestFilter requestFilter, Class clazz) { + Paging repositories = new Paging<>(); + ResponseEntity rs; + rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, new HttpEntity<>(requestFilter), Map.class); + if (!rs.getStatusCode().is2xxSuccessful()) { + logger.error("Api call not successful. Code: {} | Body: {}", rs.getStatusCode(), rs.getBody()); + } + if (rs.getBody() == null) { + logger.error("DSM response is null : [url={}]", uriComponents.toUri()); + } else { + try { + Header header = objectMapper.readValue(objectMapper.writeValueAsString(rs.getBody().get("header")), Header.class); + repositories = Paging.of(header, + objectMapper.readValue( + objectMapper.writeValueAsString(rs.getBody().get("api")), + objectMapper.getTypeFactory().constructCollectionType(List.class, clazz))); + } catch (JsonProcessingException e) { + logger.error("Error in objectMapper", e); + } + } + return repositories; + } + private String getRepositoryType(String typology) { return invertedDataSourceClass.get(typology); } diff --git a/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java b/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java index 38f5e2e..3dccc4e 100644 --- a/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java +++ b/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java @@ -1,15 +1,9 @@ package eu.dnetlib.repo.manager.utils; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import eu.dnetlib.repo.manager.domain.*; +import eu.dnetlib.repo.manager.domain.Timezone; import org.apache.commons.codec.digest.DigestUtils; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; import java.io.BufferedReader; import java.io.InputStream; @@ -18,61 +12,14 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -@Component public class Converter { private static final Logger logger = LoggerFactory.getLogger(Converter.class); - private final ObjectMapper objectMapper; - - public Converter() { - objectMapper = new ObjectMapper()/*.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)*/; - } - public Repository toRepository(Object repositoryObject) { - - Repository repository = objectMapper.convertValue(repositoryObject, Repository.class); - - return repository; + private Converter() { } - public RepositorySnippet toRepositorySnippet(JSONObject repositorySnippetObject) { - - RepositorySnippet snippet = objectMapper.convertValue(repositorySnippetObject, RepositorySnippet.class); - - return snippet; - } - - public List toRepositoryList(JSONObject json) throws JSONException { - - List resultSet = new ArrayList<>(); - JSONArray rs = json.getJSONArray("datasourceInfo"); - for (int i = 0; i < rs.length(); i++) - resultSet.add(toRepository(rs.getJSONObject(i))); - return resultSet; - } - - public List toRepositoryInterfaceList(List apiDetailsList) { - - List resultSet = new ArrayList<>(); - - for (ApiDetails entry : apiDetailsList) - resultSet.add(toRepositoryInterface(entry)); - return resultSet; - } - - public RepositoryInterface toRepositoryInterface(Object repositoryInterfaceObject) { - - RepositoryInterface repoInterface = objectMapper.convertValue(repositoryInterfaceObject, RepositoryInterface.class); - - return repoInterface; - } - - public String toJson(Repository repository) throws JsonProcessingException { - - return objectMapper.writeValueAsString(repository); - } - - public List readFile(String filename) { + public static List readFile(String filename) { String line; List list = new ArrayList<>(); try { @@ -88,7 +35,7 @@ public class Converter { return list; // It may be empty. } - public List toTimezones(List timezones) { + public static List toTimezones(List timezones) { List tmz = new ArrayList<>(); for (String t : timezones) { @@ -98,17 +45,17 @@ public class Converter { return tmz; } - private String getOpenaireId(String repositoryId) { + public static String getOpenaireId(String repositoryId) { if (repositoryId != null && repositoryId.contains("::")) return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]); return null; } - private Boolean convertStringToBoolean(String value) { + public static Boolean convertStringToBoolean(String value) { return value.equals("null") ? null : Boolean.valueOf(value); } - private Double toDouble(String number) { + public static Double toDouble(String number) { if (Objects.equals(number, "null")) return 0.0; else