Add support for names and descriptions

This commit is contained in:
Antonin Delpeuch 2017-10-01 18:01:31 +01:00
parent 07e2a8d982
commit 9f955c40ae
7 changed files with 249 additions and 23 deletions

View File

@ -220,41 +220,110 @@ SchemaAlignmentDialog._createDialog = function() {
SchemaAlignmentDialog._addItem = function(json) {
var subject = null;
var statementGroups = null;
var nameDescs = null;
if (json) {
subject = json.subject;
statementGroups = json.statementGroups;
nameDescs = json.nameDescs;
}
var item = $('<div></div>').addClass('wbs-item');
$('#schema-alignment-statements-container').append(item);
var inputContainer = $('<div></div>').addClass('wbs-item-input').appendTo(item);
SchemaAlignmentDialog._initField(inputContainer, "wikibase-item", subject);
var right = $('<div></div>').addClass('wbs-right').appendTo(item);
$('<div></div>').addClass('wbs-namedesc-container').appendTo(right);
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
$('<a></a>').addClass('wbs-add-namedesc').text('add name/description').click(function() {
SchemaAlignmentDialog._addNameDesc(item, null);
}).appendTo(toolbar);
$('<div></div>').addClass('wbs-statement-group-container').appendTo(right);
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
$('<a></a>').addClass('wbs-add-statement-group').text('add statement').click(function() {
SchemaAlignmentDialog._addStatementGroup(item, null);
}).appendTo(toolbar);
if (statementGroups) {
for(var i = 0; i != statementGroups.length; i++) {
SchemaAlignmentDialog._addStatementGroup(item, statementGroups[i]);
}
} else {
} else if (!nameDescs) {
SchemaAlignmentDialog._addStatementGroup(item);
}
$('#schema-alignment-statements-container').append(item);
if (nameDescs) {
for(var i = 0; i != nameDescs.length; i++) {
SchemaAlignmentDialog._addNameDesc(item, nameDescs[i]);
}
}
}
SchemaAlignmentDialog._itemToJSON = function (item) {
var lst = new Array();
var statementGroupLst = new Array();
item.find('.wbs-statement-group').each(function () {
lst.push(SchemaAlignmentDialog._statementGroupToJSON($(this)));
statementGroupLst.push(SchemaAlignmentDialog._statementGroupToJSON($(this)));
});
var nameDescLst = new Array();
item.find('.wbs-namedesc').each(function () {
nameDescLst.push(SchemaAlignmentDialog._nameDescToJSON($(this)));
});
var inputContainer = item.find(".wbs-item-input").first();
return {subject: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
statementGroups: lst};
statementGroups: statementGroupLst,
nameDescs: nameDescLst};
};
/**************************
* NAMES AND DESCRIPTIONS *
**************************/
SchemaAlignmentDialog._addNameDesc = function(item, json) {
var type = 'ALIAS';
var value = null;
if (json) {
type = json.name_type;
value = json.value;
}
var container = item.find('.wbs-namedesc-container').first();
var namedesc = $('<div></div>').addClass('wbs-namedesc').appendTo(container);
var type_container = $('<div></div>').addClass('wbs-namedesc-type').appendTo(namedesc);
var type_input = $('<select></select>').appendTo(type_container);
$('<option></option>')
.attr('value', 'LABEL')
.text('Label')
.appendTo(type_input);
$('<option></option>')
.attr('value', 'DESCRIPTION')
.text('Description')
.appendTo(type_input);
$('<option></option>')
.attr('value', 'ALIAS')
.text('Alias')
.appendTo(type_input);
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(namedesc);
$('<img src="images/close.png" />').attr('alt', 'remove name/description').click(function() {
namedesc.remove();
}).appendTo(toolbar);
var right = $('<div></div>').addClass('wbs-right').appendTo(namedesc);
var value_container = $('<div></div>').addClass('wbs-namedesc-value').appendTo(namedesc);
SchemaAlignmentDialog._initField(value_container, "monolingualtext", value);
}
SchemaAlignmentDialog._nameDescToJSON = function (namedesc) {
var type = namedesc.find('select').first().val();
var value = namedesc.find('.wbs-namedesc-value').first().data("jsonValue");
return {
type: "wbnamedescexpr",
name_type: type,
value: value,
}
}
/********************
* STATEMENT GROUPS *
********************/
@ -588,6 +657,7 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue,
var expanded_width = "90px";
var animation_duration = 50;
input.attr("placeholder", "lang");
input.addClass("wbs-language-input");
inputContainer.width(initial_language_width);
input.langsuggest().bind("fb-select", function(evt, data) {
inputContainer.data("jsonValue", {
@ -618,6 +688,7 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue,
}
var propagateValue = function() {
console.log('propagateValue in monolingualtext')
inputContainer.data("jsonValue", {
type: "wbmonolingualexpr",
language: inputContainerLanguage.data("jsonValue"),

View File

@ -102,7 +102,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
padding: 2px;
}
.wbs-add-item, .wbs-add-statement-group, .wbs-add-statement, .wbs-add-qualifier, .wbs-add-reference {
.wbs-add-item, .wbs-add-statement-group, .wbs-add-statement,
.wbs-add-qualifier, .wbs-add-reference, .wbs-add-namedesc {
color: #0645ad !important;
font-size: 0.9em;
}
@ -193,6 +194,24 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
background-color: #eaf3ff;
}
.wbs-namedesc {
background-color: #eaecf0;
padding: 5px;
}
.wbs-namedesc-type, .wbs-namedesc-value {
display: inline-block;
}
.wbs-namedesc-value {
padding-left: 71px;
width: 200px;
}
.wbs-language-input {
width: 100%;
}
.schema-alignment-columns-header {
margin-bottom: 0.3em;
}

View File

@ -60,24 +60,39 @@ public class QuickStatementsExporter implements WriterExporter {
}
}
protected void translateItem(ItemUpdate item, Writer writer) throws IOException {
if (item.getItemId().equals(ItemIdValue.NULL)) {
writer.write("CREATE\n");
}
for (Statement s : item.getAddedStatements()) {
translateStatement(s, s.getClaim().getMainSnak().getPropertyId().getId(), true, writer);
}
for (Statement s : item.getDeletedStatements()) {
translateStatement(s, s.getClaim().getMainSnak().getPropertyId().getId(), false, writer);
protected void translateNameDescr(String qid, List<MonolingualTextValue> values, String prefix, ItemIdValue id, Writer writer) throws IOException {
for (MonolingualTextValue value : values) {
writer.write(qid+"\t");
writer.write(prefix);
writer.write(value.getLanguageCode());
writer.write("\t\"");
writer.write(value.getText());
writer.write("\"\n");
}
}
protected void translateStatement(Statement statement, String pid, boolean add, Writer writer) throws IOException {
Claim claim = statement.getClaim();
String qid = claim.getSubject().getId();
if (claim.getSubject().equals(ItemIdValue.NULL)) {
protected void translateItem(ItemUpdate item, Writer writer) throws IOException {
String qid = item.getItemId().getId();
if (item.getItemId().equals(ItemIdValue.NULL)) {
writer.write("CREATE\n");
qid = "LAST";
}
translateNameDescr(qid, item.getLabels(), "L", item.getItemId(), writer);
translateNameDescr(qid, item.getDescriptions(), "D", item.getItemId(), writer);
translateNameDescr(qid, item.getAliases(), "A", item.getItemId(), writer);
for (Statement s : item.getAddedStatements()) {
translateStatement(qid, s, s.getClaim().getMainSnak().getPropertyId().getId(), true, writer);
}
for (Statement s : item.getDeletedStatements()) {
translateStatement(qid, s, s.getClaim().getMainSnak().getPropertyId().getId(), false, writer);
}
}
protected void translateStatement(String qid, Statement statement, String pid, boolean add, Writer writer) throws IOException {
Claim claim = statement.getClaim();
Value val = claim.getValue();
ValueVisitor<String> vv = new ValuePrinter();
String targetValue = val.accept(vv);

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
@ -18,11 +19,17 @@ public class ItemUpdate {
private ItemIdValue qid;
private List<Statement> addedStatements;
private List<Statement> deletedStatements;
private List<MonolingualTextValue> labels;
private List<MonolingualTextValue> descriptions;
private List<MonolingualTextValue> aliases;
public ItemUpdate(ItemIdValue qid) {
this.qid = qid;
this.addedStatements = new ArrayList<Statement>();
this.deletedStatements = new ArrayList<Statement>();
this.labels = new ArrayList<MonolingualTextValue>();
this.descriptions = new ArrayList<MonolingualTextValue>();
this.aliases = new ArrayList<MonolingualTextValue>();
}
public void addStatement(Statement s) {
@ -60,9 +67,40 @@ public class ItemUpdate {
public void merge(ItemUpdate other) {
addStatements(other.getAddedStatements());
deleteStatements(other.getDeletedStatements());
labels.addAll(other.getLabels());
descriptions.addAll(other.getDescriptions());
aliases.addAll(other.getAliases());
}
public boolean isNull() {
return addedStatements.isEmpty() && deletedStatements.isEmpty();
return (addedStatements.isEmpty()
&& deletedStatements.isEmpty()
&& labels.isEmpty()
&& descriptions.isEmpty()
&& aliases.isEmpty());
}
public void addLabel(MonolingualTextValue val) {
labels.add(val);
}
public void addDescription(MonolingualTextValue val) {
descriptions.add(val);
}
public void addAlias(MonolingualTextValue val) {
aliases.add(val);
}
public List<MonolingualTextValue> getLabels() {
return labels;
}
public List<MonolingualTextValue> getDescriptions() {
return descriptions;
}
public List<MonolingualTextValue> getAliases() {
return aliases;
}
}

View File

@ -20,10 +20,14 @@ public class WbItemDocumentExpr extends BiJsonizable {
public static final String jsonType = "wbitemdocument";
private WbItemExpr subjectExpr;
private List<WbNameDescExpr> nameDescExprs;
private List<WbStatementGroupExpr> statementGroupExprs;
public WbItemDocumentExpr(WbItemExpr subjectExpr, List<WbStatementGroupExpr> statementGroupExprs) {
public WbItemDocumentExpr(WbItemExpr subjectExpr,
List<WbNameDescExpr> nameDescExprs,
List<WbStatementGroupExpr> statementGroupExprs) {
this.subjectExpr = subjectExpr;
this.nameDescExprs = nameDescExprs;
this.statementGroupExprs = statementGroupExprs;
}
@ -32,6 +36,12 @@ public class WbItemDocumentExpr extends BiJsonizable {
throws JSONException {
writer.key("subject");
subjectExpr.write(writer, options);
writer.key("nameDescs");
writer.array();
for(WbNameDescExpr expr : nameDescExprs) {
expr.write(writer, options);
}
writer.endArray();
writer.key("statementGroups");
writer.array();
for(WbStatementGroupExpr expr : statementGroupExprs) {
@ -47,8 +57,16 @@ public class WbItemDocumentExpr extends BiJsonizable {
for (int i = 0; i != statementsArr.length(); i++) {
statementExprs.add(WbStatementGroupExpr.fromJSON(statementsArr.getJSONObject(i)));
}
List<WbNameDescExpr> nameDescExprs = new ArrayList<WbNameDescExpr>();
if (obj.has("nameDescs")) { // for compatibility with earlier versions
JSONArray nameDescArr = obj.getJSONArray("nameDescs");
for (int i = 0; i != nameDescArr.length(); i++) {
nameDescExprs.add(WbNameDescExpr.fromJSON(nameDescArr.getJSONObject(i)));
}
}
return new WbItemDocumentExpr(
WbItemExpr.fromJSON(subjectObj),
nameDescExprs,
statementExprs);
}
@ -60,6 +78,9 @@ public class WbItemDocumentExpr extends BiJsonizable {
update.addStatement(s);
}
}
for(WbNameDescExpr expr : nameDescExprs) {
expr.contributeTo(update, ctxt);
}
return update;
}

View File

@ -7,7 +7,6 @@ import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.Value;
public class WbMonolingualExpr extends WbValueExpr {

View File

@ -0,0 +1,63 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
public class WbNameDescExpr extends BiJsonizable {
public final String jsonType = "wbnamedescexpr";
enum NameDescrType {
LABEL,
DESCRIPTION,
ALIAS,
}
private NameDescrType _type;
private WbMonolingualExpr _value;
public WbNameDescExpr(NameDescrType type, WbMonolingualExpr value) {
_type = type;
_value = value;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("name_type");
writer.value(_type.name());
writer.value("value");
_value.write(writer, options);
}
public void contributeTo(ItemUpdate item, ExpressionContext ctxt) throws SkipStatementException {
MonolingualTextValue val = _value.evaluate(ctxt);
switch (_type) {
case LABEL:
item.addLabel(val);
break;
case DESCRIPTION:
item.addDescription(val);
break;
case ALIAS:
item.addAlias(val);
break;
}
}
@Override
public String getJsonType() {
return jsonType;
}
public static WbNameDescExpr fromJSON(JSONObject obj) throws JSONException {
return new WbNameDescExpr(NameDescrType.valueOf((String) obj.get("name_type")),
WbMonolingualExpr.fromJSON(obj.getJSONObject("value")));
}
}