Merge pull request #1524 from OpenRefine/issue/1508
support project backward compatibility to old version of OpenRefine
This commit is contained in:
commit
74a6782ca0
@ -35,8 +35,12 @@ package com.google.refine.model;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
@ -87,11 +91,19 @@ public class Cell implements HasFields, Jsonizable {
|
|||||||
} else {
|
} else {
|
||||||
writer.key("v");
|
writer.key("v");
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
if (value instanceof LocalDateTime) {
|
Instant instant = null;
|
||||||
writer.value(ParsingUtilities.localDateToString((LocalDateTime)value));
|
if (value instanceof Calendar) {
|
||||||
writer.key("t"); writer.value("date");
|
instant = ((Calendar)value).toInstant();
|
||||||
|
} else if (value instanceof Date) {
|
||||||
|
instant = ((Date)value).toInstant();
|
||||||
} else if (value instanceof OffsetDateTime) {
|
} else if (value instanceof OffsetDateTime) {
|
||||||
writer.value(ParsingUtilities.dateToString((OffsetDateTime) value));
|
instant = ((OffsetDateTime)value).toInstant();
|
||||||
|
} else if (value instanceof LocalDateTime) {
|
||||||
|
instant = ((LocalDateTime)value).toInstant(ZoneOffset.of("Z"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instant != null) {
|
||||||
|
writer.value(ParsingUtilities.instantToString(instant));
|
||||||
writer.key("t"); writer.value("date");
|
writer.key("t"); writer.value("date");
|
||||||
} else if (value instanceof Double
|
} else if (value instanceof Double
|
||||||
&& (((Double)value).isNaN() || ((Double)value).isInfinite())) {
|
&& (((Double)value).isNaN() || ((Double)value).isInfinite())) {
|
||||||
|
@ -292,11 +292,8 @@ public class ProjectMetadata extends AbstractMetadata {
|
|||||||
|
|
||||||
private void extractModifiedLocalTime(JSONObject obj) {
|
private void extractModifiedLocalTime(JSONObject obj) {
|
||||||
String modified = JSONUtilities.getString(obj, "modified", LocalDateTime.now().toString());
|
String modified = JSONUtilities.getString(obj, "modified", LocalDateTime.now().toString());
|
||||||
if (modified.endsWith("Z")) {
|
|
||||||
this._modified = ParsingUtilities.stringToDate(modified).toLocalDateTime();
|
this._modified = ParsingUtilities.stringToLocalDate(modified);
|
||||||
} else {
|
|
||||||
this._modified = ParsingUtilities.stringToLocalDate(modified);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static protected void preparePreferenceStore(PreferenceStore ps) {
|
static protected void preparePreferenceStore(PreferenceStore ps) {
|
||||||
|
@ -42,6 +42,7 @@ import java.time.Instant;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
@ -55,6 +56,8 @@ import org.json.JSONObject;
|
|||||||
import org.json.JSONTokener;
|
import org.json.JSONTokener;
|
||||||
|
|
||||||
public class ParsingUtilities {
|
public class ParsingUtilities {
|
||||||
|
public static final DateTimeFormatter ISO8601 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||||
|
|
||||||
static public Properties parseUrlParameters(HttpServletRequest request) {
|
static public Properties parseUrlParameters(HttpServletRequest request) {
|
||||||
Properties options = new Properties();
|
Properties options = new Properties();
|
||||||
|
|
||||||
@ -166,28 +169,39 @@ public class ParsingUtilities {
|
|||||||
* @return string with ISO_LOCAL_DATE_TIME formatted date & time
|
* @return string with ISO_LOCAL_DATE_TIME formatted date & time
|
||||||
*/
|
*/
|
||||||
static public String dateToString(OffsetDateTime d) {
|
static public String dateToString(OffsetDateTime d) {
|
||||||
return d.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
return d.format(ISO8601);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public String localDateToString(LocalDateTime d) {
|
static public String localDateToString(LocalDateTime d) {
|
||||||
return d.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
OffsetDateTime odt = OffsetDateTime.of(d,
|
||||||
|
OffsetDateTime.now().getOffset());
|
||||||
|
|
||||||
|
return odt.withOffsetSameInstant(ZoneOffset.of("Z")).format(ISO8601);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse an ISO_LOCAL_DATE_TIME formatted string into a Java Date.
|
* Parse an ISO_LOCAL_DATE_TIME formatted string into a Java Date.
|
||||||
*
|
* For backward compatibility, to support the version <= 2.8, cannot use the DateTimeFormatter.ISO_OFFSET_DATE_TIME. Instead, use the ISO8601 below format:
|
||||||
|
* yyyy-MM-dd'T'HH:mm:ss'Z'
|
||||||
|
*
|
||||||
* @param s the string to be parsed
|
* @param s the string to be parsed
|
||||||
* @return LocalDateTime or null if the parse failed
|
* @return LocalDateTime or null if the parse failed
|
||||||
*/
|
*/
|
||||||
static public OffsetDateTime stringToDate(String s) {
|
static public OffsetDateTime stringToDate(String s) {
|
||||||
return OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
Instant instant = Instant.parse(s);
|
||||||
|
return OffsetDateTime.ofInstant(instant, ZoneId.of("Z"));
|
||||||
}
|
}
|
||||||
|
|
||||||
static public LocalDateTime stringToLocalDate(String s) {
|
static public LocalDateTime stringToLocalDate(String s) {
|
||||||
if (s.endsWith("Z")) { // UTC time
|
Instant instant = Instant.parse(s);
|
||||||
Instant instant = Instant.parse(s);
|
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
|
||||||
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
|
}
|
||||||
} else
|
|
||||||
return LocalDateTime.parse(s, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
static public String instantToString(Instant instant) {
|
||||||
|
return OffsetDateTime.ofInstant(instant, ZoneId.of("Z")).format(ISO8601);
|
||||||
|
}
|
||||||
|
|
||||||
|
static public String instantToLocalDateTimeString(Instant instant) {
|
||||||
|
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).format(ISO8601);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,9 +40,8 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.time.LocalDateTime;
|
import java.util.Calendar;
|
||||||
import java.time.ZoneId;
|
import java.util.Date;
|
||||||
import java.time.OffsetDateTime;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -200,13 +199,13 @@ public class CsvExporterTests extends RefineTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void exportDateColumns(){
|
public void exportDateColumnsPreVersion28(){
|
||||||
CreateGrid(1,2);
|
CreateGrid(1,2);
|
||||||
LocalDateTime localDate = LocalDateTime.now();
|
Calendar calendar = Calendar.getInstance();
|
||||||
OffsetDateTime date = OffsetDateTime.now(ZoneId.of("Z"));
|
Date date = new Date();
|
||||||
|
|
||||||
when(options.getProperty("printColumnHeader")).thenReturn("false");
|
when(options.getProperty("printColumnHeader")).thenReturn("false");
|
||||||
project.rows.get(0).cells.set(0, new Cell(localDate, null));
|
project.rows.get(0).cells.set(0, new Cell(calendar, null));
|
||||||
project.rows.get(0).cells.set(1, new Cell(date, null));
|
project.rows.get(0).cells.set(1, new Cell(date, null));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -215,12 +214,11 @@ public class CsvExporterTests extends RefineTest {
|
|||||||
Assert.fail();
|
Assert.fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
String expectedOutput = alignFractionalDigits(ParsingUtilities.localDateToString(localDate)) + "," +
|
String expectedOutput = ParsingUtilities.instantToLocalDateTimeString(calendar.toInstant()) + "," +
|
||||||
alignFractionalDigits(ParsingUtilities.dateToString(date)) + "\n";
|
ParsingUtilities.instantToLocalDateTimeString(date.toInstant()) + "\n";
|
||||||
|
|
||||||
Assert.assertEquals(writer.toString(), expectedOutput);
|
Assert.assertEquals(writer.toString(), expectedOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
//helper methods
|
//helper methods
|
||||||
|
|
||||||
protected void CreateColumns(int noOfColumns){
|
protected void CreateColumns(int noOfColumns){
|
||||||
|
Loading…
Reference in New Issue
Block a user