From c35d7a647fcb73401e42e43545bc0d3ddcea919e Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Mon, 28 Oct 2024 14:59:17 +0100 Subject: [PATCH 1/2] 119687: Solr date sort field format check --- .../java/org/dspace/sort/OrderFormatDate.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java b/dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java index f56a97776f64..76f63f1c46a1 100644 --- a/dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java +++ b/dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java @@ -7,6 +7,9 @@ */ package org.dspace.sort; +import java.text.ParseException; +import java.text.SimpleDateFormat; + /** * Standard date ordering delegate implementation. The only "special" need is * to treat dates with less than 4-digit year. @@ -14,6 +17,8 @@ * @author Andrea Bollini */ public class OrderFormatDate implements OrderFormatDelegate { + private final SimpleDateFormat yearIso = new SimpleDateFormat("yyyy"); + @Override public String makeSortString(String value, String language) { int padding = 0; @@ -25,13 +30,27 @@ public String makeSortString(String value, String language) { padding = 4 - value.length(); } + String newValue = value; if (padding > 0) { // padding the value from left with 0 so that 87 -> 0087, 687-11-24 // -> 0687-11-24 - return String.format("%1$0" + padding + "d", 0) + newValue = String.format("%1$0" + padding + "d", 0) + value; + } + + if (isValidDate(newValue)) { + return newValue; } else { - return value; + return null; + } + } + + private boolean isValidDate(String value) { + try { + yearIso.parse(value); + } catch (ParseException e) { + return false; } + return true; } } From 56becc6916b5bde6a55ee8fdc2a955830e014d8d Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Wed, 30 Oct 2024 16:24:32 +0100 Subject: [PATCH 2/2] 119687: Solr date sort field format check fix --- .../main/java/org/dspace/sort/OrderFormatDate.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java b/dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java index 76f63f1c46a1..ca014a4fa484 100644 --- a/dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java +++ b/dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java @@ -21,6 +21,10 @@ public class OrderFormatDate implements OrderFormatDelegate { @Override public String makeSortString(String value, String language) { + if (!isValidDate(value)) { + return null; + } + int padding = 0; int endYearIdx = value.indexOf('-'); @@ -30,18 +34,13 @@ public String makeSortString(String value, String language) { padding = 4 - value.length(); } - String newValue = value; if (padding > 0) { // padding the value from left with 0 so that 87 -> 0087, 687-11-24 // -> 0687-11-24 - newValue = String.format("%1$0" + padding + "d", 0) + return String.format("%1$0" + padding + "d", 0) + value; - } - - if (isValidDate(newValue)) { - return newValue; } else { - return null; + return value; } }