Add StatementGroup wrapper to control JSON serialization
This commit is contained in:
parent
214efc73b4
commit
b8905f5190
@ -28,6 +28,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.jsoup.helper.Validate;
|
import org.jsoup.helper.Validate;
|
||||||
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
|
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.helpers.Datamodel;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
|
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
|
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
|
||||||
|
@ -36,6 +36,8 @@ import java.util.function.Function;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.jsoup.helper.Validate;
|
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.implementation.StatementGroupImpl;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
|
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
|
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
|
||||||
@ -176,7 +178,7 @@ public class ItemUpdate {
|
|||||||
*
|
*
|
||||||
* @return the list of all added statements
|
* @return the list of all added statements
|
||||||
*/
|
*/
|
||||||
@JsonProperty("addedStatements")
|
@JsonIgnore // exposed as statement groups below
|
||||||
public List<Statement> getAddedStatements() {
|
public List<Statement> getAddedStatements() {
|
||||||
return addedStatements;
|
return addedStatements;
|
||||||
}
|
}
|
||||||
@ -295,6 +297,7 @@ public class ItemUpdate {
|
|||||||
*
|
*
|
||||||
* @return a grouped version of getAddedStatements()
|
* @return a grouped version of getAddedStatements()
|
||||||
*/
|
*/
|
||||||
|
@JsonIgnore
|
||||||
public List<StatementGroup> getAddedStatementGroups() {
|
public List<StatementGroup> getAddedStatementGroups() {
|
||||||
Map<PropertyIdValue, List<Statement>> map = new HashMap<>();
|
Map<PropertyIdValue, List<Statement>> map = new HashMap<>();
|
||||||
for (Statement statement : getAddedStatements()) {
|
for (Statement statement : getAddedStatements()) {
|
||||||
@ -306,11 +309,23 @@ public class ItemUpdate {
|
|||||||
}
|
}
|
||||||
List<StatementGroup> result = new ArrayList<>();
|
List<StatementGroup> result = new ArrayList<>();
|
||||||
for (Map.Entry<PropertyIdValue, List<Statement>> entry : map.entrySet()) {
|
for (Map.Entry<PropertyIdValue, List<Statement>> 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()));
|
result.add(new StatementGroupImpl(entry.getValue()));
|
||||||
}
|
}
|
||||||
return result;
|
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<StatementGroupJson> 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
|
* Group a list of ItemUpdates by subject: this is useful to make one single
|
||||||
* edit per item.
|
* edit per item.
|
||||||
@ -339,6 +354,7 @@ public class ItemUpdate {
|
|||||||
/**
|
/**
|
||||||
* Is this update about a new item?
|
* Is this update about a new item?
|
||||||
*/
|
*/
|
||||||
|
@JsonProperty("new")
|
||||||
public boolean isNew() {
|
public boolean isNew() {
|
||||||
return EntityIdValue.SITE_LOCAL.equals(getItemId().getSiteIri());
|
return EntityIdValue.SITE_LOCAL.equals(getItemId().getSiteIri());
|
||||||
}
|
}
|
||||||
|
@ -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<Statement> getStatements() {
|
||||||
|
return statementGroup.getStatements();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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": [],
|
"deletedStatements": [],
|
||||||
"descriptions": [],
|
"descriptions": [],
|
||||||
"labels": [],
|
"labels": [],
|
||||||
|
Loading…
Reference in New Issue
Block a user