More work on the extend data preview dialog: columns can now be removed.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@299 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-03-15 19:44:33 +00:00
parent d0f77a5ef8
commit c30a5126df
4 changed files with 131 additions and 19 deletions

View File

@ -21,6 +21,7 @@ import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob.ColumnInfo;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob.DataExtension;
public class PreviewExtendDataCommand extends Command {
@ -73,10 +74,23 @@ public class PreviewExtendDataCommand extends Command {
writer.key("code"); writer.value("ok");
writer.key("columns");
writer.array();
for (String name : job.columnNames) {
for (ColumnInfo info : job.columns) {
writer.object();
writer.key("names");
writer.array();
for (String name : info.names) {
writer.value(name);
}
writer.endArray();
writer.key("path");
writer.array();
for (String id : info.path) {
writer.value(id);
}
writer.endArray();
writer.endObject();
}
writer.endArray();
writer.key("rows");
writer.array();

View File

@ -33,14 +33,24 @@ public class FreebaseDataExtensionJob {
}
}
static public class ColumnInfo {
final public List<String> names;
final public List<String> path;
protected ColumnInfo(List<String> names, List<String> path) {
this.names = names;
this.path = path;
}
}
final public JSONObject extension;
final public int columnCount;
final public List<String> columnNames = new ArrayList<String>();
final public List<ColumnInfo> columns = new ArrayList<ColumnInfo>();
public FreebaseDataExtensionJob(JSONObject obj) throws JSONException {
this.extension = obj;
this.columnCount = (obj.has("properties") && !obj.isNull("properties")) ?
countColumns(obj.getJSONArray("properties"), columnNames) : 0;
countColumns(obj.getJSONArray("properties"), columns, new ArrayList<String>(), new ArrayList<String>()) : 0;
}
public Map<String, FreebaseDataExtensionJob.DataExtension> extend(Set<String> guids) throws Exception {
@ -177,7 +187,7 @@ public class FreebaseDataExtensionJob {
} else {
return new int[] {
startRowIndex,
startColumnIndex + countColumns(extNode, null)
startColumnIndex + countColumns(extNode, null, new ArrayList<String>(), new ArrayList<String>())
};
}
}
@ -295,26 +305,40 @@ public class FreebaseDataExtensionJob {
}
}
static protected int countColumns(JSONObject obj, List<String> columnNames) throws JSONException {
static protected int countColumns(JSONObject obj, List<ColumnInfo> columns, List<String> names, List<String> path) throws JSONException {
String name = obj.getString("name");
List<String> names2 = null;
List<String> path2 = null;
if (columns != null) {
names2 = new ArrayList<String>(names);
names2.add(name);
path2 = new ArrayList<String>(path);
path2.add(obj.getString("id"));
}
if (obj.has("properties") && !obj.isNull("properties")) {
boolean included = (obj.has("included") && obj.getBoolean("included"));
if (included && columnNames != null) {
columnNames.add(obj.getString("name"));
if (included && columns != null) {
columns.add(new ColumnInfo(names2, path2));
}
return (included ? 1 : 0) + countColumns(obj.getJSONArray("properties"), columnNames);
return (included ? 1 : 0) +
countColumns(obj.getJSONArray("properties"), columns, names2, path2);
} else {
if (columnNames != null) {
columnNames.add(obj.getString("name"));
if (columns != null) {
columns.add(new ColumnInfo(names2, path2));
}
return 1;
}
}
static protected int countColumns(JSONArray a, List<String> columnNames) throws JSONException {
static protected int countColumns(JSONArray a, List<ColumnInfo> columns, List<String> names, List<String> path) throws JSONException {
int c = 0;
int l = a.length();
for (int i = 0; i < l; i++) {
c += countColumns(a.getJSONObject(i), columnNames);
c += countColumns(a.getJSONObject(i), columns, names, path);
}
return c;
}

View File

@ -161,11 +161,71 @@ ExtendDataPreviewDialog.prototype._update = function() {
};
ExtendDataPreviewDialog.prototype._addProperty = function(p) {
this._extension.properties.push(p);
var addSeveralToList = function(properties, oldProperties) {
for (var i = 0; i < properties.length; i++) {
addToList(properties[i], oldProperties);
}
};
var addToList = function(property, oldProperties) {
for (var i = 0; i < oldProperties.length; i++) {
var oldProperty = oldProperties[i];
if (oldProperty.id == property.id) {
if ("included" in property) {
oldProperty.included = "included" in oldProperty ?
(oldProperty.included || property.included) :
property.included;
}
if ("properties" in property) {
if ("properties" in oldProperty) {
addSeveralToList(property.properties, oldProperty.properties);
} else {
oldProperty.properties = property.properties;
}
}
return;
}
}
oldProperties.push(property);
};
addToList(p, this._extension.properties);
this._update();
};
ExtendDataPreviewDialog.prototype._removeProperty = function(path) {
var removeFromList = function(path, index, properties) {
var id = path[index];
for (var i = properties.length - 1; i >= 0; i--) {
var property = properties[i];
if (property.id == id) {
if (index === path.length - 1) {
if ("included" in property) {
delete property.included;
}
} else if ("properties" in property && property.properties.length > 0) {
removeFromList(path, index + 1, property.properties);
}
if (!("properties" in property) || property.properties.length === 0) {
properties.splice(i, 1);
}
return;
}
}
};
removeFromList(path, 0, this._extension.properties);
this._update();
};
ExtendDataPreviewDialog.prototype._renderPreview = function(data) {
var self = this;
var container = this._elmts.previewContainer.empty();
if (data.code == "error") {
container.text("Error.");
@ -174,10 +234,19 @@ ExtendDataPreviewDialog.prototype._renderPreview = function(data) {
var table = $('<table>')[0];
var trHead = table.insertRow(table.rows.length);
$(trHead.insertCell(trHead.cells.length)).text(this._column.name);
$('<th>').appendTo(trHead).text(this._column.name);
for (var c = 0; c < data.columns.length; c++) {
$(trHead.insertCell(trHead.cells.length)).text(data.columns[c]);
var column = data.columns[c];
var th = $('<th>').appendTo(trHead);
$('<span>').html(column.names.join(" &raquo; ")).appendTo(th);
$('<img>')
.attr("src", "images/close.png")
.attr("title", "Remove this column")
.click(function() {
self._removeProperty(column.path);
}).appendTo(th);
}
for (var r = 0; r < data.rows.length; r++) {

View File

@ -25,7 +25,12 @@
border-collapse: collapse;
}
.extend-data-preview-dialog .preview-container td {
.extend-data-preview-dialog .preview-container td, .extend-data-preview-dialog .preview-container th {
padding: 3px 5px;
border-bottom: 1px solid #ddd;
}
.extend-data-preview-dialog .preview-container th img {
vertical-align: top;
margin-left: 5px;
}