9996: add year date range filter + minor fix #8

Merged
aisimeon349e6 merged 1 commits from task-9996/filter_with_publication_year into master 2024-09-29 22:30:39 +02:00
4 changed files with 75 additions and 68 deletions

View File

@ -9,9 +9,6 @@ import jakarta.validation.constraints.Pattern;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
import static eu.openaire.api.mappers.Utils.API_CURSOR_DESC;
import static eu.openaire.api.mappers.Utils.API_PAGE_DESC;
@ -77,32 +74,28 @@ public class ProjectRequest implements PaginatedRequest {
private String[] fundingStreamId;
@Parameter(
description = "Gets the projects with start date greater than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
schema = @Schema(type = "string", format = "date")
description = "Gets the projects with start date greater than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
schema = @Schema(type = "string")
)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate fromStartDate;
private String fromStartDate;
@Parameter(
description = "Gets the projects with start date less than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
schema = @Schema(type = "string", format = "date")
description = "Gets the projects with start date less than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
schema = @Schema(type = "string")
)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate toStartDate;
private String toStartDate;
@Parameter(
description = "Gets the projects with end date greater than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
schema = @Schema(type = "string", format = "date")
description = "Gets the projects with end date greater than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
schema = @Schema(type = "string")
)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate fromEndDate;
private String fromEndDate;
@Parameter(
description = "Gets the projects with end date less than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
schema = @Schema(type = "string", format = "date")
description = "Gets the projects with end date less than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
schema = @Schema(type = "string")
)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate toEndDate;
private String toEndDate;
@Parameter(
description = "The name or short name of the related organization",

View File

@ -9,9 +9,6 @@ import jakarta.validation.constraints.Pattern;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
import static eu.openaire.api.mappers.Utils.API_CURSOR_DESC;
import static eu.openaire.api.mappers.Utils.API_PAGE_DESC;
@ -76,18 +73,14 @@ public class ResearchProductsRequest implements PaginatedRequest {
private String[] type;
@Parameter(
description = "Gets the research products whose publication date is greater than or equal to he given date. Please provide a date formatted as YYYY-MM-DD",
schema = @Schema(type = "string", format = "date")
)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate fromPublicationDate;
description = "Gets the research products whose publication date is greater than or equal to he given date. Provide a date in YYYY or YYYY-MM-DD format",
schema = @Schema(type = "string"))
private String fromPublicationDate;
@Parameter(
description = "Gets the research products whose publication date is less than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
schema = @Schema(type = "string", format = "date")
)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate toPublicationDate;
description = "Gets the research products whose publication date is less than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
schema = @Schema(type = "string"))
private String toPublicationDate;
@Parameter(
description = "List of subjects associated to the research product",

View File

@ -9,6 +9,7 @@ import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
public class Utils {
@ -38,7 +39,7 @@ public class Utils {
indicating that there are no more results.
""";
static public String escapeAndJoin(String[] tokens, String predicate, boolean addQuotes, String suffix) {
public static String escapeAndJoin(String[] tokens, String predicate, boolean addQuotes, String suffix) {
tokens = Arrays.stream(tokens)
// remove empty tokens
@ -51,7 +52,7 @@ public class Utils {
return String.join(" " + predicate + " ", tokens);
}
static public String handleInput(String token, boolean addQuotes, String suffix) {
public static String handleInput(String token, boolean addQuotes, String suffix) {
boolean hasLogicalOperator = containsLogicalOperator(token);
@ -73,11 +74,11 @@ public class Utils {
return sb.toString();
}
static private boolean containsLogicalOperator(String input) {
private static boolean containsLogicalOperator(String input) {
return input.contains("AND") || input.contains("OR") || input.contains("NOT");
}
static public String escapeInput(String input, String suffix) {
public static String escapeInput(String input, String suffix) {
// Split the input into tokens at whitespace or parentheses or quotes
String[] tokens = input.split("(\\s+|(?=[()\\\"]|(?<=[()\\\"])))");
@ -163,24 +164,66 @@ public class Utils {
}
return value;
}
static private void removeLastElementIfSpace(List<String> list) {
private static void removeLastElementIfSpace(List<String> list) {
if (list.get(list.size() - 1).equals(" ")) {
list.remove(list.size() - 1);
}
}
static public boolean isNullOrEmpty(String str) {
public static boolean isNullOrEmpty(String str) {
return str == null || str.isBlank();
}
static public boolean isNullOrEmpty(String[] str) {
public static boolean isNullOrEmpty(String[] str) {
return str == null || str.length == 0;
}
static private String formatDate(LocalDate date) {
public static String formatSolrDateRange(String fieldName, String fromDate, String toDate) {
if (fromDate != null && toDate != null) {
return String.format(fieldName,
appendHHMMSS(appendMMDD(fromDate, "-01-01")),
appendHHMMSS(appendMMDD(toDate, "-12-31")));
} else {
if (fromDate != null) {
return String.format(fieldName,
appendHHMMSS(appendMMDD(fromDate, "-01-01")), "*");
}
if (toDate != null) {
return String.format(fieldName, "*",
appendHHMMSS(appendMMDD(toDate, "-12-31")));
}
}
return null;
}
/***
* Append -mm-dd if date came in yyyy format
* @return LocalDate in yyyy-mm-dd format
*/
private static LocalDate appendMMDD(String date, String monthDaySuffix) {
try {
if (date.length() == 10) { //yyyy-mm-dd
return LocalDate.parse(date);
} else if (date.length() == 4) { //yyyy
return LocalDate.parse(date + monthDaySuffix);
} else {
throw new IllegalArgumentException("Invalid date format");
}
} catch (DateTimeParseException e) {
throw new IllegalArgumentException("Failed to parse date: " + date, e);
}
}
/***
* The incoming date comes in yyyy-mm-dd, so we have to append hh-mm-ss, also.
* @return String in yyyy-MM-dd'T'HH:mm:ss'Z' format
*/
private static String appendHHMMSS(LocalDate date) {
// IMPORTANT: all dates are indexed in 12:00:00 in the index (not sure why)
// so we need to set this time to the date (this should change if dates are indexed in a different way)
@ -200,23 +243,7 @@ public class Utils {
return zdt.format(formatter);
}
static public String formatSolrDateRange(String fieldName, LocalDate fromDate, LocalDate toDate) {
if (fromDate != null && toDate != null) {
return String.format(fieldName, Utils.formatDate(fromDate), Utils.formatDate(toDate));
} else {
if (fromDate != null) {
return String.format(fieldName, Utils.formatDate(fromDate), "*");
}
if (toDate != null) {
return String.format(fieldName, "*", Utils.formatDate(toDate));
}
}
return null;
}
static public List<SolrQuery.SortClause> formatSortByParam(String sortBy, Map<String, String> fieldMapping) {
public static List<SolrQuery.SortClause> formatSortByParam(String sortBy, Map<String, String> fieldMapping) {
if (Utils.isNullOrEmpty(sortBy)) {
return null;

View File

@ -139,11 +139,8 @@ public interface ResearchProductMapper {
return null;
List<Pid> orcid = authorPidList
.stream()
.filter(
ap -> ap
.getTypeCode()
.equals(ModelConstants.ORCID))
.collect(Collectors.toList());
.filter(ap -> ModelConstants.ORCID.equals(ap.getTypeCode()))
.toList();
if (orcid.size() == 1) {
return getAuthorPid(orcid.get(0));
}
@ -151,11 +148,8 @@ public interface ResearchProductMapper {
return null;
orcid = authorPidList
.stream()
.filter(
ap -> ap
.getTypeCode()
.equals(ModelConstants.ORCID_PENDING))
.collect(Collectors.toList());
.filter(ap -> ModelConstants.ORCID_PENDING.equals(ap.getTypeCode()))
.toList();
if (orcid.size() == 1) {
return getAuthorPid(orcid.get(0));
}