![David Huynh](/assets/img/avatar_default.png)
git-svn-id: http://google-refine.googlecode.com/svn/trunk@92 7d457c2a-affb-35e4-300a-418c747d4874
210 lines
6.2 KiB
JavaScript
210 lines
6.2 KiB
JavaScript
var SchemaAlignment = {};
|
|
|
|
SchemaAlignment.autoAlign = function() {
|
|
var protograph = {};
|
|
|
|
var columns = theProject.columnModel.columns;
|
|
|
|
var typedCandidates = [];
|
|
var candidates = [];
|
|
|
|
for (var c = 0; c < columns.length; c++) {
|
|
var column = columns[c];
|
|
var typed = "reconConfig" in column && column.reconConfig != null;
|
|
var candidate = {
|
|
status: "unbound",
|
|
typed: typed,
|
|
index: c,
|
|
column: column
|
|
};
|
|
|
|
candidates.push(candidate);
|
|
if (typed) {
|
|
typedCandidates.push(candidate);
|
|
}
|
|
}
|
|
|
|
if (typedCandidates.length > 0) {
|
|
|
|
} else {
|
|
var queries = {};
|
|
for (var i = 0; i < candidates.length; i++) {
|
|
var candidate = candidates[i];
|
|
var name = SchemaAlignment._cleanName(candidate.column.headerLabel);
|
|
var key = "t" + i + ":search";
|
|
queries[key] = {
|
|
"query" : name,
|
|
"limit" : 10,
|
|
"type" : "/type/type,/type/property",
|
|
"type_strict" : "any"
|
|
};
|
|
}
|
|
|
|
SchemaAlignment._batchSearch(queries, function(result) {
|
|
console.log(result);
|
|
});
|
|
}
|
|
};
|
|
|
|
SchemaAlignment._batchSearch = function(queries, onDone) {
|
|
var keys = [];
|
|
for (var n in queries) {
|
|
if (queries.hasOwnProperty(n)) {
|
|
keys.push(n);
|
|
}
|
|
}
|
|
|
|
var result = {};
|
|
var args = [];
|
|
var makeBatch = function(keyBatch) {
|
|
var batch = {};
|
|
for (var k = 0; k < keyBatch.length; k++) {
|
|
var key = keyBatch[k];
|
|
batch[key] = queries[key];
|
|
}
|
|
|
|
args.push("http://api.freebase.com/api/service/search?" +
|
|
$.param({ "queries" : JSON.stringify(batch) }) + "&callback=?");
|
|
|
|
args.push(null); // no data
|
|
args.push(function(data) {
|
|
for (var k = 0; k < keyBatch.length; k++) {
|
|
var key = keyBatch[k];
|
|
result[key] = data[key];
|
|
}
|
|
});
|
|
};
|
|
|
|
for (var i = 0; i < keys.length; i += 10) {
|
|
makeBatch(keys.slice(i, i + 10));
|
|
}
|
|
|
|
args.push(function() {
|
|
onDone(result);
|
|
})
|
|
|
|
Ajax.chainGetJSON.apply(null, args);
|
|
};
|
|
|
|
SchemaAlignment._cleanName = function(s) {
|
|
return s.replace(/\W/g, " ").replace(/\s+/g, " ").toLowerCase();
|
|
}
|
|
|
|
function SchemaAlignmentDialog(protograph, onDone) {
|
|
this._onDone = onDone;
|
|
this._originalProtograph = protograph || { rootNodes: [] };
|
|
this._protograph = cloneDeep(protograph); // this is what can be munched on
|
|
|
|
if (this._protograph.rootNodes.length == 0) {
|
|
this._protograph.rootNodes.push({
|
|
nodeType: "cell-as-topic",
|
|
links: [
|
|
]
|
|
});
|
|
}
|
|
|
|
this._nodeUIs = [];
|
|
this._createDialog();
|
|
};
|
|
|
|
SchemaAlignmentDialog.prototype._createDialog = function() {
|
|
var self = this;
|
|
var frame = DialogSystem.createDialog();
|
|
|
|
frame.width("1000px");
|
|
|
|
var header = $('<div></div>').addClass("dialog-header").text("Schema Alignment").appendTo(frame);
|
|
var body = $('<div></div>').addClass("dialog-body").appendTo(frame);
|
|
var footer = $('<div></div>').addClass("dialog-footer").appendTo(frame);
|
|
|
|
this._renderFooter(footer);
|
|
this._renderBody(body);
|
|
|
|
this._level = DialogSystem.showDialog(frame);
|
|
};
|
|
|
|
SchemaAlignmentDialog.prototype._renderFooter = function(footer) {
|
|
var self = this;
|
|
|
|
$('<button></button>').html(" OK ").click(function() {
|
|
var protograph = self.getJSON();
|
|
|
|
$.post(
|
|
"/command/save-protograph?" + $.param({ project: theProject.id }),
|
|
{ protograph: JSON.stringify(protograph) },
|
|
function(data) {
|
|
if (data.code == "error") {
|
|
alert("Failed to save protograph");
|
|
return;
|
|
} else if (data.code == "ok") {
|
|
ui.historyWidget.update();
|
|
} else {
|
|
ui.processWidget.update();
|
|
}
|
|
|
|
DialogSystem.dismissUntil(self._level - 1);
|
|
|
|
theProject.protograph = protograph;
|
|
|
|
self._onDone(protograph);
|
|
},
|
|
"json"
|
|
);
|
|
}).appendTo(footer);
|
|
|
|
$('<button></button>').text("Cancel").click(function() {
|
|
DialogSystem.dismissUntil(self._level - 1);
|
|
}).appendTo(footer);
|
|
};
|
|
|
|
SchemaAlignmentDialog.prototype._renderBody = function(body) {
|
|
var self = this;
|
|
|
|
$('<p>' +
|
|
'The protograph serves as a skeleton for the graph-shaped data that will get generated ' +
|
|
'from your grid-shaped data and written into Freebase. The cells in each record of your data will ' +
|
|
'get placed into nodes within the protograph. Configure the protograph by specifying which ' +
|
|
'column to substitute into which node. A node can also be an automatically generated ' +
|
|
'anonymous node, or it can be an explicit value or topic that is the same for all records.' +
|
|
'</p>').appendTo(body);
|
|
|
|
this._canvas = $('<div></div>').addClass("schema-alignment-dialog-canvas").appendTo(body);
|
|
this._nodeTable = $('<table></table>').addClass("schema-alignment-table-layout").appendTo(this._canvas)[0];
|
|
|
|
for (var i = 0; i < this._protograph.rootNodes.length; i++) {
|
|
this._nodeUIs.push(new SchemaAlignmentDialog.UINode(
|
|
this._protograph.rootNodes[i],
|
|
this._nodeTable,
|
|
{
|
|
expanded: true,
|
|
mustBeCellTopic: true
|
|
}
|
|
));
|
|
}
|
|
};
|
|
|
|
SchemaAlignmentDialog.prototype.getJSON = function() {
|
|
var rootNodes = [];
|
|
for (var i = 0; i < this._nodeUIs.length; i++) {
|
|
var node = this._nodeUIs[i].getJSON();
|
|
if (node != null) {
|
|
rootNodes.push(node);
|
|
}
|
|
}
|
|
|
|
return {
|
|
rootNodes: rootNodes
|
|
};
|
|
};
|
|
|
|
SchemaAlignmentDialog._findColumn = function(cellIndex) {
|
|
var columns = theProject.columnModel.columns;
|
|
for (var i = 0; i < columns.length; i++) {
|
|
var column = columns[i];
|
|
if (column.cellIndex == cellIndex) {
|
|
return column;
|
|
}
|
|
}
|
|
return null;
|
|
};
|