2010-02-22 19:42:25 +01:00
|
|
|
package com.metaweb.gridworks.operations;
|
2010-02-04 21:51:44 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONWriter;
|
|
|
|
|
|
|
|
import com.metaweb.gridworks.browsing.RowVisitor;
|
|
|
|
import com.metaweb.gridworks.expr.Evaluable;
|
|
|
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
|
|
|
import com.metaweb.gridworks.expr.Parser;
|
2010-02-22 05:23:25 +01:00
|
|
|
import com.metaweb.gridworks.model.AbstractOperation;
|
2010-02-04 21:51:44 +01:00
|
|
|
import com.metaweb.gridworks.model.Cell;
|
|
|
|
import com.metaweb.gridworks.model.Column;
|
|
|
|
import com.metaweb.gridworks.model.Project;
|
|
|
|
import com.metaweb.gridworks.model.Row;
|
|
|
|
import com.metaweb.gridworks.model.changes.CellChange;
|
|
|
|
|
|
|
|
public class TextTransformOperation extends EngineDependentMassCellOperation {
|
|
|
|
private static final long serialVersionUID = -7698202759999537298L;
|
|
|
|
|
|
|
|
final protected String _expression;
|
|
|
|
|
2010-02-22 05:23:25 +01:00
|
|
|
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
|
|
|
|
JSONObject engineConfig = obj.getJSONObject("engineConfig");
|
|
|
|
|
|
|
|
return new TextTransformOperation(
|
|
|
|
engineConfig,
|
|
|
|
obj.getString("columnName"),
|
|
|
|
obj.getString("expression")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-02-17 01:26:38 +01:00
|
|
|
public TextTransformOperation(JSONObject engineConfig, String columnName, String expression) {
|
|
|
|
super(engineConfig, columnName, true);
|
2010-02-04 21:51:44 +01:00
|
|
|
_expression = expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
2010-02-17 02:30:09 +01:00
|
|
|
|
|
|
|
writer.object();
|
2010-02-22 05:23:25 +01:00
|
|
|
writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass()));
|
|
|
|
writer.key("description"); writer.value(getBriefDescription());
|
2010-02-17 02:40:41 +01:00
|
|
|
writer.key("engineConfig"); writer.value(getEngineConfig());
|
2010-02-17 02:30:09 +01:00
|
|
|
writer.key("columnName"); writer.value(_columnName);
|
|
|
|
writer.key("expression"); writer.value(_expression);
|
|
|
|
writer.endObject();
|
2010-02-04 21:51:44 +01:00
|
|
|
}
|
|
|
|
|
2010-02-19 00:27:40 +01:00
|
|
|
protected String getBriefDescription() {
|
2010-02-22 05:23:25 +01:00
|
|
|
return "Text transform on cells in column " + _columnName + " using expression " + _expression;
|
2010-02-19 00:27:40 +01:00
|
|
|
}
|
|
|
|
|
2010-02-04 21:51:44 +01:00
|
|
|
protected String createDescription(Column column,
|
|
|
|
List<CellChange> cellChanges) {
|
|
|
|
|
|
|
|
return "Text transform on " + cellChanges.size() +
|
|
|
|
" cells in column " + column.getHeaderLabel() + ": " + _expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges) throws Exception {
|
2010-02-17 01:26:38 +01:00
|
|
|
Column column = project.columnModel.getColumnByName(_columnName);
|
|
|
|
|
2010-02-04 21:51:44 +01:00
|
|
|
Evaluable eval = new Parser(_expression).getExpression();
|
|
|
|
Properties bindings = ExpressionUtils.createBindings(project);
|
|
|
|
|
|
|
|
return new RowVisitor() {
|
|
|
|
int cellIndex;
|
|
|
|
Properties bindings;
|
|
|
|
List<CellChange> cellChanges;
|
|
|
|
Evaluable eval;
|
|
|
|
|
|
|
|
public RowVisitor init(int cellIndex, Properties bindings, List<CellChange> cellChanges, Evaluable eval) {
|
|
|
|
this.cellIndex = cellIndex;
|
|
|
|
this.bindings = bindings;
|
|
|
|
this.cellChanges = cellChanges;
|
|
|
|
this.eval = eval;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-02-06 00:05:00 +01:00
|
|
|
public boolean visit(Project project, int rowIndex, Row row, boolean contextual) {
|
2010-02-07 04:36:40 +01:00
|
|
|
Cell cell = row.getCell(cellIndex);
|
|
|
|
|
2010-02-23 00:51:44 +01:00
|
|
|
ExpressionUtils.bind(bindings, row, rowIndex, cell);
|
2010-02-07 04:36:40 +01:00
|
|
|
|
|
|
|
Object v = eval.evaluate(bindings);
|
|
|
|
if ((cell != null && cell.value != null) || v != null) {
|
2010-02-28 01:55:09 +01:00
|
|
|
Cell newCell = new Cell(v, (cell != null) ? cell.recon : null);
|
2010-02-07 04:36:40 +01:00
|
|
|
|
|
|
|
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
|
|
|
|
cellChanges.add(cellChange);
|
|
|
|
}
|
|
|
|
|
2010-02-04 21:51:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 01:26:38 +01:00
|
|
|
}.init(column.getCellIndex(), bindings, cellChanges, eval);
|
2010-02-04 21:51:44 +01:00
|
|
|
}
|
|
|
|
}
|