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