diff --git a/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js b/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js index 141f8f064..2e6935524 100644 --- a/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js +++ b/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js @@ -117,7 +117,6 @@ SchemaAlignmentDialog._reset = function(schema, initial) { SchemaAlignmentDialog._save = function(onDone) { var self = this; var schema = this.getJSON(); - console.log(schema); Refine.postProcess( "wikidata", @@ -213,8 +212,6 @@ SchemaAlignmentDialog._createDialog = function() { }; SchemaAlignmentDialog._addItem = function(json) { - console.log('addItem') - console.log(json); var subject = null; var statementGroups = null; if (json) { @@ -224,7 +221,7 @@ SchemaAlignmentDialog._addItem = function(json) { var item = $('
').addClass('wbs-item'); var inputContainer = $('').addClass('wbs-item-input').appendTo(item); - SchemaAlignmentDialog._initField(inputContainer, "item", subject); + SchemaAlignmentDialog._initField(inputContainer, "wikibase-item", subject); var right = $('').addClass('wbs-right').appendTo(item); $('').addClass('wbs-statement-group-container').appendTo(right); var toolbar = $('').addClass('wbs-toolbar').appendTo(right); @@ -253,8 +250,6 @@ SchemaAlignmentDialog._itemToJSON = function (item) { }; SchemaAlignmentDialog._addStatementGroup = function(item, json) { - console.log('addStatementGroup') - console.log(json); var property = null; var statements = null; if (json) { @@ -265,22 +260,24 @@ SchemaAlignmentDialog._addStatementGroup = function(item, json) { var container = item.find('.wbs-statement-group-container').first(); var statementGroup = $('').addClass('wbs-statement-group'); var inputContainer = $('').addClass('wbs-prop-input').appendTo(statementGroup); - SchemaAlignmentDialog._initField(inputContainer, "property", property); var right = $('').addClass('wbs-right').appendTo(statementGroup); - $('').addClass('wbs-statement-container').appendTo(right); + var statementContainer = $('').addClass('wbs-statement-container').appendTo(right); + SchemaAlignmentDialog._initPropertyField(inputContainer, statementContainer, property); var toolbar = $('').addClass('wbs-toolbar').appendTo(right); - $('').addClass('wbs-add-statement').text('add value').click(function() { - SchemaAlignmentDialog._addStatement(statementGroup, null); - }).appendTo(toolbar); + var addValueButton = $('').addClass('wbs-add-statement').text('add value').click(function() { + var datatype = inputContainer.data("jsonValue").datatype; + SchemaAlignmentDialog._addStatement(statementContainer, datatype, null); + }).appendTo(toolbar).hide(); + container.append(statementGroup); if (statements) { for (var i = 0; i != statements.length; i++) { - SchemaAlignmentDialog._addStatement(statementGroup, statements[i]); + SchemaAlignmentDialog._addStatement(statementContainer, property.datatype, statements[i]); + addValueButton.show(); } - } else { - SchemaAlignmentDialog._addStatement(statementGroup, null); } + } SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) { @@ -294,24 +291,23 @@ SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) { }; -SchemaAlignmentDialog._addStatement = function(statementGroup, json) { +SchemaAlignmentDialog._addStatement = function(container, datatype, json) { + console.log('addStatement'); + console.log(datatype); var qualifiers = null; var value = null; - console.log('add statement'); - console.log(json); if (json) { qualifiers = json.qualifiers; value = json.value; } - var container = statementGroup.find('.wbs-statement-container').first(); var statement = $('').addClass('wbs-statement'); var toolbar1 = $('').addClass('wbs-toolbar').appendTo(statement); $('').attr('alt', 'remove statement').click(function() { SchemaAlignmentDialog._removeStatement(statement); }).appendTo(toolbar1); var inputContainer = $('').addClass('wbs-target-input').appendTo(statement); - SchemaAlignmentDialog._initField(inputContainer, "target", value); + SchemaAlignmentDialog._initField(inputContainer, datatype, value); var right = $('').addClass('wbs-right').appendTo(statement); $('').addClass('wbs-qualifier-container').appendTo(right); var toolbar2 = $('').addClass('wbs-toolbar').appendTo(right); @@ -328,37 +324,94 @@ SchemaAlignmentDialog._statementToJSON = function (statement) { }; }; -SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue) { +SchemaAlignmentDialog._getPropertyType = function(pid, callback) { + $.ajax({ + url:'https://www.wikidata.org/w/api.php', + data: { + action: "wbgetentities", + format: "json", + ids: pid, + props: "datatype", + }, + dataType: "jsonp", + success: function(data) { + callback(data.entities[pid].datatype); + }}); +} + +SchemaAlignmentDialog._initPropertyField = function(inputContainer, targetContainer, initialValue) { var input = $('').appendTo(inputContainer); if (this._reconService !== null) { + endpoint = this._reconService.suggest.property; + var suggestConfig = $.extend({}, endpoint); + suggestConfig.key = null; + suggestConfig.query_param_name = "prefix"; + + input.suggestP(suggestConfig).bind("fb-select", function(evt, data) { + // Fetch the type of this property and add the appropriate target value type + var statementGroup = inputContainer.parents(".wbs-statement-group").first(); + SchemaAlignmentDialog._getPropertyType(data.id, function(datatype) { + inputContainer.data("jsonValue", { + type : "wbpropconstant", + pid : data.id, + label: data.name, + datatype: datatype, + }); + SchemaAlignmentDialog._addStatement(targetContainer, datatype, null); + var addValueButtons = targetContainer.parent().find('.wbs-add-statement'); + addValueButtons.show(); + }); + SchemaAlignmentDialog._hasChanged(); + }).bind("fb-textchange", function(evt, data) { + inputContainer.data("jsonValue", null); + targetContainer.find('.wbs-statement').remove(); + var addValueButtons = targetContainer.parent().find('.wbs-add-statement'); + addValueButtons.hide(); + }); + } + + // Init with the provided initial value. + if (initialValue) { + if (initialValue.type === "wbpropconstant") { + input.val(initialValue.label); + } + inputContainer.data("jsonValue", initialValue); + } + +} + +SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue) { + var input = $('').appendTo(inputContainer); + + if (this._reconService !== null && mode === "wikibase-item") { var endpoint = null; - if (mode === "item" || mode === "target") { - endpoint = this._reconService.suggest.entity; - } else if (mode === "property") { - endpoint = this._reconService.suggest.property; - } + endpoint = this._reconService.suggest.entity; var suggestConfig = $.extend({}, endpoint); suggestConfig.key = null; suggestConfig.query_param_name = "prefix"; input.suggestP(suggestConfig).bind("fb-select", function(evt, data) { - if (mode === "item" || mode === "target") { - inputContainer.data("jsonValue", { - type : "wbitemconstant", - qid : data.id, - label: data.name, - }); - } else if (mode === "property") { - inputContainer.data("jsonValue", { - type : "wbpropconstant", - pid : data.id, - label: data.name, - }); - } + inputContainer.data("jsonValue", { + type : "wbitemconstant", + qid : data.id, + label: data.name, + }); SchemaAlignmentDialog._hasChanged(); }); + } else if (mode === "external-id") { + var propagateValue = function(val) { + inputContainer.data("jsonValue", { + type: "wbstringconstant", + value: val, + }); + }; + propagateValue(""); + input.change(function() { + propagateValue($(this).val()); + SchemaAlignmentDialog._hasChanged(); + }); } var acceptDraggableColumn = function(column) { @@ -374,42 +427,44 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue) }); }; - // If it isn't a property, make it droppable - if (mode !== "property") { - var acceptClass = ".wbs-draggable-column"; - if (mode === "item") { - acceptClass = ".wbs-reconciled-column"; - } - - - inputContainer.droppable({ - accept: acceptClass, - }).on("drop", function (evt, ui) { - var column = ui.draggable.clone(); - acceptDraggableColumn(column); - inputContainer.data("jsonValue", { - type : "wbitemvariable", - columnName: ui.draggable.text(), - }); - SchemaAlignmentDialog._hasChanged(); - return true; - }).on("dropactivate", function(evt, ui) { - input.addClass("wbs-accepting-input"); - }).on("dropdeactivate", function(evt, ui) { - input.removeClass("wbs-accepting-input"); - }); + // Make it droppable + var acceptClass = ".wbs-draggable-column"; + var wbVariableType = "wbstringvariable"; + if (mode === "wikibase-item") { + acceptClass = ".wbs-reconciled-column"; + wbVariableType = "wbitemvariable"; } + + inputContainer.droppable({ + accept: acceptClass, + }).on("drop", function (evt, ui) { + var column = ui.draggable.clone(); + acceptDraggableColumn(column); + inputContainer.data("jsonValue", { + type : wbVariableType, + columnName: ui.draggable.text(), + }); + SchemaAlignmentDialog._hasChanged(); + return true; + }).on("dropactivate", function(evt, ui) { + input.addClass("wbs-accepting-input"); + }).on("dropdeactivate", function(evt, ui) { + input.removeClass("wbs-accepting-input"); + }); + // Init with the provided initial value. - console.log('initField'); if (initialValue) { - console.log(initialValue); - console.log(initialValue.type); - if (initialValue.type === "wbitemconstant" || initialValue.type === "wbpropconstant") { + if (initialValue.type === "wbitemconstant") { input.val(initialValue.label); } else if (initialValue.type == "wbitemvariable") { var cell = SchemaAlignmentDialog._createDraggableColumn(initialValue.columnName, true); acceptDraggableColumn(cell); + } else if (initialValue.type == "wbstringconstant") { + input.val(initialValue.value); + } else if (initialValue.type == "wbstringvariable") { + var cell = SchemaAlignmentDialog._createDraggableColumn(initialValue.columnName, false); + acceptDraggableColumn(cell); } inputContainer.data("jsonValue", initialValue); } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java index d30722532..2cc9def7d 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java @@ -16,10 +16,12 @@ public class WbPropConstant extends WbPropExpr { private String pid; private String label; + private String datatype; - public WbPropConstant(String pid, String label) { + public WbPropConstant(String pid, String label, String datatype) { this.pid = pid; this.label = label; + this.datatype = datatype; } @Override @@ -29,10 +31,15 @@ public class WbPropConstant extends WbPropExpr { writer.value(pid); writer.key("label"); writer.value(label); + writer.key("datatype"); + writer.value(datatype); } public static WbPropConstant fromJSON(JSONObject obj) throws JSONException { - return new WbPropConstant(obj.getString("pid"), obj.getString("label")); + return new WbPropConstant( + obj.getString("pid"), + obj.getString("label"), + obj.getString("datatype")); } @Override diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStatementGroupExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStatementGroupExpr.java index 362605d21..3268aaca3 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStatementGroupExpr.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStatementGroupExpr.java @@ -9,14 +9,10 @@ import org.json.JSONException; import org.json.JSONObject; import org.json.JSONWriter; import org.wikidata.wdtk.datamodel.helpers.Datamodel; -import org.wikidata.wdtk.datamodel.interfaces.Claim; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; -import org.wikidata.wdtk.datamodel.interfaces.Snak; -import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.StatementGroup; -import org.wikidata.wdtk.datamodel.interfaces.Value; public class WbStatementGroupExpr extends BiJsonizable { diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java index ee2be8fd1..5c9de96b4 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java @@ -6,11 +6,10 @@ import org.json.JSONException; import org.json.JSONObject; import org.json.JSONWriter; import org.wikidata.wdtk.datamodel.helpers.Datamodel; -import org.wikidata.wdtk.datamodel.implementation.StringValueImpl; -import org.wikidata.wdtk.datamodel.interfaces.Value; +import org.wikidata.wdtk.datamodel.interfaces.StringValue; -public class WbStringConstant extends WbValueExpr { +public class WbStringConstant extends WbStringExpr { public static final String jsonType = "wbstringconstant"; @@ -31,7 +30,7 @@ public class WbStringConstant extends WbValueExpr { } @Override - public Value evaluate(ExpressionContext ctxt) { + public StringValue evaluate(ExpressionContext ctxt) { return Datamodel.makeStringValue(value); } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringExpr.java new file mode 100644 index 000000000..b882f62c9 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringExpr.java @@ -0,0 +1,21 @@ +package org.openrefine.wikidata.schema; + + +import org.json.JSONException; +import org.json.JSONObject; +import org.wikidata.wdtk.datamodel.interfaces.StringValue; + +public abstract class WbStringExpr extends WbValueExpr { + public abstract StringValue evaluate(ExpressionContext ctxt) throws SkipStatementException; + + public static WbStringExpr fromJSON(JSONObject obj) throws JSONException { + String type = obj.getString(jsonTypeKey); + if (WbStringConstant.jsonType.equals(type)) { + return WbStringConstant.fromJSON(obj); + } else if (WbStringVariable.jsonType.equals(type)) { + return WbStringVariable.fromJSON2(obj); + } else { + throw new JSONException("unknown type for WbItemExpr"); + } + } +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java new file mode 100644 index 000000000..a3a08c270 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java @@ -0,0 +1,48 @@ +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.helpers.Datamodel; +import org.wikidata.wdtk.datamodel.interfaces.StringValue; + +import com.google.refine.model.Cell; + +public class WbStringVariable extends WbStringExpr { + public static final String jsonType = "wbstringvariable"; + + private String columnName; + + public WbStringVariable(String columnName) { + this.columnName = columnName; + } + + @Override + public StringValue evaluate(ExpressionContext ctxt) + throws SkipStatementException { + Cell cell = ctxt.getCellByName(columnName); + if (cell != null) { + return Datamodel.makeStringValue(cell.value.toString()); + } + throw new SkipStatementException(); + } + + @Override + public void writeFields(JSONWriter writer, Properties options) + throws JSONException { + writer.key("columnName"); + writer.value(columnName); + } + + public static WbStringVariable fromJSON2(JSONObject obj) throws JSONException { + return new WbStringVariable(obj.getString("columnName")); + } + + @Override + public String getJsonType() { + return jsonType; + } + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java index da538b454..498560816 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java @@ -26,6 +26,8 @@ public abstract class WbValueExpr extends BiJsonizable { valueExpr = WbItemConstant.fromJSON(obj); } else if (WbItemVariable.jsonType.equals(type)) { valueExpr = WbItemVariable.fromJSON(obj); + } else if (WbStringVariable.jsonType.equals(type)) { + valueExpr = WbStringVariable.fromJSON2(obj); } else if (WbStringConstant.jsonType.equals(type)) { valueExpr = WbStringConstant.fromJSON(obj); } else {