JSON serialization of schema to model
This commit is contained in:
parent
82930da4db
commit
e38aeb7ba5
@ -46,6 +46,11 @@ function init() {
|
|||||||
ExporterRegistry.registerExporter("qsv2", new QSV2());
|
ExporterRegistry.registerExporter("qsv2", new QSV2());
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Commands
|
||||||
|
*/
|
||||||
|
RefineServlet.registerCommand(module, "save-wikibase-schema", new SaveWikibaseSchemaCommand());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Resources
|
* Resources
|
||||||
*/
|
*/
|
||||||
|
@ -202,17 +202,18 @@ SchemaAlignmentDialog._reset = function(protograph, initial) {
|
|||||||
|
|
||||||
SchemaAlignmentDialog._save = function(onDone) {
|
SchemaAlignmentDialog._save = function(onDone) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var protograph = this.getJSON();
|
var schema = this.getJSON();
|
||||||
|
console.log(schema);
|
||||||
|
|
||||||
Refine.postProcess(
|
Refine.postProcess(
|
||||||
"freebase",
|
"wikidata",
|
||||||
"save-protograph",
|
"save-wikibase-schema",
|
||||||
{},
|
{},
|
||||||
{ protograph: JSON.stringify(protograph) },
|
{ schema: JSON.stringify(schema) },
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
onDone: function() {
|
onDone: function() {
|
||||||
theProject.overlayModels.freebaseProtograph = protograph;
|
theProject.overlayModels.wikibaseSchema = schema;
|
||||||
|
|
||||||
self._elmts.statusIndicator.hide();
|
self._elmts.statusIndicator.hide();
|
||||||
self._hasUnsavedChanges = false;
|
self._hasUnsavedChanges = false;
|
||||||
@ -229,19 +230,35 @@ SchemaAlignmentDialog._createDialog = function() {
|
|||||||
var elmts = this._elmts = DOM.bind(frame);
|
var elmts = this._elmts = DOM.bind(frame);
|
||||||
|
|
||||||
this._level = DialogSystem.showDialog(frame);
|
this._level = DialogSystem.showDialog(frame);
|
||||||
|
this._wikibasePrefix = "http://www.wikidata.org/entity/"; // hardcoded for now
|
||||||
|
|
||||||
// Init the column area
|
// Init the column area
|
||||||
var columns = theProject.columnModel.columns;
|
var columns = theProject.columnModel.columns;
|
||||||
this._columnArea = $(".schema-alignment-dialog-columns-area");
|
this._columnArea = $(".schema-alignment-dialog-columns-area");
|
||||||
for (var i = 0; i < columns.length; i++) {
|
for (var i = 0; i < columns.length; i++) {
|
||||||
var cell = $("<div></div>").addClass('schema-alignment-draggable-column').text(columns[i].name);
|
var column = columns[i];
|
||||||
|
var reconConfig = column.reconConfig;
|
||||||
|
var cell = $("<div></div>").addClass('wbs-draggable-column').text(columns[i].name);
|
||||||
|
console.log(column.reconStats);
|
||||||
|
if (reconConfig && reconConfig.identifierSpace === this._wikibasePrefix && column.reconStats) {
|
||||||
|
cell.addClass('wbs-reconciled-column');
|
||||||
|
} else {
|
||||||
|
cell.addClass('wbs-unreconciled-column');
|
||||||
|
}
|
||||||
this._columnArea.append(cell);
|
this._columnArea.append(cell);
|
||||||
}
|
}
|
||||||
$('.schema-alignment-draggable-column').draggable({
|
|
||||||
|
$('.wbs-reconciled-column').draggable({
|
||||||
helper: "clone",
|
helper: "clone",
|
||||||
revert: true,
|
cursor: "crosshair",
|
||||||
revertDuration: 200,
|
snap: ".wbs-item-input input, .wbs-target-input input",
|
||||||
});
|
});
|
||||||
|
$('.wbs-unreconciled-column').draggable({
|
||||||
|
helper: "clone",
|
||||||
|
cursor: "crosshair",
|
||||||
|
snap: ".wbs-target-input input",
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
var dismiss = function() {
|
var dismiss = function() {
|
||||||
DialogSystem.dismissUntil(self._level - 1);
|
DialogSystem.dismissUntil(self._level - 1);
|
||||||
@ -270,29 +287,40 @@ SchemaAlignmentDialog._createDialog = function() {
|
|||||||
this._canvas = $(".schema-alignment-dialog-canvas");
|
this._canvas = $(".schema-alignment-dialog-canvas");
|
||||||
this._nodeTable = $('<table></table>').addClass("schema-alignment-table-layout").appendTo(this._canvas)[0];
|
this._nodeTable = $('<table></table>').addClass("schema-alignment-table-layout").appendTo(this._canvas)[0];
|
||||||
|
|
||||||
SchemaAlignmentDialog._reconService = ReconciliationManager.ensureDefaultServicePresent();
|
var url = ReconciliationManager.ensureDefaultServicePresent();
|
||||||
console.log(SchemaAlignmentDialog._reconService);
|
SchemaAlignmentDialog._reconService = ReconciliationManager.getServiceFromUrl(url);
|
||||||
};
|
};
|
||||||
|
|
||||||
SchemaAlignmentDialog._addItem = function() {
|
SchemaAlignmentDialog._addItem = function() {
|
||||||
var item = $('<div></div>').addClass('wbs-item');
|
var item = $('<div></div>').addClass('wbs-item');
|
||||||
var input = $('<div></div>').addClass('wbs-item-input').appendTo(item);
|
var inputContainer = $('<div></div>').addClass('wbs-item-input').appendTo(item);
|
||||||
SchemaAlignmentDialog._initField(input);
|
SchemaAlignmentDialog._initField(inputContainer, "item");
|
||||||
var right = $('<div></div>').addClass('wbs-right').appendTo(item);
|
var right = $('<div></div>').addClass('wbs-right').appendTo(item);
|
||||||
$('<div></div>').addClass('wbs-statement-group-container').appendTo(right);
|
$('<div></div>').addClass('wbs-statement-group-container').appendTo(right);
|
||||||
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
|
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
|
||||||
$('<a></a>').addClass('wbs-add-statement-group').text('add statement').click(function() {
|
$('<a></a>').addClass('wbs-add-statement-group').text('add statement').click(function() {
|
||||||
SchemaAlignmentDialog._addStatementGroup(item);
|
SchemaAlignmentDialog._addStatementGroup(item);
|
||||||
}).appendTo(toolbar);
|
}).appendTo(toolbar);
|
||||||
|
|
||||||
SchemaAlignmentDialog._addStatementGroup(item);
|
SchemaAlignmentDialog._addStatementGroup(item);
|
||||||
$('#schema-alignment-statements-container').append(item);
|
$('#schema-alignment-statements-container').append(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SchemaAlignmentDialog._itemToJSON = function (item) {
|
||||||
|
var lst = new Array();
|
||||||
|
item.find('.wbs-statement-group').each(function () {
|
||||||
|
lst.push(SchemaAlignmentDialog._statementGroupToJSON($(this)));
|
||||||
|
});
|
||||||
|
var inputContainer = item.find(".wbs-item-input").first();
|
||||||
|
return {item: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
|
||||||
|
values: lst};
|
||||||
|
};
|
||||||
|
|
||||||
SchemaAlignmentDialog._addStatementGroup = function(item) {
|
SchemaAlignmentDialog._addStatementGroup = function(item) {
|
||||||
var container = item.find('.wbs-statement-group-container').first();
|
var container = item.find('.wbs-statement-group-container').first();
|
||||||
var statementGroup = $('<div></div>').addClass('wbs-statement-group');
|
var statementGroup = $('<div></div>').addClass('wbs-statement-group');
|
||||||
var input = $('<div></div>').addClass('wbs-prop-input').appendTo(statementGroup);
|
var inputContainer = $('<div></div>').addClass('wbs-prop-input').appendTo(statementGroup);
|
||||||
SchemaAlignmentDialog._initField(input);
|
SchemaAlignmentDialog._initField(inputContainer, "property");
|
||||||
var right = $('<div></div>').addClass('wbs-right').appendTo(statementGroup);
|
var right = $('<div></div>').addClass('wbs-right').appendTo(statementGroup);
|
||||||
$('<div></div>').addClass('wbs-statement-container').appendTo(right);
|
$('<div></div>').addClass('wbs-statement-container').appendTo(right);
|
||||||
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
|
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
|
||||||
@ -303,15 +331,26 @@ SchemaAlignmentDialog._addStatementGroup = function(item) {
|
|||||||
SchemaAlignmentDialog._addStatement(statementGroup);
|
SchemaAlignmentDialog._addStatement(statementGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) {
|
||||||
|
var lst = new Array();
|
||||||
|
statementGroup.find('.wbs-statement').each(function () {
|
||||||
|
lst.push(SchemaAlignmentDialog._statementToJSON($(this)));
|
||||||
|
});
|
||||||
|
var inputContainer = statementGroup.find(".wbs-prop-input").first();
|
||||||
|
return {prop: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
|
||||||
|
values: lst};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
SchemaAlignmentDialog._addStatement = function(statementGroup) {
|
SchemaAlignmentDialog._addStatement = function(statementGroup) {
|
||||||
var container = statementGroup.find('.wbs-statement-container').first();
|
var container = statementGroup.find('.wbs-statement-container').first();
|
||||||
var statement = $('<div></div>').addClass('wbs-statement');
|
var statement = $('<div></div>').addClass('wbs-statement');
|
||||||
var toolbar1 = $('<div></div>').addClass('wbs-toolbar').appendTo(statement);
|
var toolbar1 = $('<div></div>').addClass('wbs-toolbar').appendTo(statement);
|
||||||
$('<a></a>').addClass('wbs-remove-statement').text('x').click(function() {
|
$('<img src="images/close.png" />').attr('alt', 'remove statement').click(function() {
|
||||||
SchemaAlignmentDialog._removeStatement(statement);
|
SchemaAlignmentDialog._removeStatement(statement);
|
||||||
}).appendTo(toolbar1);
|
}).appendTo(toolbar1);
|
||||||
var input = $('<div></div>').addClass('wbs-target-input').appendTo(statement);
|
var inputContainer = $('<div></div>').addClass('wbs-target-input').appendTo(statement);
|
||||||
SchemaAlignmentDialog._initField(input);
|
SchemaAlignmentDialog._initField(inputContainer, "target");
|
||||||
var right = $('<div></div>').addClass('wbs-right').appendTo(statement);
|
var right = $('<div></div>').addClass('wbs-right').appendTo(statement);
|
||||||
$('<div></div>').addClass('wbs-qualifier-container').appendTo(right);
|
$('<div></div>').addClass('wbs-qualifier-container').appendTo(right);
|
||||||
var toolbar2 = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
|
var toolbar2 = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
|
||||||
@ -319,25 +358,70 @@ SchemaAlignmentDialog._addStatement = function(statementGroup) {
|
|||||||
container.append(statement);
|
container.append(statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
SchemaAlignmentDialog._initField = function(input) {
|
SchemaAlignmentDialog._statementToJSON = function (statement) {
|
||||||
input.droppable({
|
var inputContainer = statement.find(".wbs-target-input").first();
|
||||||
accept: ".schema-alignment-draggable-column"
|
return SchemaAlignmentDialog._inputContainerToJSON(inputContainer);
|
||||||
}).on("drop", function (evt, ui) {
|
};
|
||||||
input.text(ui.draggable.text());
|
|
||||||
});
|
SchemaAlignmentDialog._initField = function(inputContainer, mode) {
|
||||||
var suggestConfig = $.extend({}, this._reconService.suggest.property);
|
var input = $('<input></input>').appendTo(inputContainer);
|
||||||
|
|
||||||
|
if (this._reconService !== null) {
|
||||||
|
var endpoint = null;
|
||||||
|
if (mode === "item" || mode === "target") {
|
||||||
|
endpoint = this._reconService.suggest.entity;
|
||||||
|
} else if (mode == "property") {
|
||||||
|
endpoint = this._reconService.suggest.property;
|
||||||
|
}
|
||||||
|
var suggestConfig = $.extend({}, endpoint);
|
||||||
suggestConfig.key = null;
|
suggestConfig.key = null;
|
||||||
suggestConfig.query_param_name = "prefix";
|
suggestConfig.query_param_name = "prefix";
|
||||||
|
|
||||||
input.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
|
input.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
|
||||||
console.log(data); /*
|
inputContainer.data("jsonValue", {
|
||||||
self._addProperty({
|
type : "wbitemconstant",
|
||||||
id : data.id,
|
id : data.id,
|
||||||
name: data.name,
|
name: data.name,
|
||||||
}); */
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
input.hide();
|
||||||
|
var columnDiv = $('<div></div>').appendTo(inputContainer);
|
||||||
|
var column = ui.draggable.clone().appendTo(columnDiv);
|
||||||
|
var deleteButton = $(' <img src="images/close.png" />').addClass('wbs-delete-column-button').appendTo(column);
|
||||||
|
deleteButton.attr('alt', 'remove column');
|
||||||
|
deleteButton.click(function () {
|
||||||
|
columnDiv.remove();
|
||||||
|
input.show();
|
||||||
|
});
|
||||||
|
inputContainer.data("jsonValue", {
|
||||||
|
type : "wbitemvariable",
|
||||||
|
name: ui.draggable.text(),
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}).on("dropactivate", function(evt, ui) {
|
||||||
|
input.addClass("wbs-accepting-input");
|
||||||
|
}).on("dropdeactivate", function(evt, ui) {
|
||||||
|
input.removeClass("wbs-accepting-input");
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SchemaAlignmentDialog._inputContainerToJSON = function (inputContainer) {
|
||||||
|
return inputContainer.data().jsonValue;
|
||||||
|
};
|
||||||
|
|
||||||
SchemaAlignmentDialog._removeStatement = function(statement) {
|
SchemaAlignmentDialog._removeStatement = function(statement) {
|
||||||
var statementGroup = statement.parents('.wbs-statement-group').first();
|
var statementGroup = statement.parents('.wbs-statement-group').first();
|
||||||
statement.remove();
|
statement.remove();
|
||||||
@ -359,16 +443,13 @@ SchemaAlignmentDialog._addStatement = function() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
SchemaAlignmentDialog.getJSON = function() {
|
SchemaAlignmentDialog.getJSON = function() {
|
||||||
var rootNodes = [];
|
var list = new Array();
|
||||||
for (var i = 0; i < this._nodeUIs.length; i++) {
|
$('.wbs-item').each(function () {
|
||||||
var node = this._nodeUIs[i].getJSON();
|
list.push(SchemaAlignmentDialog._itemToJSON($(this)));
|
||||||
if (node !== null) {
|
});
|
||||||
rootNodes.push(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
rootNodes: rootNodes
|
'items': list,
|
||||||
|
'wikibasePrefix': this._wikibasePrefix,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -51,20 +51,30 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
padding: 5px;
|
padding: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.schema-alignment-draggable-column {
|
.wbs-draggable-column {
|
||||||
border: 1px solid #aaa;
|
border: 1px solid #aaa;
|
||||||
padding: 2px;
|
padding: 2px;
|
||||||
margin: 2px;
|
margin: 2px;
|
||||||
background-color: #eee;
|
background-color: #eee;
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
|
padding-bottom: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wbs-reconciled-column {
|
||||||
|
border-bottom: 3px solid #282;
|
||||||
|
padding-bottom: 1px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wbs-accepting-input {
|
||||||
|
box-shadow: inset 0 0 10px #a7cdff;
|
||||||
}
|
}
|
||||||
|
|
||||||
.wbs-item-input, .wbs-prop-input, .wbs-target-input {
|
.wbs-item-input, .wbs-prop-input, .wbs-target-input {
|
||||||
width: 160px;
|
width: 160px;
|
||||||
height: 20px;
|
height: 20px;
|
||||||
border: 1px solid #a2a9b1;
|
/* border: 1px solid #a2a9b1; */
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
background: white;
|
/* background: white; */
|
||||||
margin: 5px;
|
margin: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,6 +82,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
width: 120px;
|
width: 120px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.wbs-item-input input, .wbs-prop-input input, .wbs-target-input input {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wbs-draggable-column img {
|
||||||
|
float: right;
|
||||||
|
}
|
||||||
|
|
||||||
.wbs-toolbar {
|
.wbs-toolbar {
|
||||||
float: right;
|
float: right;
|
||||||
width: 100px;
|
width: 100px;
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
package org.openrefine.wikidata.commands;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.openrefine.wikidata.operations.SaveWikibaseSchemaOperation;
|
||||||
|
import org.openrefine.wikidata.schema.WikibaseSchema;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import com.google.refine.commands.Command;
|
||||||
|
import com.google.refine.model.AbstractOperation;
|
||||||
|
import com.google.refine.model.Project;
|
||||||
|
import com.google.refine.process.Process;
|
||||||
|
import com.google.refine.util.ParsingUtilities;
|
||||||
|
|
||||||
|
public class SaveWikibaseSchemaCommand extends Command {
|
||||||
|
|
||||||
|
public SaveWikibaseSchemaCommand() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
Project project = getProject(request);
|
||||||
|
|
||||||
|
String jsonString = request.getParameter("schema");
|
||||||
|
JSONObject json = ParsingUtilities.evaluateJsonStringToObject(jsonString);
|
||||||
|
WikibaseSchema schema = WikibaseSchema.reconstruct(json);
|
||||||
|
|
||||||
|
AbstractOperation op = new SaveWikibaseSchemaOperation(schema);
|
||||||
|
Process process = op.createProcess(project, new Properties());
|
||||||
|
|
||||||
|
performProcessAndRespond(request, response, project, process);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
respondException(response, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user