diff --git a/src/main/java/eu/openaire/api/controllers/DataSourceController.java b/src/main/java/eu/openaire/api/controllers/DataSourceController.java index 2ac4b1a..23ea22d 100644 --- a/src/main/java/eu/openaire/api/controllers/DataSourceController.java +++ b/src/main/java/eu/openaire/api/controllers/DataSourceController.java @@ -47,7 +47,7 @@ public class DataSourceController @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) }) @GetMapping(value = "/{id}") - public Object getById(@PathVariable @Parameter(description = "The OpenAIRE id of the data source") String id) { + public Datasource getById(@PathVariable @Parameter(description = "The OpenAIRE id of the data source") String id) { return dataSourceService.getById(id); } diff --git a/src/main/java/eu/openaire/api/controllers/OrganizationController.java b/src/main/java/eu/openaire/api/controllers/OrganizationController.java index 8d7e6f5..a4a61b6 100644 --- a/src/main/java/eu/openaire/api/controllers/OrganizationController.java +++ b/src/main/java/eu/openaire/api/controllers/OrganizationController.java @@ -47,7 +47,7 @@ public class OrganizationController @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) }) @GetMapping(value = "/{id}") - public Object getById(@PathVariable @Parameter(description = "The OpenAIRE id of the project") String id) { + public Organization getById(@PathVariable @Parameter(description = "The OpenAIRE id of the project") String id) { return organizationService.getById(id); } diff --git a/src/main/java/eu/openaire/api/controllers/ProjectController.java b/src/main/java/eu/openaire/api/controllers/ProjectController.java index 9c56dd4..4a76c11 100644 --- a/src/main/java/eu/openaire/api/controllers/ProjectController.java +++ b/src/main/java/eu/openaire/api/controllers/ProjectController.java @@ -47,7 +47,7 @@ public class ProjectController @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) }) @GetMapping(value = "/{id}") - public Object getById(@PathVariable @Parameter(description = "The OpenAIRE id of the project") String id) { + public Project getById(@PathVariable @Parameter(description = "The OpenAIRE id of the project") String id) { return projectService.getById(id); } diff --git a/src/main/java/eu/openaire/api/controllers/ResearchProductsController.java b/src/main/java/eu/openaire/api/controllers/ResearchProductsController.java index beec8e0..8b0e51d 100644 --- a/src/main/java/eu/openaire/api/controllers/ResearchProductsController.java +++ b/src/main/java/eu/openaire/api/controllers/ResearchProductsController.java @@ -47,7 +47,7 @@ public class ResearchProductsController @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) }) @GetMapping(value = "/{id}") - public Object getById(@PathVariable @Parameter(description = "The OpenAIRE id of the research product") String id) { + public GraphResult getById(@PathVariable @Parameter(description = "The OpenAIRE id of the research product") String id) { return researchProductService.getById(id); } @@ -61,7 +61,7 @@ public class ResearchProductsController @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) }) @GetMapping(value = "") - public Object search(@Valid @ParameterObject final ResearchProductsRequest request, BindingResult validationResult) { + public SearchResponse search(@Valid @ParameterObject final ResearchProductsRequest request, BindingResult validationResult) { if (validationResult.hasErrors()) { throw new BadRequestException(RequestValidator.getErrorMessage(validationResult.getAllErrors())); diff --git a/src/main/java/eu/openaire/api/mappers/response/ResponseResultsMapper.java b/src/main/java/eu/openaire/api/mappers/response/ResponseResultsMapper.java index 85e7c3b..ac840d8 100644 --- a/src/main/java/eu/openaire/api/mappers/response/ResponseResultsMapper.java +++ b/src/main/java/eu/openaire/api/mappers/response/ResponseResultsMapper.java @@ -32,4 +32,18 @@ public class ResponseResultsMapper { .map(mapFunction) .collect(Collectors.toList()); } + + public T toSingleResult(String jsonPayload, Function mapFunction) throws RuntimeException { + try { + + // Deserialize the JSON field to SolrRecord + SolrRecord solrRecord = objectMapper.readValue(jsonPayload, SolrRecord.class); + + // Apply the mapFunction to transform SolrRecord to T + return mapFunction.apply(solrRecord); + + } catch (JsonProcessingException e) { + throw new RuntimeException("Error deserializing JSON", e); + } + } } diff --git a/src/main/java/eu/openaire/api/services/DataSourceService.java b/src/main/java/eu/openaire/api/services/DataSourceService.java index 5ef0dce..2971c35 100644 --- a/src/main/java/eu/openaire/api/services/DataSourceService.java +++ b/src/main/java/eu/openaire/api/services/DataSourceService.java @@ -16,7 +16,6 @@ import lombok.SneakyThrows; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.solr.client.solrj.response.QueryResponse; -import org.apache.solr.common.SolrDocument; import org.springframework.stereotype.Service; import java.util.List; @@ -38,14 +37,14 @@ public class DataSourceService { private final Logger log = LogManager.getLogger(this.getClass()); @SneakyThrows - public SolrDocument getById(String id) { + public Datasource getById(String id) { var doc = solrRepository.getById(id); if (doc == null) { throw new NotFoundException("Data source with id: " + id + " not found"); } - return doc; + return responseResultsMapper.toSingleResult(doc.get("__json").toString(), datasourceMapper::toGraphDatasource); } @SneakyThrows diff --git a/src/main/java/eu/openaire/api/services/OrganizationService.java b/src/main/java/eu/openaire/api/services/OrganizationService.java index 3ff13c4..7980284 100644 --- a/src/main/java/eu/openaire/api/services/OrganizationService.java +++ b/src/main/java/eu/openaire/api/services/OrganizationService.java @@ -16,7 +16,6 @@ import lombok.SneakyThrows; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.solr.client.solrj.response.QueryResponse; -import org.apache.solr.common.SolrDocument; import org.springframework.stereotype.Service; import java.util.List; @@ -38,14 +37,14 @@ public class OrganizationService { private final Logger log = LogManager.getLogger(this.getClass()); @SneakyThrows - public SolrDocument getById(String id) { + public Organization getById(String id) { var doc = solrRepository.getById(id); if (doc == null) { throw new NotFoundException("Organization with id: " + id + " not found"); } - return doc; + return responseResultsMapper.toSingleResult(doc.get("__json").toString(), organizationMapper::toGraphOrganization); } @SneakyThrows diff --git a/src/main/java/eu/openaire/api/services/ProjectService.java b/src/main/java/eu/openaire/api/services/ProjectService.java index b61a125..cfd8211 100644 --- a/src/main/java/eu/openaire/api/services/ProjectService.java +++ b/src/main/java/eu/openaire/api/services/ProjectService.java @@ -16,7 +16,6 @@ import lombok.SneakyThrows; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.solr.client.solrj.response.QueryResponse; -import org.apache.solr.common.SolrDocument; import org.springframework.stereotype.Service; import java.util.List; @@ -38,14 +37,14 @@ public class ProjectService { private final Logger log = LogManager.getLogger(this.getClass()); @SneakyThrows - public SolrDocument getById(String id) { + public Project getById(String id) { var doc = solrRepository.getById(id); if (doc == null) { throw new NotFoundException("Project with id: " + id + " not found"); } - return doc; + return responseResultsMapper.toSingleResult(doc.get("__json").toString(), projectMapper::toGraphProject); } @SneakyThrows diff --git a/src/main/java/eu/openaire/api/services/ResearchProductService.java b/src/main/java/eu/openaire/api/services/ResearchProductService.java index 18005cc..b2bb7b3 100644 --- a/src/main/java/eu/openaire/api/services/ResearchProductService.java +++ b/src/main/java/eu/openaire/api/services/ResearchProductService.java @@ -16,7 +16,6 @@ import lombok.SneakyThrows; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.solr.client.solrj.response.QueryResponse; -import org.apache.solr.common.SolrDocument; import org.springframework.stereotype.Service; import java.util.List; @@ -38,14 +37,15 @@ public class ResearchProductService { private final Logger log = LogManager.getLogger(this.getClass()); @SneakyThrows - public SolrDocument getById(String id) { + public GraphResult getById(String id) { var doc = solrRepository.getById(id); if (doc == null) { throw new NotFoundException("Research product with id: " + id + " not found"); } - return doc; + return responseResultsMapper.toSingleResult(doc.get("__json").toString(), researchProductMapper::toGraphResult); + } @SneakyThrows diff --git a/src/main/java/eu/openaire/api/solr/SolrResponseBean.java b/src/main/java/eu/openaire/api/solr/SolrResponseBean.java index b76dcfd..a48c381 100644 --- a/src/main/java/eu/openaire/api/solr/SolrResponseBean.java +++ b/src/main/java/eu/openaire/api/solr/SolrResponseBean.java @@ -4,7 +4,7 @@ import lombok.Data; import org.apache.solr.client.solrj.beans.Field; @Data -public class SolrResponseBean { +public class SolrResponseBean { @Field("__json") private String jsonField;