From b8905f5190f775462d0311a0cd249053544cee3c Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Fri, 14 Jun 2019 16:08:20 +0100 Subject: [PATCH] Add StatementGroup wrapper to control JSON serialization --- .../wikidata/schema/WbStatementGroupExpr.java | 1 + .../wikidata/updates/ItemUpdate.java | 18 +++++++- .../wikidata/utils/StatementGroupJson.java | 43 +++++++++++++++++++ .../tests/data/updates/statement_groups.json | 28 ------------ 4 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 extensions/wikidata/src/org/openrefine/wikidata/utils/StatementGroupJson.java diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStatementGroupExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStatementGroupExpr.java index 724c8656d..53b89ba5e 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStatementGroupExpr.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStatementGroupExpr.java @@ -28,6 +28,7 @@ import java.util.List; import org.jsoup.helper.Validate; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; +import org.openrefine.wikidata.utils.StatementGroupJson; import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; diff --git a/extensions/wikidata/src/org/openrefine/wikidata/updates/ItemUpdate.java b/extensions/wikidata/src/org/openrefine/wikidata/updates/ItemUpdate.java index d9571ffcb..46def6711 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/updates/ItemUpdate.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/updates/ItemUpdate.java @@ -36,6 +36,8 @@ import java.util.function.Function; import java.util.stream.Collectors; import org.jsoup.helper.Validate; +import org.openrefine.wikidata.utils.StatementGroupJson; +import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.implementation.StatementGroupImpl; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; @@ -176,7 +178,7 @@ public class ItemUpdate { * * @return the list of all added statements */ - @JsonProperty("addedStatements") + @JsonIgnore // exposed as statement groups below public List getAddedStatements() { return addedStatements; } @@ -295,6 +297,7 @@ public class ItemUpdate { * * @return a grouped version of getAddedStatements() */ + @JsonIgnore public List getAddedStatementGroups() { Map> map = new HashMap<>(); for (Statement statement : getAddedStatements()) { @@ -306,10 +309,22 @@ public class ItemUpdate { } List result = new ArrayList<>(); for (Map.Entry> entry : map.entrySet()) { + // We have to do this rather than use Datamodel in order to preserve the + // custom entity id values which can link to new items. result.add(new StatementGroupImpl(entry.getValue())); } return result; } + + /** + * Json serialization for preview of item updates. Because StatementGroup + * is not designed for serialization (so its format is not specified by WDTK), + * we add a wrapper on top to specify it. + */ + @JsonProperty("addedStatementGroups") + public List getAddedStatementGroupsJson() { + return this.getAddedStatementGroups().stream().map(s -> new StatementGroupJson(s)).collect(Collectors.toList()); + } /** * Group a list of ItemUpdates by subject: this is useful to make one single @@ -339,6 +354,7 @@ public class ItemUpdate { /** * Is this update about a new item? */ + @JsonProperty("new") public boolean isNew() { return EntityIdValue.SITE_LOCAL.equals(getItemId().getSiteIri()); } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/utils/StatementGroupJson.java b/extensions/wikidata/src/org/openrefine/wikidata/utils/StatementGroupJson.java new file mode 100644 index 000000000..71796f129 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/utils/StatementGroupJson.java @@ -0,0 +1,43 @@ +package org.openrefine.wikidata.utils; + +import java.util.List; + +import org.wikidata.wdtk.datamodel.helpers.Datamodel; +import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; +import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.datamodel.interfaces.StatementGroup; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Wikidata-Toolkit's StatementGroup class is not designed to be serialized, + * so its serialization via Jackson is not specified. This adds annotations + * to specify its behaviour. + * + * @author Antonin Delpeuch + */ +public class StatementGroupJson { + + protected final StatementGroup statementGroup; + + public StatementGroupJson(StatementGroup s) { + statementGroup = s; + } + + @JsonProperty("subject") + public EntityIdValue getSubject() { + return statementGroup.getSubject(); + } + + @JsonProperty("property") + public PropertyIdValue getProperty() { + return statementGroup.getProperty(); + } + + @JsonProperty("statements") + public List getStatements() { + return statementGroup.getStatements(); + } + +} diff --git a/extensions/wikidata/tests/data/updates/statement_groups.json b/extensions/wikidata/tests/data/updates/statement_groups.json index ec70fa140..bf5f019b1 100644 --- a/extensions/wikidata/tests/data/updates/statement_groups.json +++ b/extensions/wikidata/tests/data/updates/statement_groups.json @@ -68,34 +68,6 @@ } } ], - "addedStatements": [ - { - "mainsnak": { - "property": "P348", - "snaktype": "novalue" - }, - "rank": "normal", - "type": "statement" - }, - { - "mainsnak": { - "datatype": "wikibase-item", - "datavalue": { - "entityType": "http://www.wikidata.org/ontology#Item", - "id": "Q1234", - "iri": "http://localhost/entity/Q1234", - "label": "new item", - "reconInternalId": 1234, - "siteIri": "http://localhost/entity/", - "types": [] - }, - "property": "P52", - "snaktype": "value" - }, - "rank": "normal", - "type": "statement" - } - ], "deletedStatements": [], "descriptions": [], "labels": [],