From b68ffc2cdf02db815de1d0c827ceb14d8d89770d Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Mon, 9 Dec 2024 10:56:47 +0100 Subject: [PATCH] added a method to retrieve a record from the index --- .../controllers/LegacyApiController.java | 32 +++++++++++++++---- .../directindex/mapping/SolrRecordMapper.java | 8 +++-- .../service/DirectIndexService.java | 7 ++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/eu/dnetlib/app/directindex/controllers/LegacyApiController.java b/src/main/java/eu/dnetlib/app/directindex/controllers/LegacyApiController.java index 9ee1e46..bbad521 100644 --- a/src/main/java/eu/dnetlib/app/directindex/controllers/LegacyApiController.java +++ b/src/main/java/eu/dnetlib/app/directindex/controllers/LegacyApiController.java @@ -1,5 +1,8 @@ package eu.dnetlib.app.directindex.controllers; +import java.util.LinkedHashMap; +import java.util.Map; + import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -10,6 +13,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -23,6 +27,8 @@ import eu.dnetlib.app.directindex.errors.DirectIndexApiException; import eu.dnetlib.app.directindex.input.ResultEntry; import eu.dnetlib.app.directindex.mapping.SolrRecordMapper; import eu.dnetlib.app.directindex.service.DirectIndexService; +import eu.dnetlib.app.directindex.solr.SolrIndexClientFactory; +import eu.dnetlib.dhp.schema.solr.SolrRecord; @RestController @RequestMapping("/api") @@ -37,20 +43,33 @@ public class LegacyApiController { @Autowired private SolrRecordMapper solrRecordMapper; + @Autowired + private SolrIndexClientFactory solrIndexClientFactory; + @PostMapping("/results/feedObject") - public String feedResult(@RequestBody final ResultEntry pub, - @RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit, - final HttpServletRequest req) + public String feedResult(@RequestBody final ResultEntry pub, final HttpServletRequest req) throws DirectIndexApiException { return service.prepareMetadataInsertOrUpdate(pub, req.getRemoteAddr()); } + @GetMapping("/result/{openaireId}") + public Map getResultWithOpenaireId(@PathVariable(value = "openaireId") final String openaireId) throws DirectIndexApiException { + + final SolrRecord indexed = solrIndexClientFactory.getClient().findRecord(openaireId); + + final Map res = new LinkedHashMap(); + res.put("indexed_oaf", indexed); + res.put("indexed_simple", indexed != null ? solrRecordMapper.toResultEntry(indexed) : null); + res.put("pending_action", service.findPendingAction(openaireId).orElse(null)); + + return res; + } + @DeleteMapping("/result/{openaireId}") - public boolean deleteResultWithOpenaireId(@PathVariable(value = "openaireId") final String openaireId, - @RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit, - final HttpServletRequest req) throws DirectIndexApiException { + public boolean deleteResultWithOpenaireId(@PathVariable(value = "openaireId") final String openaireId, final HttpServletRequest req) + throws DirectIndexApiException { service.prepareMetadataDeletion(openaireId, req.getRemoteAddr()); @@ -61,7 +80,6 @@ public class LegacyApiController { public boolean deleteResultWithOriginalId( @RequestParam(value = "originalId", required = true) final String originalId, @RequestParam(value = "collectedFromId", required = true) final String collectedFromId, - @RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit, final HttpServletRequest req) throws DirectIndexApiException { final String openaireId = solrRecordMapper.calculateOpenaireId(originalId, collectedFromId); diff --git a/src/main/java/eu/dnetlib/app/directindex/mapping/SolrRecordMapper.java b/src/main/java/eu/dnetlib/app/directindex/mapping/SolrRecordMapper.java index 82e3434..7849b48 100644 --- a/src/main/java/eu/dnetlib/app/directindex/mapping/SolrRecordMapper.java +++ b/src/main/java/eu/dnetlib/app/directindex/mapping/SolrRecordMapper.java @@ -192,7 +192,8 @@ public class SolrRecordMapper { re.setContexts(contexts); // @formatter:off - final List projects = r.getLinks() + if (r.getLinks() != null) { + final List projects = r.getLinks() .stream() .filter(l -> l.getHeader().getRelatedRecordType() == RecordType.project) .map(p -> String.format("info:eu-repo/grantAgreement/%s/%s/%s/%s/%s/%s", @@ -204,9 +205,10 @@ public class SolrRecordMapper { StringUtils.defaultIfBlank(p.getAcronym(), ""))) .distinct() .collect(Collectors.toList()); - // @formatter:on - re.setLinksToProjects(projects); + re.setLinksToProjects(projects); + } + // @formatter:on return re; } diff --git a/src/main/java/eu/dnetlib/app/directindex/service/DirectIndexService.java b/src/main/java/eu/dnetlib/app/directindex/service/DirectIndexService.java index d20c829..80f328d 100644 --- a/src/main/java/eu/dnetlib/app/directindex/service/DirectIndexService.java +++ b/src/main/java/eu/dnetlib/app/directindex/service/DirectIndexService.java @@ -3,6 +3,7 @@ package eu.dnetlib.app.directindex.service; import java.time.LocalDateTime; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Optional; import javax.transaction.Transactional; @@ -31,6 +32,11 @@ public class DirectIndexService { @Autowired private DatasourceManagerClient dsmClient; + @Transactional + public Optional findPendingAction(final String id) { + return pendingActionRepository.findById(id); + } + @Transactional public void prepareMetadataDeletion(final String openaireId, final String createdBy) { final PendingAction action = new PendingAction(); @@ -136,4 +142,5 @@ public class DirectIndexService { return info; } + }