2010-02-22 19:42:25 +01:00
|
|
|
package com.metaweb.gridworks.operations;
|
2010-02-05 00:38:40 +01:00
|
|
|
|
2010-03-04 20:59:31 +01:00
|
|
|
import java.io.Serializable;
|
2010-02-05 00:38:40 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
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.Engine;
|
|
|
|
import com.metaweb.gridworks.browsing.FilteredRows;
|
|
|
|
import com.metaweb.gridworks.browsing.RowVisitor;
|
|
|
|
import com.metaweb.gridworks.expr.Evaluable;
|
|
|
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
2010-03-01 02:50:56 +01:00
|
|
|
import com.metaweb.gridworks.expr.MetaParser;
|
2010-02-05 00:38:40 +01:00
|
|
|
import com.metaweb.gridworks.history.Change;
|
|
|
|
import com.metaweb.gridworks.history.HistoryEntry;
|
2010-02-22 05:23:25 +01:00
|
|
|
import com.metaweb.gridworks.model.AbstractOperation;
|
2010-02-05 00:38:40 +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.CellAtRow;
|
|
|
|
import com.metaweb.gridworks.model.changes.ColumnAdditionChange;
|
|
|
|
|
|
|
|
public class ColumnAdditionOperation extends EngineDependentOperation {
|
2010-03-04 01:32:54 +01:00
|
|
|
final protected String _baseColumnName;
|
2010-03-03 05:19:58 +01:00
|
|
|
final protected String _expression;
|
2010-03-04 01:32:54 +01:00
|
|
|
final protected OnError _onError;
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
final protected String _newColumnName;
|
2010-03-03 05:19:58 +01:00
|
|
|
final protected int _columnInsertIndex;
|
2010-02-05 00:38:40 +01:00
|
|
|
|
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 ColumnAdditionOperation(
|
|
|
|
engineConfig,
|
|
|
|
obj.getString("baseColumnName"),
|
|
|
|
obj.getString("expression"),
|
2010-03-04 01:32:54 +01:00
|
|
|
TextTransformOperation.stringToOnError(obj.getString("onError")),
|
2010-03-06 08:43:45 +01:00
|
|
|
obj.getString("newColumnName"),
|
2010-02-22 05:23:25 +01:00
|
|
|
obj.getInt("columnInsertIndex")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public ColumnAdditionOperation(
|
|
|
|
JSONObject engineConfig,
|
2010-03-04 01:32:54 +01:00
|
|
|
String baseColumnName,
|
2010-03-03 05:19:58 +01:00
|
|
|
String expression,
|
2010-03-04 01:32:54 +01:00
|
|
|
OnError onError,
|
2010-03-06 08:43:45 +01:00
|
|
|
String newColumnName,
|
2010-03-04 01:32:54 +01:00
|
|
|
int columnInsertIndex
|
2010-03-03 05:19:58 +01:00
|
|
|
) {
|
|
|
|
super(engineConfig);
|
|
|
|
|
|
|
|
_baseColumnName = baseColumnName;
|
|
|
|
_expression = expression;
|
2010-03-04 01:32:54 +01:00
|
|
|
_onError = onError;
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
_newColumnName = newColumnName;
|
2010-03-03 05:19:58 +01:00
|
|
|
_columnInsertIndex = columnInsertIndex;
|
|
|
|
}
|
2010-02-05 00:38:40 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
|
|
|
writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass()));
|
|
|
|
writer.key("description"); writer.value(getBriefDescription(null));
|
|
|
|
writer.key("engineConfig"); writer.value(getEngineConfig());
|
2010-03-06 08:43:45 +01:00
|
|
|
writer.key("newColumnName"); writer.value(_newColumnName);
|
2010-03-03 05:19:58 +01:00
|
|
|
writer.key("columnInsertIndex"); writer.value(_columnInsertIndex);
|
|
|
|
writer.key("baseColumnName"); writer.value(_baseColumnName);
|
|
|
|
writer.key("expression"); writer.value(_expression);
|
2010-03-04 01:32:54 +01:00
|
|
|
writer.key("onError"); writer.value(TextTransformOperation.onErrorToString(_onError));
|
2010-03-03 05:19:58 +01:00
|
|
|
writer.endObject();
|
|
|
|
}
|
2010-02-05 00:38:40 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
protected String getBriefDescription(Project project) {
|
2010-03-06 08:43:45 +01:00
|
|
|
return "Create column " + _newColumnName +
|
2010-02-22 05:23:25 +01:00
|
|
|
" at index " + _columnInsertIndex +
|
|
|
|
" based on column " + _baseColumnName +
|
|
|
|
" using expression " + _expression;
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
2010-02-19 00:27:40 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
protected String createDescription(Column column, List<CellAtRow> cellsAtRows) {
|
2010-03-06 08:43:45 +01:00
|
|
|
return "Create new column " + _newColumnName +
|
|
|
|
" based on column " + column.getName() +
|
2010-03-03 05:19:58 +01:00
|
|
|
" by filling " + cellsAtRows.size() +
|
|
|
|
" rows with " + _expression;
|
|
|
|
}
|
|
|
|
|
2010-02-22 05:23:25 +01:00
|
|
|
protected HistoryEntry createHistoryEntry(Project project) throws Exception {
|
|
|
|
Engine engine = createEngine(project);
|
|
|
|
|
|
|
|
Column column = project.columnModel.getColumnByName(_baseColumnName);
|
|
|
|
if (column == null) {
|
|
|
|
throw new Exception("No column named " + _baseColumnName);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<CellAtRow> cellsAtRows = new ArrayList<CellAtRow>(project.rows.size());
|
|
|
|
|
|
|
|
FilteredRows filteredRows = engine.getAllFilteredRows(false);
|
|
|
|
filteredRows.accept(project, createRowVisitor(project, cellsAtRows));
|
|
|
|
|
|
|
|
String description = createDescription(column, cellsAtRows);
|
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
Change change = new ColumnAdditionChange(_newColumnName, _columnInsertIndex, cellsAtRows);
|
2010-02-22 05:23:25 +01:00
|
|
|
|
|
|
|
return new HistoryEntry(
|
|
|
|
project, description, this, change);
|
|
|
|
}
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
protected RowVisitor createRowVisitor(Project project, List<CellAtRow> cellsAtRows) throws Exception {
|
|
|
|
Column column = project.columnModel.getColumnByName(_baseColumnName);
|
|
|
|
|
|
|
|
Evaluable eval = MetaParser.parse(_expression);
|
2010-02-05 00:38:40 +01:00
|
|
|
Properties bindings = ExpressionUtils.createBindings(project);
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
return new RowVisitor() {
|
2010-03-12 02:06:23 +01:00
|
|
|
int cellIndex;
|
|
|
|
Properties bindings;
|
|
|
|
List<CellAtRow> cellsAtRows;
|
|
|
|
Evaluable eval;
|
2010-03-03 05:19:58 +01:00
|
|
|
|
|
|
|
public RowVisitor init(int cellIndex, Properties bindings, List<CellAtRow> cellsAtRows, Evaluable eval) {
|
|
|
|
this.cellIndex = cellIndex;
|
|
|
|
this.bindings = bindings;
|
|
|
|
this.cellsAtRows = cellsAtRows;
|
|
|
|
this.eval = eval;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-03-12 02:06:23 +01:00
|
|
|
public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) {
|
2010-03-03 05:19:58 +01:00
|
|
|
Cell cell = row.getCell(cellIndex);
|
2010-02-07 04:36:40 +01:00
|
|
|
|
2010-04-06 07:35:48 +02:00
|
|
|
ExpressionUtils.bind(bindings, row, rowIndex, _baseColumnName, cell);
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-03-04 20:59:31 +01:00
|
|
|
Serializable v = ExpressionUtils.wrapStorable(eval.evaluate(bindings));
|
2010-03-04 01:32:54 +01:00
|
|
|
if (ExpressionUtils.isError(v)) {
|
|
|
|
if (_onError == OnError.SetToBlank) {
|
|
|
|
return false;
|
|
|
|
} else if (_onError == OnError.KeepOriginal) {
|
|
|
|
v = cell != null ? cell.value : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v != null) {
|
2010-02-07 04:36:40 +01:00
|
|
|
Cell newCell = new Cell(v, null);
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-02-07 04:36:40 +01:00
|
|
|
cellsAtRows.add(new CellAtRow(rowIndex, newCell));
|
|
|
|
}
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}.init(column.getCellIndex(), bindings, cellsAtRows, eval);
|
|
|
|
}
|
2010-02-05 00:38:40 +01:00
|
|
|
}
|