Jackson deserialization for HistoryEntry
This commit is contained in:
parent
86d1159926
commit
0d14df1427
@ -33,18 +33,22 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
package com.google.refine.history;
|
package com.google.refine.history;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JacksonInject;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
|
import com.fasterxml.jackson.databind.InjectableValues;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.refine.ProjectManager;
|
import com.google.refine.ProjectManager;
|
||||||
import com.google.refine.model.AbstractOperation;
|
import com.google.refine.model.AbstractOperation;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
@ -55,7 +59,7 @@ import com.google.refine.util.ParsingUtilities;
|
|||||||
* This is the metadata of a Change. It's small, so we can load it in order to
|
* This is the metadata of a Change. It's small, so we can load it in order to
|
||||||
* obtain information about a change without actually loading the change.
|
* obtain information about a change without actually loading the change.
|
||||||
*/
|
*/
|
||||||
public class HistoryEntry {
|
public class HistoryEntry {
|
||||||
final static Logger logger = LoggerFactory.getLogger("HistoryEntry");
|
final static Logger logger = LoggerFactory.getLogger("HistoryEntry");
|
||||||
@JsonProperty("id")
|
@JsonProperty("id")
|
||||||
final public long id;
|
final public long id;
|
||||||
@ -93,6 +97,19 @@ public class HistoryEntry {
|
|||||||
static public long allocateID() {
|
static public long allocateID() {
|
||||||
return Math.round(Math.random() * 1000000) + System.currentTimeMillis();
|
return Math.round(Math.random() * 1000000) + System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
protected HistoryEntry(
|
||||||
|
@JsonProperty("id")
|
||||||
|
long id,
|
||||||
|
@JacksonInject("projectID")
|
||||||
|
long projectID,
|
||||||
|
@JsonProperty("description")
|
||||||
|
String description,
|
||||||
|
@JsonProperty(OPERATION)
|
||||||
|
AbstractOperation operation) {
|
||||||
|
this(id,projectID,description,operation,OffsetDateTime.now(ZoneId.of("Z")));
|
||||||
|
}
|
||||||
|
|
||||||
public HistoryEntry(long id, Project project, String description, AbstractOperation operation, Change change) {
|
public HistoryEntry(long id, Project project, String description, AbstractOperation operation, Change change) {
|
||||||
this(id,project.id,description,operation,OffsetDateTime.now(ZoneId.of("Z")));
|
this(id,project.id,description,operation,OffsetDateTime.now(ZoneId.of("Z")));
|
||||||
@ -152,21 +169,13 @@ public class HistoryEntry {
|
|||||||
getChange().revert(project);
|
getChange().revert(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public HistoryEntry load(Project project, String s) throws Exception {
|
static public HistoryEntry load(Project project, String s) throws IOException {
|
||||||
JSONObject obj = ParsingUtilities.evaluateJsonStringToObject(s);
|
ObjectMapper mapper = ParsingUtilities.mapper.copy();
|
||||||
|
InjectableValues injection = new InjectableValues.Std()
|
||||||
AbstractOperation operation = null;
|
.addValue("projectID", project.id);
|
||||||
if (obj.has(OPERATION) && !obj.isNull(OPERATION)) {
|
mapper.setInjectableValues(injection);
|
||||||
operation = ParsingUtilities.mapper.readValue(obj.getJSONObject(OPERATION).toString(), AbstractOperation.class);
|
|
||||||
}
|
return mapper.readValue(s, HistoryEntry.class);
|
||||||
|
|
||||||
return new HistoryEntry(
|
|
||||||
obj.getLong("id"),
|
|
||||||
project.id,
|
|
||||||
obj.getString("description"),
|
|
||||||
operation,
|
|
||||||
ParsingUtilities.stringToDate(obj.getString("time"))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(){
|
public void delete(){
|
||||||
|
@ -84,6 +84,7 @@ public class ParsingUtilities {
|
|||||||
module.addSerializer(double.class, new SerializationFilters.DoubleSerializer());
|
module.addSerializer(double.class, new SerializationFilters.DoubleSerializer());
|
||||||
module.addSerializer(OffsetDateTime.class, new SerializationFilters.OffsetDateSerializer());
|
module.addSerializer(OffsetDateTime.class, new SerializationFilters.OffsetDateSerializer());
|
||||||
module.addSerializer(LocalDateTime.class, new SerializationFilters.LocalDateSerializer());
|
module.addSerializer(LocalDateTime.class, new SerializationFilters.LocalDateSerializer());
|
||||||
|
module.addDeserializer(OffsetDateTime.class, new SerializationFilters.OffsetDateDeserializer());
|
||||||
module.addDeserializer(LocalDateTime.class, new SerializationFilters.LocalDateDeserializer());
|
module.addDeserializer(LocalDateTime.class, new SerializationFilters.LocalDateDeserializer());
|
||||||
|
|
||||||
mapper.registerModule(module);
|
mapper.registerModule(module);
|
||||||
|
@ -125,6 +125,20 @@ public class SerializationFilters {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class OffsetDateDeserializer extends StdDeserializer<OffsetDateTime> {
|
||||||
|
private static final long serialVersionUID = 93872874L;
|
||||||
|
|
||||||
|
public OffsetDateDeserializer() {
|
||||||
|
super(OffsetDateTime.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OffsetDateTime deserialize(JsonParser p, DeserializationContext ctxt)
|
||||||
|
throws IOException, JsonProcessingException {
|
||||||
|
return ParsingUtilities.stringToDate(p.getValueAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class LocalDateDeserializer extends StdDeserializer<LocalDateTime> {
|
public static class LocalDateDeserializer extends StdDeserializer<LocalDateTime> {
|
||||||
private static final long serialVersionUID = 93872874L;
|
private static final long serialVersionUID = 93872874L;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user