Added an option for what to do when a text transform errors out. Made a custom expression preview dialog for the text transform command in order to suppor that option.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@178 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-03-03 22:12:48 +00:00
parent a3181bbd8f
commit b4d2cef526
5 changed files with 117 additions and 18 deletions

View File

@ -81,6 +81,8 @@ public class ProjectManager implements Serializable {
} }
static protected ProjectManager load(File file) { static protected ProjectManager load(File file) {
Gridworks.log("Loading project metadata from " + file.getAbsolutePath());
ProjectManager pm = null; ProjectManager pm = null;
FileInputStream fis = null; FileInputStream fis = null;
ObjectInputStream in = null; ObjectInputStream in = null;
@ -198,6 +200,8 @@ public class ProjectManager implements Serializable {
} }
public void save() { public void save() {
Gridworks.log("Saving project metadata ...");
File file = new File(_dir, "projects"); File file = new File(_dir, "projects");
FileOutputStream fos = null; FileOutputStream fos = null;
@ -226,6 +230,7 @@ public class ProjectManager implements Serializable {
} }
public void saveAllProjects() { public void saveAllProjects() {
Gridworks.log("Saving all projects ...");
for (Project project : _projects.values()) { for (Project project : _projects.values()) {
try { try {
saveProject(project); saveProject(project);

View File

@ -16,7 +16,12 @@ public class DoTextTransformCommand extends EngineDependentCommand {
String columnName = request.getParameter("columnName"); String columnName = request.getParameter("columnName");
String expression = request.getParameter("expression"); String expression = request.getParameter("expression");
String onError = request.getParameter("onError");
return new TextTransformOperation(engineConfig, columnName, expression); return new TextTransformOperation(
engineConfig,
columnName,
expression,
TextTransformOperation.stringToOnError(onError));
} }
} }

View File

@ -53,4 +53,14 @@ public class ExpressionUtils {
((Boolean) o).booleanValue() : ((Boolean) o).booleanValue() :
Boolean.parseBoolean(o.toString())); Boolean.parseBoolean(o.toString()));
} }
static public boolean sameValue(Object v1, Object v2) {
if (v1 == null) {
return (v2 == null) || (v2 instanceof String && ((String) v2).length() == 0);
} else if (v2 == null) {
return (v1 == null) || (v1 instanceof String && ((String) v1).length() == 0);
} else {
return v1.equals(v2);
}
}
} }

View File

