dspace-api: improve date parsing for Solr sort

Re-use DSpace date parsing from o.d.util.MultiFormatDateParser for
more robust date support when creating of Solr browse/sort indexes.
This commit is contained in:
Alan Orth
2025-06-10 14:32:51 +03:00
parent c587d70a09
commit b594ebbf9e

View File

@@ -7,31 +7,28 @@
*/
package org.dspace.sort;
import java.util.Date;
import org.dspace.util.MultiFormatDateParser;
/**
* Standard date ordering delegate implementation. The only "special" need is
* to treat dates with less than 4-digit year.
* Standard date ordering delegate implementation using date format
* parsing from o.d.u.MultiFormatDateParser.
*
* @author Andrea Bollini
* @author Alan Orth
*/
public class OrderFormatDate implements OrderFormatDelegate {
@Override
public String makeSortString(String value, String language) {
int padding = 0;
int endYearIdx = value.indexOf('-');
Date result = MultiFormatDateParser.parse(value);
if (endYearIdx >= 0 && endYearIdx < 4) {
padding = 4 - endYearIdx;
} else if (value.length() < 4) {
padding = 4 - value.length();
}
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)
+ value;
// If parsing was successful we return the value as an ISO instant,
// otherwise we return null so Solr does not index this date value.
if (result != null) {
return result.toInstant().toString();
} else {
return value;
return null;
}
}
}