fix issue #1383. use the localDateTime for project info

This commit is contained in:
Jacky 2017-12-15 15:24:09 -05:00
parent 880620b538
commit 0258dc1787
15 changed files with 115 additions and 79 deletions

View File

@ -35,10 +35,11 @@ package com.google.refine;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -179,7 +180,7 @@ public abstract class ProjectManager {
}//FIXME what should be the behaviour if metadata is null? i.e. not found
Project project = getProject(id);
if (project != null && metadata != null && metadata.getModified().after(project.getLastSave())) {
if (project != null && metadata != null && metadata.getModified().isAfter(project.getLastSave())) {
try {
saveProject(project);
} catch (Exception e) {
@ -242,7 +243,7 @@ public abstract class ProjectManager {
*/
protected void saveProjects(boolean allModified) {
List<SaveRecord> records = new ArrayList<SaveRecord>();
Date startTimeOfSave = new Date();
LocalDateTime startTimeOfSave = LocalDateTime.now();
synchronized (this) {
for (long id : _projectsMetadata.keySet()) {
@ -251,18 +252,18 @@ public abstract class ProjectManager {
if (project != null) {
boolean hasUnsavedChanges =
metadata.getModified().getTime() >= project.getLastSave().getTime();
metadata.getModified().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() >= project.getLastSave().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
// We use >= instead of just > to avoid the case where a newly created project
// has the same modified and last save times, resulting in the project not getting
// saved at all.
if (hasUnsavedChanges) {
long msecsOverdue = startTimeOfSave.getTime() - project.getLastSave().getTime();
long msecsOverdue = startTimeOfSave.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() - project.getLastSave().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
records.add(new SaveRecord(project, msecsOverdue));
} else if (!project.getProcessManager().hasPending()
&& startTimeOfSave.getTime() - project.getLastSave().getTime() > PROJECT_FLUSH_DELAY) {
&& startTimeOfSave.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() - project.getLastSave().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() > PROJECT_FLUSH_DELAY) {
/*
* It's been a while since the project was last saved and it hasn't been
@ -295,7 +296,8 @@ public abstract class ProjectManager {
for (int i = 0;
i < records.size() &&
(allModified || (new Date().getTime() - startTimeOfSave.getTime() < QUICK_SAVE_MAX_TIME));
(allModified || (LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() -
startTimeOfSave.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() < QUICK_SAVE_MAX_TIME));
i++) {
try {
@ -318,7 +320,7 @@ public abstract class ProjectManager {
ProjectMetadata metadata = getProjectMetadata(id);
Project project = _projects.get(id);
if (project != null && !project.getProcessManager().hasPending()
&& metadata.getModified().getTime() < project.getLastSave().getTime()) {
&& metadata.getModified().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() < project.getLastSave().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()) {
_projects.remove(id).dispose();
}
}

View File

@ -35,7 +35,7 @@ package com.google.refine;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -55,9 +55,9 @@ import com.google.refine.util.JSONUtilities;
import com.google.refine.util.ParsingUtilities;
public class ProjectMetadata implements Jsonizable {
private final Date _created;
private Date _modified;
private Date written = null;
private final LocalDateTime _created;
private LocalDateTime _modified;
private LocalDateTime written = null;
private String _name = "";
private String _password = "";
@ -81,17 +81,17 @@ public class ProjectMetadata implements Jsonizable {
private final static Logger logger = LoggerFactory.getLogger("project_metadata");
protected ProjectMetadata(Date date) {
protected ProjectMetadata(LocalDateTime date) {
_created = date;
preparePreferenceStore(_preferenceStore);
}
public ProjectMetadata() {
this(new Date());
this(LocalDateTime.now());
_modified = _created;
}
public ProjectMetadata(Date created, Date modified, String name) {
public ProjectMetadata(LocalDateTime created, LocalDateTime modified, String name) {
this(created);
_modified = modified;
_name = name;
@ -103,8 +103,8 @@ public class ProjectMetadata implements Jsonizable {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("created"); writer.value(ParsingUtilities.dateToString(_created));
writer.key("modified"); writer.value(ParsingUtilities.dateToString(_modified));
writer.key("created"); writer.value(ParsingUtilities.localDateToString(_created));
writer.key("modified"); writer.value(ParsingUtilities.localDateToString(_modified));
writer.key("creator"); writer.value(_creator);
writer.key("contributors"); writer.value(_contributors);
writer.key("subject"); writer.value(_subject);
@ -143,7 +143,7 @@ public class ProjectMetadata implements Jsonizable {
writer.endObject();
if (isSaveMode(options)) {
written = new Date();
written = LocalDateTime.now();
}
}
@ -158,7 +158,7 @@ public class ProjectMetadata implements Jsonizable {
}
public boolean isDirty() {
return written == null || _modified.after(written);
return written == null || _modified.isAfter(written);
}
public void write(JSONWriter jsonWriter) throws JSONException {
@ -181,9 +181,9 @@ public class ProjectMetadata implements Jsonizable {
static public ProjectMetadata loadFromJSON(JSONObject obj) {
// TODO: Is this correct? It's using modified date for creation date
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date()));
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getLocalDate(obj, "modified", LocalDateTime.now()));
pm._modified = JSONUtilities.getDate(obj, "modified", new Date());
pm._modified = JSONUtilities.getLocalDate(obj, "modified", LocalDateTime.now());
pm._name = JSONUtilities.getString(obj, "name", "<Error recovering project name>");
pm._password = JSONUtilities.getString(obj, "password", "");
@ -249,7 +249,7 @@ public class ProjectMetadata implements Jsonizable {
}
}
pm.written = new Date(); // Mark it as not needing writing until modified
pm.written = LocalDateTime.now(); // Mark it as not needing writing until modified
return pm;
}
@ -259,7 +259,7 @@ public class ProjectMetadata implements Jsonizable {
// Any project specific preferences?
}
public Date getCreated() {
public LocalDateTime getCreated() {
return _created;
}
@ -305,12 +305,12 @@ public class ProjectMetadata implements Jsonizable {
return _password;
}
public Date getModified() {
public LocalDateTime getModified() {
return _modified;
}
public void updateModified() {
_modified = new Date();
_modified = LocalDateTime.now();
}
public PreferenceStore getPreferenceStore() {

View File

@ -35,8 +35,10 @@ package com.google.refine.commands.expr;
import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
@ -212,10 +214,10 @@ public class PreviewExpressionCommand extends Command {
Calendar c = (Calendar) v;
sb.append("[date " +
ParsingUtilities.dateToString(c.getTime()) +"]");
} else if (v instanceof Date) {
ParsingUtilities.dateToString(ZonedDateTime.ofInstant(c.toInstant(), ZoneId.systemDefault())) +"]");
} else if (v instanceof LocalDateTime) {
sb.append("[date " +
ParsingUtilities.dateToString((Date) v) +"]");
ParsingUtilities.dateToString((ZonedDateTime) v) +"]");
} else if (v instanceof String) {
if (quote) {
sb.append(JSONObject.quote((String) v));

View File

@ -35,6 +35,7 @@ package com.google.refine.expr.functions;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@ -83,7 +84,7 @@ public class ToDate implements Function {
try {
return CalendarParser.parse( o1, (month_first) ? CalendarParser.MM_DD_YY : CalendarParser.DD_MM_YY);
} catch (CalendarParserException e) {
Date d = ParsingUtilities.stringToDate(o1);
ZonedDateTime d = ParsingUtilities.stringToDate(o1);
if (d != null) {
return d;
} else {

View File

@ -34,7 +34,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.history;
import java.io.Writer;
import java.util.Date;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Properties;
import org.json.JSONException;
@ -59,7 +60,7 @@ public class HistoryEntry implements Jsonizable {
final public long id;
final public long projectID;
final public String description;
final public Date time;
final public ZonedDateTime time;
// the manager (deals with IO systems or databases etc.)
final public HistoryEntryManager _manager;
@ -85,11 +86,11 @@ public class HistoryEntry implements Jsonizable {
}
public HistoryEntry(long id, Project project, String description, AbstractOperation operation, Change change) {
this(id,project.id,description,operation,new Date());
this(id,project.id,description,operation,ZonedDateTime.now(ZoneId.of("Z")));
setChange(change);
}
protected HistoryEntry(long id, long projectID, String description, AbstractOperation operation, Date time) {
protected HistoryEntry(long id, long projectID, String description, AbstractOperation operation, ZonedDateTime time) {
this.id = id;
this.projectID = projectID;
this.description = description;

View File

@ -39,7 +39,9 @@ import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import org.apache.commons.lang.StringUtils;
@ -137,7 +139,9 @@ public class ProjectMetadataUtilities {
mtime = Math.max(mtime, time);
}
}
pm = new ProjectMetadata(new Date(ctime),new Date(mtime), tempName);
pm = new ProjectMetadata(LocalDateTime.ofInstant(Instant.ofEpochMilli(ctime), ZoneId.systemDefault()),
LocalDateTime.ofInstant(Instant.ofEpochMilli(mtime), ZoneId.systemDefault()),
tempName);
logger.error("Partially recovered missing metadata project in directory " + projectDir + " - " + tempName);
}
return pm;

View File

@ -35,15 +35,16 @@ package com.google.refine.model;
import java.io.Serializable;
import java.io.Writer;
import java.util.Calendar;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.json.JSONException;
import org.json.JSONWriter;
import com.google.refine.Jsonizable;
import com.google.refine.expr.EvalError;
@ -86,11 +87,11 @@ public class Cell implements HasFields, Jsonizable {
} else {
writer.key("v");
if (value != null) {
if (value instanceof Calendar) {
writer.value(ParsingUtilities.dateToString(((Calendar) value).getTime()));
if (value instanceof LocalDateTime) {
writer.value(ParsingUtilities.localDateToString((LocalDateTime)value));
writer.key("t"); writer.value("date");
} else if (value instanceof Date) {
writer.value(ParsingUtilities.dateToString((Date) value));
} else if (value instanceof ZonedDateTime) {
writer.value(ParsingUtilities.dateToString((ZonedDateTime) value));
writer.key("t"); writer.value("date");
} else if (value instanceof Double
&& (((Double)value).isNaN() || ((Double)value).isInfinite())) {

View File

@ -41,6 +41,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@ -80,7 +81,7 @@ public class Project {
final public History history;
transient public ProcessManager processManager = new ProcessManager();
transient private Date _lastSave = new Date();
transient private LocalDateTime _lastSave = LocalDateTime.now();
final static Logger logger = LoggerFactory.getLogger("project");
@ -113,14 +114,14 @@ public class Project {
// The rest of the project should get garbage collected when we return.
}
public Date getLastSave(){
public LocalDateTime getLastSave(){
return this._lastSave;
}
/**
* Sets the lastSave time to now
*/
public void setLastSave(){
this._lastSave = new Date();
this._lastSave = LocalDateTime.now();
}
public ProjectMetadata getMetadata() {

View File

@ -38,6 +38,8 @@ import java.io.InputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -307,9 +309,11 @@ public class StandardReconConfig extends ReconConfig {
jsonWriter.key("name"); jsonWriter.value(cell2.recon.match.name);
jsonWriter.endObject();
} else if (cell2.value instanceof Calendar) {
jsonWriter.value(ParsingUtilities.dateToString(((Calendar) cell2.value).getTime()));
Calendar calendar = (Calendar) cell2.value;
ZonedDateTime d = ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.of("Z"));
jsonWriter.value(ParsingUtilities.dateToString(d));
} else if (cell2.value instanceof Date) {
jsonWriter.value(ParsingUtilities.dateToString((Date) cell2.value));
jsonWriter.value(ParsingUtilities.dateToString((ZonedDateTime) cell2.value));
} else {
jsonWriter.value(cell2.value.toString());
}

View File

@ -33,6 +33,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.util;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
@ -94,9 +97,19 @@ public class JSONUtilities {
}
}
static public Date getDate(JSONObject obj, String key, Date def) {
static public ZonedDateTime getDate(JSONObject obj, String key, ZonedDateTime def) {
try {
Date d = ParsingUtilities.stringToDate(obj.getString(key));
ZonedDateTime d = ParsingUtilities.stringToDate(obj.getString(key));
return d != null ? d : def;
} catch (JSONException e) {
return def;
}
}
static public LocalDateTime getLocalDate(JSONObject obj, String key, LocalDateTime def) {
try {
LocalDateTime d = ParsingUtilities.stringToLocalDate(obj.getString(key));
return d != null ? d : def;
} catch (JSONException e) {
@ -180,9 +193,9 @@ public class JSONUtilities {
} else if (value instanceof Boolean) {
obj.put(key, value);
} else if (value instanceof Date) {
obj.put(key, ParsingUtilities.dateToString((Date) value));
obj.put(key, ParsingUtilities.dateToString((ZonedDateTime) value));
} else if (value instanceof Calendar) {
obj.put(key, ParsingUtilities.dateToString(((Calendar) value).getTime()));
obj.put(key, ParsingUtilities.dateToString(ZonedDateTime.ofInstant(((Calendar)value).toInstant(), ZoneId.of("Z"))));
} else if (value instanceof String) {
obj.put(key, value);
} else {

View File

@ -38,9 +38,10 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
@ -168,26 +169,32 @@ public class ParsingUtilities {
}
/**
* Convert a date/time to an ISO 8601 string
* Convert a date/time to an ISO_LOCAL_DATE_TIME string
*
* @param d the date to be written
* @return string with ISO 8601 formatted date & time
* @return string with ISO_LOCAL_DATE_TIME formatted date & time
*/
static public String dateToString(Date d) {
return ISO8601_FORMAT.get().format(d);
static public String dateToString(ZonedDateTime d) {
// return d.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
return d.format(DateTimeFormatter.ISO_INSTANT);
}
static public String localDateToString(LocalDateTime d) {
return d.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
/**
* Parse an ISO 8601 formatted string into a Java Date.
* Parse an ISO_LOCAL_DATE_TIME formatted string into a Java Date.
*
* @param s the string to be parsed
* @return Date or null if the parse failed
* @return LocalDateTime or null if the parse failed
*/
static public Date stringToDate(String s) {
try {
return ISO8601_FORMAT.get().parse(s);
} catch (ParseException e) {
return null;
}
static public ZonedDateTime stringToDate(String s) {
// return LocalDateTime.parse(s, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
return ZonedDateTime.parse(s, DateTimeFormatter.ISO_INSTANT);
}
static public LocalDateTime stringToLocalDate(String s) {
return LocalDateTime.parse(s, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}

View File

@ -41,8 +41,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import java.util.Date;
import java.util.GregorianCalendar;
import java.time.LocalDateTime;
import org.mockito.Mockito;
import org.slf4j.LoggerFactory;
@ -222,7 +221,7 @@ public class ProjectManagerTests extends RefineTest {
}
protected void whenProjectGetLastSave(Project proj){
Date projectLastSaveDate = new GregorianCalendar(1970,01,02,00,30,00).getTime();
LocalDateTime projectLastSaveDate = LocalDateTime.of(1970,01,02,00,30,00);
when(proj.getLastSave()).thenReturn(projectLastSaveDate);
}
@ -230,7 +229,7 @@ public class ProjectManagerTests extends RefineTest {
whenMetadataGetModified(meta, 5*60);
}
protected void whenMetadataGetModified(ProjectMetadata meta, int secondsDifference){
Date metadataModifiedDate = new GregorianCalendar(1970,01,02,00, 30, secondsDifference).getTime();
LocalDateTime metadataModifiedDate = LocalDateTime.of(1970,01,02,00, 30 + secondsDifference);
when(meta.getModified()).thenReturn(metadataModifiedDate);
}

View File

@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.tests.model;
package com.google.refine.tests.browsing.facets;
import static org.mockito.Mockito.mock;

View File

@ -40,8 +40,9 @@ import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Properties;
import org.slf4j.LoggerFactory;
@ -200,11 +201,11 @@ public class CsvExporterTests extends RefineTest {
@Test
public void exportDateColumns(){
CreateGrid(1,2);
Calendar calendar = Calendar.getInstance();
Date date = new Date();
LocalDateTime localDate = LocalDateTime.now();
ZonedDateTime date = ZonedDateTime.now(ZoneId.of("Z"));
when(options.getProperty("printColumnHeader")).thenReturn("false");
project.rows.get(0).cells.set(0, new Cell(calendar, null));
project.rows.get(0).cells.set(0, new Cell(localDate, null));
project.rows.get(0).cells.set(1, new Cell(date, null));
try {
@ -213,7 +214,7 @@ public class CsvExporterTests extends RefineTest {
Assert.fail();
}
String expectedOutput = ParsingUtilities.dateToString(calendar.getTime()) + "," +
String expectedOutput = ParsingUtilities.localDateToString(localDate) + "," +
ParsingUtilities.dateToString(date) + "\n";
Assert.assertEquals(writer.toString(), expectedOutput);

View File

@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.tests.model;
package com.google.refine.tests.operations.cell;
import static org.mockito.Mockito.mock;