Skip to content

Commit b594ebb

Browse files
committed
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.
1 parent c587d70 commit b594ebb

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

dspace-api/src/main/java/org/dspace/sort/OrderFormatDate.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,28 @@
77
*/
88
package org.dspace.sort;
99

10+
import java.util.Date;
11+
12+
import org.dspace.util.MultiFormatDateParser;
13+
1014
/**
11-
* Standard date ordering delegate implementation. The only "special" need is
12-
* to treat dates with less than 4-digit year.
15+
* Standard date ordering delegate implementation using date format
16+
* parsing from o.d.u.MultiFormatDateParser.
1317
*
1418
* @author Andrea Bollini
19+
* @author Alan Orth
1520
*/
1621
public class OrderFormatDate implements OrderFormatDelegate {
1722
@Override
1823
public String makeSortString(String value, String language) {
19-
int padding = 0;
20-
int endYearIdx = value.indexOf('-');
21-
22-
if (endYearIdx >= 0 && endYearIdx < 4) {
23-
padding = 4 - endYearIdx;
24-
} else if (value.length() < 4) {
25-
padding = 4 - value.length();
26-
}
24+
Date result = MultiFormatDateParser.parse(value);
2725

28-
if (padding > 0) {
29-
// padding the value from left with 0 so that 87 -> 0087, 687-11-24
30-
// -> 0687-11-24
31-
return String.format("%1$0" + padding + "d", 0)
32-
+ value;
26+
// If parsing was successful we return the value as an ISO instant,
27+
// otherwise we return null so Solr does not index this date value.
28+
if (result != null) {
29+
return result.toInstant().toString();
3330
} else {
34-
return value;
31+
return null;
3532
}
3633
}
3734
}

0 commit comments

Comments
 (0)