From eaef7b2394e3da115479faaa3068695c1505cac3 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Thu, 4 Mar 2010 00:32:54 +0000 Subject: [PATCH] Also let user decide what to do on expression evaluation error when creating a new column. git-svn-id: http://google-refine.googlecode.com/svn/trunk@182 7d457c2a-affb-35e4-300a-418c747d4874 --- .../commands/edit/AddColumnCommand.java | 3 +++ .../operations/ColumnAdditionOperation.java | 21 +++++++++++++++---- .../metaweb/gridworks/operations/OnError.java | 10 +++++++++ .../operations/TextTransformOperation.java | 6 ------ .../views/data-table-column-header-ui.js | 13 +++++++++++- 5 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/metaweb/gridworks/operations/OnError.java diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/AddColumnCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/AddColumnCommand.java index c0d33ddba..2dc4863c2 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/AddColumnCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/AddColumnCommand.java @@ -8,6 +8,7 @@ import com.metaweb.gridworks.commands.EngineDependentCommand; import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.operations.ColumnAdditionOperation; +import com.metaweb.gridworks.operations.TextTransformOperation; public class AddColumnCommand extends EngineDependentCommand { @Override @@ -18,11 +19,13 @@ public class AddColumnCommand extends EngineDependentCommand { String expression = request.getParameter("expression"); String headerLabel = request.getParameter("headerLabel"); int columnInsertIndex = Integer.parseInt(request.getParameter("columnInsertIndex")); + String onError = request.getParameter("onError"); return new ColumnAdditionOperation( engineConfig, baseColumnName, expression, + TextTransformOperation.stringToOnError(onError), headerLabel, columnInsertIndex ); diff --git a/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java b/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java index 4c572e53b..8010f51d9 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java @@ -27,8 +27,9 @@ import com.metaweb.gridworks.model.changes.ColumnAdditionChange; public class ColumnAdditionOperation extends EngineDependentOperation { private static final long serialVersionUID = -5672677479629932356L; - final protected String _baseColumnName; + final protected String _baseColumnName; final protected String _expression; + final protected OnError _onError; final protected String _headerLabel; final protected int _columnInsertIndex; @@ -40,6 +41,7 @@ public class ColumnAdditionOperation extends EngineDependentOperation { engineConfig, obj.getString("baseColumnName"), obj.getString("expression"), + TextTransformOperation.stringToOnError(obj.getString("onError")), obj.getString("headerLabel"), obj.getInt("columnInsertIndex") ); @@ -47,15 +49,17 @@ public class ColumnAdditionOperation extends EngineDependentOperation { public ColumnAdditionOperation( JSONObject engineConfig, - String baseColumnName, + String baseColumnName, String expression, + OnError onError, String headerLabel, - int columnInsertIndex + int columnInsertIndex ) { super(engineConfig); _baseColumnName = baseColumnName; _expression = expression; + _onError = onError; _headerLabel = headerLabel; _columnInsertIndex = columnInsertIndex; @@ -72,6 +76,7 @@ public class ColumnAdditionOperation extends EngineDependentOperation { writer.key("columnInsertIndex"); writer.value(_columnInsertIndex); writer.key("baseColumnName"); writer.value(_baseColumnName); writer.key("expression"); writer.value(_expression); + writer.key("onError"); writer.value(TextTransformOperation.onErrorToString(_onError)); writer.endObject(); } @@ -136,7 +141,15 @@ public class ColumnAdditionOperation extends EngineDependentOperation { ExpressionUtils.bind(bindings, row, rowIndex, cell); Object v = eval.evaluate(bindings); - if (ExpressionUtils.isNonBlankData(v)) { + if (ExpressionUtils.isError(v)) { + if (_onError == OnError.SetToBlank) { + return false; + } else if (_onError == OnError.KeepOriginal) { + v = cell != null ? cell.value : null; + } + } + + if (v != null) { Cell newCell = new Cell(v, null); cellsAtRows.add(new CellAtRow(rowIndex, newCell)); diff --git a/src/main/java/com/metaweb/gridworks/operations/OnError.java b/src/main/java/com/metaweb/gridworks/operations/OnError.java new file mode 100644 index 000000000..072842ed5 --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/operations/OnError.java @@ -0,0 +1,10 @@ +/** + * + */ +package com.metaweb.gridworks.operations; + +public enum OnError { + KeepOriginal, + SetToBlank, + StoreError +} \ No newline at end of file diff --git a/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java b/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java index 28befc626..e27b59ab9 100644 --- a/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java @@ -21,12 +21,6 @@ import com.metaweb.gridworks.model.changes.CellChange; public class TextTransformOperation extends EngineDependentMassCellOperation { private static final long serialVersionUID = -7698202759999537298L; - static public enum OnError { - KeepOriginal, - SetToBlank, - StoreError - } - final protected String _expression; final protected OnError _onError; final protected boolean _repeat; 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 8d1d2765b..eb66dc05f 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 @@ -622,6 +622,16 @@ DataTableColumnHeaderUI.prototype._doAddColumn = function(initialExpression) { '' + '' + '' + + '' + + '' + + 'On error' + + '' + + '' + + ' set to blank ' + + ' store error ' + + ' keep original' + + '' + + '' + '' + '' + ExpressionPreviewDialog.generateWidgetHtml() + '' + '' + @@ -653,7 +663,8 @@ DataTableColumnHeaderUI.prototype._doAddColumn = function(initialExpression) { baseColumnName: self._column.headerLabel, expression: previewWidget.getExpression(true), headerLabel: columnName, - columnInsertIndex: self._columnIndex + 1 + columnInsertIndex: self._columnIndex + 1, + onError: $('input[name="create-column-dialog-onerror-choice"]:checked')[0].value }, null, { modelsChanged: true }