From 99ae7dea29e4587a914f4879f9897ab40c4a1351 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Sat, 13 Mar 2010 08:08:25 +0000 Subject: [PATCH] More work on the extend data preview dialog. It's starting to render some results. git-svn-id: http://google-refine.googlecode.com/svn/trunk@290 7d457c2a-affb-35e4-300a-418c747d4874 --- .../util/PreviewExtendDataCommand.java | 1 + .../util/FreebaseDataExtensionJob.java | 42 ++++++---- .../dialogs/extend-data-preview-dialog.js | 82 +++++++++++++++++-- .../webapp/scripts/util/custom-suggest.js | 10 ++- .../views/data-table-column-header-ui.js | 2 +- 5 files changed, 110 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/metaweb/gridworks/commands/util/PreviewExtendDataCommand.java b/src/main/java/com/metaweb/gridworks/commands/util/PreviewExtendDataCommand.java index 32d5276cb..a3355be27 100644 --- a/src/main/java/com/metaweb/gridworks/commands/util/PreviewExtendDataCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/util/PreviewExtendDataCommand.java @@ -70,6 +70,7 @@ public class PreviewExtendDataCommand extends Command { JSONWriter writer = new JSONWriter(response.getWriter()); writer.object(); + writer.key("code"); writer.value("ok"); writer.key("columns"); writer.array(); for (String name : job.columnNames) { diff --git a/src/main/java/com/metaweb/gridworks/util/FreebaseDataExtensionJob.java b/src/main/java/com/metaweb/gridworks/util/FreebaseDataExtensionJob.java index 83bdb803a..00e23e8a2 100644 --- a/src/main/java/com/metaweb/gridworks/util/FreebaseDataExtensionJob.java +++ b/src/main/java/com/metaweb/gridworks/util/FreebaseDataExtensionJob.java @@ -76,7 +76,7 @@ public class FreebaseDataExtensionJob { protected FreebaseDataExtensionJob.DataExtension collectResult(JSONObject obj) throws JSONException { List rows = new ArrayList(); - collectResult(rows, extension, obj, 0, 0); + collectResult(rows, extension.getJSONArray("properties"), obj, 0, 0); Object[][] data = new Object[rows.size()][columnCount]; rows.toArray(data); @@ -157,16 +157,18 @@ public class FreebaseDataExtensionJob { storeCell(rows, startRowIndex2++, startColumnIndex2++, o); } - int[] rowcol = collectResult( - rows, - extNode.getJSONArray("properties"), - o, - startRowIndex, - startColumnIndex2 - ); - - startRowIndex = rowcol[0]; - maxColIndex = Math.max(maxColIndex, rowcol[1]); + if (hasSubProperties) { + int[] rowcol = collectResult( + rows, + extNode.getJSONArray("properties"), + o, + startRowIndex, + startColumnIndex2 + ); + + startRowIndex = rowcol[0]; + maxColIndex = Math.max(maxColIndex, rowcol[1]); + } } return new int[] { startRowIndex, maxColIndex }; @@ -210,6 +212,7 @@ static protected InputStream doPost(URL url, String name, String load) throws IO URLConnection connection = url.openConnection(); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setConnectTimeout(5000); + connection.setDoOutput(true); DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); try { @@ -242,7 +245,7 @@ static protected void formulateQuery(Set guids, JSONObject node, Writer } jsonWriter.endArray(); - formulateQueryNode(node, jsonWriter); + formulateQueryNode(node.getJSONArray("properties"), jsonWriter); jsonWriter.endObject(); jsonWriter.endArray(); @@ -270,12 +273,7 @@ static protected void formulateQueryNode(JSONObject node, JSONWriter writer) thr } if (hasSubProperties) { - JSONArray a = node.getJSONArray("properties"); - int l = a.length(); - - for (int i = 0; i < l; i++) { - formulateQueryNode(a.getJSONObject(i), writer); - } + formulateQueryNode(node.getJSONArray("properties"), writer); } } writer.endObject(); @@ -284,6 +282,14 @@ static protected void formulateQueryNode(JSONObject node, JSONWriter writer) thr writer.endArray(); } +static protected void formulateQueryNode(JSONArray propertiesA, JSONWriter writer) throws JSONException { + int l = propertiesA.length(); + + for (int i = 0; i < l; i++) { + formulateQueryNode(propertiesA.getJSONObject(i), writer); + } +} + static protected int countColumns(JSONObject obj, List columnNames) throws JSONException { if (obj.has("properties") && !obj.isNull("properties")) { boolean included = (obj.has("included") && obj.getBoolean("included")); diff --git a/src/main/webapp/scripts/dialogs/extend-data-preview-dialog.js b/src/main/webapp/scripts/dialogs/extend-data-preview-dialog.js index 949592812..e602990f4 100644 --- a/src/main/webapp/scripts/dialogs/extend-data-preview-dialog.js +++ b/src/main/webapp/scripts/dialogs/extend-data-preview-dialog.js @@ -1,21 +1,25 @@ -function ExtendDataPreviewDialog(columnName, rowIndices, onDone) { +function ExtendDataPreviewDialog(column, rowIndices, onDone) { + this._column = column; + this._rowIndices = rowIndices; this._onDone = onDone; + this._extension = { properties: [] }; var self = this; var frame = DialogSystem.createDialog(); frame.width("900px").addClass("extend-data-preview-dialog"); - var header = $('
').addClass("dialog-header").text("Add Columns from Freebase Based on Column " + columnName).appendTo(frame); + var header = $('
').addClass("dialog-header").text("Add Columns from Freebase Based on Column " + column.name).appendTo(frame); var body = $('
').addClass("dialog-body").appendTo(frame); var footer = $('
').addClass("dialog-footer").appendTo(frame); var html = $( '
' + '' + '' + - '' + + '' + '' + '' + - '' + + '' + + '' + '' + '' + '' + @@ -38,8 +42,74 @@ function ExtendDataPreviewDialog(columnName, rowIndices, onDone) { }).appendTo(footer); this._level = DialogSystem.showDialog(frame); + + var suggestConfig = { + type: '/type/property' + }; + if ("reconConfig" in column) { + suggestConfig.schema = column.reconConfig.type.id; + } + this._elmts.addPropertyInput.suggestP(suggestConfig).bind("fb-select", function(evt, data) { + self._addProperty(data); + }); }; -ExtendDataPreviewDialog.prototype.update = function() { +ExtendDataPreviewDialog.prototype._update = function() { + this._elmts.previewContainer.empty().text("Querying Freebase ..."); -}; \ No newline at end of file + var self = this; + var params = { + project: theProject.id, + columnName: this._column.name + }; + + $.post( + "/command/preview-extend-data?" + $.param(params), + { + rowIndices: JSON.stringify(this._rowIndices), + extension: JSON.stringify(this._extension) + }, + function(data) { + self._renderPreview(data) + }, + "json" + ); +}; + +ExtendDataPreviewDialog.prototype._addProperty = function(p) { + this._extension.properties.push({ + id : p.id, + name: p.name, + expected: p["/type/property/expected_type"] + }); + this._update(); +}; + +ExtendDataPreviewDialog.prototype._renderPreview = function(data) { + var container = this._elmts.previewContainer.empty(); + if (data.code == "error") { + container.text("Error."); + return; + } + + var table = $('
Add Property
Preview
Suggested Properties
')[0]; + + for (var r = 0; r < data.rows.length; r++) { + var tr = table.insertRow(table.rows.length); + var row = data.rows[r]; + + for (var c = 0; c < row.length; c++) { + var td = tr.insertCell(tr.cells.length); + var cell = row[c]; + if (cell != null) { + if ($.isPlainObject(cell)) { + $('').attr("href", "http://www.freebase.com/view" + cell.id).text(cell.name).appendTo(td); + } else { + $('').text(cell).appendTo(td); + } + } + } + } + + container.append(table); +} \ No newline at end of file diff --git a/src/main/webapp/scripts/util/custom-suggest.js b/src/main/webapp/scripts/util/custom-suggest.js index da294ebc8..024f844b8 100644 --- a/src/main/webapp/scripts/util/custom-suggest.js +++ b/src/main/webapp/scripts/util/custom-suggest.js @@ -66,7 +66,8 @@ "id" : val, "name" : null, "guid" : null, - "x:type" : "/type/property" + "x:type" : "/type/property", + "/type/property/expected_type" : null }]; var data = { query: JSON.stringify({ query: query }) @@ -122,7 +123,12 @@ var o = this.options; var data = { - query: val + query: val, + mql_output: JSON.stringify([{ + "id" : null, + "name" : null, + "/type/property/expected_type" : null + }]) }; if (start) { data.start = start; diff --git a/src/main/webapp/scripts/views/data-table-column-header-ui.js b/src/main/webapp/scripts/views/data-table-column-header-ui.js index ff9ec0639..df34dea9e 100644 --- a/src/main/webapp/scripts/views/data-table-column-header-ui.js +++ b/src/main/webapp/scripts/views/data-table-column-header-ui.js @@ -822,7 +822,7 @@ DataTableColumnHeaderUI.prototype._doAddColumnFromFreebase = function() { var o = DataTableView.sampleVisibleRows(this._column); new ExtendDataPreviewDialog( - this._column.name, + this._column, o.rowIndices, function() {} );