From 8453eaef06b44356ac4d4a0aaaca6fba1912912b Mon Sep 17 00:00:00 2001 From: Serafeim Chatzopoulos Date: Wed, 12 Jun 2024 15:44:45 +0300 Subject: [PATCH] Add filter by publication date --- .../dto/request/ResearchProductsRequest.java | 13 ++++++++-- .../query/ResearchProductsRequestMapper.java | 25 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/openaire/api/dto/request/ResearchProductsRequest.java b/src/main/java/eu/openaire/api/dto/request/ResearchProductsRequest.java index 35748d8..e70c747 100644 --- a/src/main/java/eu/openaire/api/dto/request/ResearchProductsRequest.java +++ b/src/main/java/eu/openaire/api/dto/request/ResearchProductsRequest.java @@ -9,6 +9,9 @@ import jakarta.validation.constraints.Pattern; import lombok.Data; import lombok.Getter; import lombok.Setter; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; @Getter @Setter @@ -34,9 +37,16 @@ public class ResearchProductsRequest { private String[] originalId; @Parameter(description = "The type of the research product", array = @ArraySchema(schema = @Schema(type = "string", allowableValues = {"publication", "dataset", "software", "other"}))) -// @Pattern(regexp = "^(publication|dataset|software|other)$", message = "type should be one of 'publication', 'dataset', 'software', 'other'") private String[] type; + @Parameter(description = "Gets the research products whose publication date is greater than or equal the given date. Please provide a date formatted as YYYY-MM-DD", schema = @Schema(type = "string", format = "date")) + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date fromPublicationDate; + + @Parameter(description = "Gets the research products whose publication date is less than or equal the given date. Please provide a date formatted as YYYY-MM-DD", schema = @Schema(type = "string", format = "date")) + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date toPublicationDate; + @Parameter(description = "List of subjects associated to the research product", array = @ArraySchema(schema = @Schema(type = "string"))) private String[] subjects; @@ -49,7 +59,6 @@ public class ResearchProductsRequest { @Parameter(description = "The ORCiD of the authors involved in producing this research product", array = @ArraySchema(schema = @Schema(type = "string"))) private String[] authorOrcid; - @Parameter(description = "The name of the entity that holds, archives, publishes prints, distributes, releases, issues, or produces the resource.", array = @ArraySchema(schema = @Schema(type = "string"))) private String[] publisher; diff --git a/src/main/java/eu/openaire/api/mappers/query/ResearchProductsRequestMapper.java b/src/main/java/eu/openaire/api/mappers/query/ResearchProductsRequestMapper.java index 9472b82..6227029 100644 --- a/src/main/java/eu/openaire/api/mappers/query/ResearchProductsRequestMapper.java +++ b/src/main/java/eu/openaire/api/mappers/query/ResearchProductsRequestMapper.java @@ -5,6 +5,7 @@ import eu.openaire.api.mappers.Utils; import eu.openaire.api.solr.SolrQueryParams; import org.mapstruct.*; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Map; @@ -60,7 +61,8 @@ public interface ResearchProductsRequestMapper { Map.entry("isInDiamondJournal", "isindiamondjournal:(%s)"), Map.entry("isPubliclyFunded", "publiclyfunded:(%s)"), Map.entry("isGreen", "isgreen:(%s)"), - Map.entry("openAccessColor", "openaccesscolor:(%s)") + Map.entry("openAccessColor", "openaccesscolor:(%s)"), + Map.entry("publicationDate", "resultdateofacceptance:[%s TO %s]") ); @@ -94,10 +96,6 @@ public interface ResearchProductsRequestMapper { qList.add(hasProjectField); } -// if (!Utils.isNullOrEmpty(src.getKeywords())) { -// qList.add(String.format(solrFieldMapping.get("keywords"), Utils.escapeInput(src.getKeywords()))); -// } - // form query string if (!qList.isEmpty()) { solrQueryParams.setQ(String.join(" AND ", qList)); @@ -221,6 +219,23 @@ public interface ResearchProductsRequestMapper { fqList.add(String.format(solrFieldMapping.get("openAccessColor"), Utils.escapeAndJoin(src.getOpenAccessColor(), "OR", false))); } + // publication date + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + if (src.getFromPublicationDate() != null && src.getToPublicationDate() != null) { + fqList.add(String.format(solrFieldMapping.get("publicationDate"), formatter.format(src.getFromPublicationDate()), formatter.format(src.getToPublicationDate()))); + } else { + if (src.getFromPublicationDate() != null) { + fqList.add(String.format(solrFieldMapping.get("publicationDate"), formatter.format(src.getFromPublicationDate()), "*")); + } + + if (src.getToPublicationDate() != null) { + fqList.add(String.format(solrFieldMapping.get("publicationDate"), "*", formatter.format(src.getToPublicationDate()))); + } + } + + + solrQueryParams.setFq(fqList); }