@ -20,8 +20,15 @@ import com.metaweb.gridworks.model.changes.CellChange;
public class TextTransformOperation extends EngineDependentMassCellOperation { public class TextTransformOperation extends EngineDependentMassCellOperation {
private static final long serialVersionUID = -7698202759999537298L; private static final long serialVersionUID = -7698202759999537298L;
static public enum OnError {
KeepOriginal,
SetToBlank,
StoreError
}
final protected String _expression; final protected String _expression;
final protected OnError _onError;
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
JSONObject engineConfig = obj.getJSONObject("engineConfig"); JSONObject engineConfig = obj.getJSONObject("engineConfig");
@ -29,13 +36,34 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
return new TextTransformOperation( return new TextTransformOperation(
engineConfig, engineConfig,
obj.getString("columnName"), obj.getString("columnName"),
obj.getString("expression") obj.getString("expression"),
stringToOnError(obj.getString("onError"))
); );
} }
public TextTransformOperation(JSONObject engineConfig, String columnName, String expression) { static public OnError stringToOnError(String s) {
if ("set-to-blank".equalsIgnoreCase(s)) {
return OnError.SetToBlank;
} else if ("store-error".equalsIgnoreCase(s)) {
return OnError.StoreError;
} else {
return OnError.KeepOriginal;
}
}
static public String onErrorToString(OnError onError) {
if (onError == OnError.SetToBlank) {
return "set-to-blank";
} else if (onError == OnError.StoreError) {
return "store-error";
} else {
return "keep-original";
}
}
public TextTransformOperation(JSONObject engineConfig, String columnName, String expression, OnError onError) {
super(engineConfig, columnName, true); super(engineConfig, columnName, true);
_expression = expression; _expression = expression;
_onError = onError;
} }
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
@ -47,6 +75,7 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
writer.key("engineConfig"); writer.value(getEngineConfig()); writer.key("engineConfig"); writer.value(getEngineConfig());
writer.key("columnName"); writer.value(_columnName); writer.key("columnName"); writer.value(_columnName);
writer.key("expression"); writer.value(_expression); writer.key("expression"); writer.value(_expression);
writer.key("onError"); writer.value(onErrorToString(_onError));
writer.endObject(); writer.endObject();
} }
@ -83,12 +112,21 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { public boolean visit(Project project, int rowIndex, Row row, boolean contextual) {
Cell cell = row.getCell(cellIndex); Cell cell = row.getCell(cellIndex);
Object oldValue = cell != null ? cell.value : null;
ExpressionUtils.bind(bindings, row, rowIndex, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object v = eval.evaluate(bindings); Object newValue = eval.evaluate(bindings);
if ((cell != null && cell.value != null) || v != null) { if (ExpressionUtils.isError(newValue)) {
Cell newCell = new Cell(v, (cell != null) ? cell.recon : null); if (_onError == OnError.KeepOriginal) {
return false;
} else if (_onError == OnError.SetToBlank) {
newValue = null;
}
}
if (!ExpressionUtils.sameValue(oldValue, newValue)) {
Cell newCell = new Cell(newValue, (cell != null) ? cell.recon : null);
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
cellChanges.add(cellChange); cellChanges.add(cellChange);

View File

@ -59,15 +59,15 @@ DataTableColumnHeaderUI.prototype._createMenuForColumnHeader = function(elmt) {
submenu: [ submenu: [
{ {
label: "To Titlecase", label: "To Titlecase",
click: function() { self._doTextTransform("toTitlecase(value)"); } click: function() { self._doTextTransform("toTitlecase(value)", "store-blank"); }
}, },
{ {
label: "To Uppercase", label: "To Uppercase",
click: function() { self._doTextTransform("toUppercase(value)"); } click: function() { self._doTextTransform("toUppercase(value)", "store-blank"); }
}, },
{ {
label: "To Lowercase", label: "To Lowercase",
click: function() { self._doTextTransform("toLowercase(value)"); } click: function() { self._doTextTransform("toLowercase(value)", "store-blank"); }
}, },
{ {
label: "Custom Transform ...", label: "Custom Transform ...",
@ -399,10 +399,10 @@ DataTableColumnHeaderUI.prototype._doFilterByExpressionPrompt = function(express
); );
}; };
DataTableColumnHeaderUI.prototype._doTextTransform = function(expression) { DataTableColumnHeaderUI.prototype._doTextTransform = function(expression, onError) {
Gridworks.postProcess( Gridworks.postProcess(
"do-text-transform", "do-text-transform",
{ columnName: this._column.headerLabel, expression: expression }, { columnName: this._column.headerLabel, expression: expression, onError: onError },
null, null,
{ cellsChanged: true } { cellsChanged: true }
); );
@ -410,13 +410,54 @@ DataTableColumnHeaderUI.prototype._doTextTransform = function(expression) {
DataTableColumnHeaderUI.prototype._doTextTransformPrompt = function() { DataTableColumnHeaderUI.prototype._doTextTransformPrompt = function() {
var self = this; var self = this;
DataTableView.promptExpressionOnVisibleRows( var frame = DialogSystem.createDialog();
this._column, frame.width("700px");
"Custom Transform on " + this._column.headerLabel,
"value", var header = $('<div></div>').addClass("dialog-header").text("Custom text transform on column " + this._column.headerLabel).appendTo(frame);
function(expression) { var body = $('<div></div>').addClass("dialog-body").appendTo(frame);
self._doTextTransform(expression); var footer = $('<div></div>').addClass("dialog-footer").appendTo(frame);
}
body.html(ExpressionPreviewDialog.generateWidgetHtml());
var bodyElmts = DOM.bind(body);
footer.html(
'<table class="expression-preview-layout">' +
'<tr>' +
'<td align="left">' +
'On error ' +
'<input type="radio" name="text-transform-dialog-onerror-choice" value="set-to-blank" checked /> set to blank ' +
'<input type="radio" name="text-transform-dialog-onerror-choice" value="store-error" /> store error ' +
'<input type="radio" name="text-transform-dialog-onerror-choice" value="keep-original" /> keep original' +
'</td>' +
'<td align="right">' +
'<button bind="okButton">&nbsp;&nbsp;OK&nbsp;&nbsp;</button>' +
'<button bind="cancelButton">Cancel</button>' +
'</td>' +
'</tr>' +
'</table>');
var footerElmts = DOM.bind(footer);
var level = DialogSystem.showDialog(frame);
var dismiss = function() {
DialogSystem.dismissUntil(level - 1);
};
footerElmts.okButton.click(function() {
self._doTextTransform(
previewWidget.getExpression(true),
$('input[name="text-transform-dialog-onerror-choice"]:checked')[0].value
);
dismiss();
})
footerElmts.cancelButton.click(dismiss);
var o = DataTableView.sampleVisibleRows(this._column);
var previewWidget = new ExpressionPreviewDialog.Widget(
bodyElmts,
this._column.cellIndex,
o.rowIndices,
o.values,
"value"
); );
}; };