Added repeat and repeatCount options for text transform operation. This lets us fix those & repeated encoding problems easily.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@179 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-03-04 00:00:46 +00:00
parent b4d2cef526
commit 2fe8f98e4e
5 changed files with 95 additions and 30 deletions

View File

@ -17,11 +17,22 @@ public class DoTextTransformCommand extends EngineDependentCommand {
String columnName = request.getParameter("columnName");
String expression = request.getParameter("expression");
String onError = request.getParameter("onError");
boolean repeat = "true".equals(request.getParameter("repeat"));
int repeatCount = 10;
String repeatCountString = request.getParameter("repeatCount");
try {
repeatCount = Math.max(Math.min(Integer.parseInt(repeatCountString), 10), 0);
} catch (Exception e) {
}
return new TextTransformOperation(
engineConfig,
columnName,
expression,
TextTransformOperation.stringToOnError(onError));
TextTransformOperation.stringToOnError(onError),
repeat,
repeatCount
);
}
}

View File

@ -27,8 +27,10 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
StoreError
}
final protected String _expression;
final protected String _expression;
final protected OnError _onError;
final protected boolean _repeat;
final protected int _repeatCount;
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
JSONObject engineConfig = obj.getJSONObject("engineConfig");
@ -37,7 +39,9 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
engineConfig,
obj.getString("columnName"),
obj.getString("expression"),
stringToOnError(obj.getString("onError"))
stringToOnError(obj.getString("onError")),
obj.getBoolean("repeat"),
obj.getInt("repeatCount")
);
}
@ -60,10 +64,19 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
}
}
public TextTransformOperation(JSONObject engineConfig, String columnName, String expression, OnError onError) {
public TextTransformOperation(
JSONObject engineConfig,
String columnName,
String expression,
OnError onError,
boolean repeat,
int repeatCount
) {
super(engineConfig, columnName, true);
_expression = expression;
_onError = onError;
_repeat = repeat;
_repeatCount = repeatCount;
}
public void write(JSONWriter writer, Properties options)
@ -76,6 +89,8 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
writer.key("columnName"); writer.value(_columnName);
writer.key("expression"); writer.value(_expression);
writer.key("onError"); writer.value(onErrorToString(_onError));
writer.key("repeat"); writer.value(_repeat);
writer.key("repeatCount"); writer.value(_repeatCount);
writer.endObject();
}
@ -128,6 +143,21 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
if (!ExpressionUtils.sameValue(oldValue, newValue)) {
Cell newCell = new Cell(newValue, (cell != null) ? cell.recon : null);
if (_repeat) {
for (int i = 0; i < _repeatCount; i++) {
ExpressionUtils.bind(bindings, row, rowIndex, newCell);
newValue = eval.evaluate(bindings);
if (ExpressionUtils.isError(newValue)) {
break;
} else if (ExpressionUtils.sameValue(newCell.value, newValue)) {
break;
}
newCell = new Cell(newValue, newCell.recon);
}
}
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
cellChanges.add(cellChange);
}

View File

@ -32,7 +32,7 @@ function ExpressionPreviewDialog(title, cellIndex, rowIndices, values, expressio
};
ExpressionPreviewDialog.generateWidgetHtml = function() {
return '<table class="expression-preview-layout">' +
return '<table class="expression-preview-layout" rows="4" cols="2">' +
'<tr>' +
'<td>Expression</td>' +
'<td>Language</td>' +
@ -48,7 +48,7 @@ ExpressionPreviewDialog.generateWidgetHtml = function() {
'</td>' +
'</tr>' +
'<tr>' +
'<td class="expression-preview-error-container" bind="expressionPreviewErrorContainer" width="150"></td>' +
'<td class="expression-preview-error-container" bind="expressionPreviewErrorContainer" width="150" style="vertical-align: top;"></td>' +
'</tr>' +
'<tr>' +
'<td colspan="2">' +
@ -61,7 +61,7 @@ ExpressionPreviewDialog.generateWidgetHtml = function() {
'<div id="expression-preview-tabs-preview">' +
'<div class="expression-preview-container" bind="expressionPreviewPreviewContainer"></div>' +
'</div>' +
'<div id="expression-preview-tabs-history">' +
'<div id="expression-preview-tabs-history" style="display: none;">' +
'<div class="expression-preview-container" bind="expressionPreviewHistoryContainer"></div>' +
'</div>' +
'<div id="expression-preview-tabs-help" style="display: none;">' +
@ -90,7 +90,7 @@ ExpressionPreviewDialog.Widget = function(
this._timerID = null;
$("#expression-preview-tabs").tabs();
$("#expression-preview-tabs-preview").css("display", "");
$("#expression-preview-tabs-history").css("display", "");
$("#expression-preview-tabs-help").css("display", "");
var self = this;

View File

@ -59,15 +59,15 @@ DataTableColumnHeaderUI.prototype._createMenuForColumnHeader = function(elmt) {
submenu: [
{
label: "To Titlecase",
click: function() { self._doTextTransform("toTitlecase(value)", "store-blank"); }
click: function() { self._doTextTransform("toTitlecase(value)", "store-blank", false, ""); }
},
{
label: "To Uppercase",
click: function() { self._doTextTransform("toUppercase(value)", "store-blank"); }
click: function() { self._doTextTransform("toUppercase(value)", "store-blank", false, ""); }
},
{
label: "To Lowercase",
click: function() { self._doTextTransform("toLowercase(value)", "store-blank"); }
click: function() { self._doTextTransform("toLowercase(value)", "store-blank", false, ""); }
},
{
label: "Custom Transform ...",
@ -399,10 +399,16 @@ DataTableColumnHeaderUI.prototype._doFilterByExpressionPrompt = function(express
);
};
DataTableColumnHeaderUI.prototype._doTextTransform = function(expression, onError) {
DataTableColumnHeaderUI.prototype._doTextTransform = function(expression, onError, repeat, repeatCount) {
Gridworks.postProcess(
"do-text-transform",
{ columnName: this._column.headerLabel, expression: expression, onError: onError },
{
columnName: this._column.headerLabel,
expression: expression,
onError: onError,
repeat: repeat,
repeatCount: repeatCount
},
null,
{ cellsChanged: true }
);
@ -417,24 +423,36 @@ DataTableColumnHeaderUI.prototype._doTextTransformPrompt = function() {
var body = $('<div></div>').addClass("dialog-body").appendTo(frame);
var footer = $('<div></div>').addClass("dialog-footer").appendTo(frame);
body.html(ExpressionPreviewDialog.generateWidgetHtml());
body.html(
'<table class="expression-preview-layout">' +
'<tr>' +
'<td colspan="4">' + ExpressionPreviewDialog.generateWidgetHtml() + '</td>' +
'</tr>' +
'<tr style="white-space: pre;">' +
'<td width="1%">' +
'On error' +
'</td>' +
'<td>' +
'<input type="radio" name="text-transform-dialog-onerror-choice" value="set-to-blank" checked /> set to blank<br/>' +
'<input type="radio" name="text-transform-dialog-onerror-choice" value="store-error" /> store error<br/>' +
'<input type="radio" name="text-transform-dialog-onerror-choice" value="keep-original" /> keep original' +
'</td>' +
'<td width="1%">' +
'<input type="checkbox" bind="repeatCheckbox" />' +
'</td>' +
'<td>' +
'Re-transform until no change<br/>' +
'up to <input bind="repeatCountInput" value="10" size="3" /> times' +
'</td>' +
'</tr>' +
'</table>'
);
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>');
'<button bind="okButton">&nbsp;&nbsp;OK&nbsp;&nbsp;</button>' +
'<button bind="cancelButton">Cancel</button>'
);
var footerElmts = DOM.bind(footer);
var level = DialogSystem.showDialog(frame);
@ -445,7 +463,9 @@ DataTableColumnHeaderUI.prototype._doTextTransformPrompt = function() {
footerElmts.okButton.click(function() {
self._doTextTransform(
previewWidget.getExpression(true),
$('input[name="text-transform-dialog-onerror-choice"]:checked')[0].value
$('input[name="text-transform-dialog-onerror-choice"]:checked')[0].value,
bodyElmts.repeatCheckbox[0].checked,
bodyElmts.repeatCountInput[0].value
);
dismiss();
})

View File

@ -14,7 +14,11 @@ table {
}
tr, td {
vertical-align: top;
vertical-align: baseline;
}
input {
vertical-align: middle;
}
#header {