2010-02-05 00:38:40 +01:00
|
|
|
package com.metaweb.gridworks.model.operations;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import com.metaweb.gridworks.expr.Parser;
|
|
|
|
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 {
|
|
|
|
private static final long serialVersionUID = -5672677479629932356L;
|
|
|
|
|
2010-02-17 01:26:38 +01:00
|
|
|
final protected String _baseColumnName;
|
2010-02-05 00:38:40 +01:00
|
|
|
final protected String _expression;
|
|
|
|
|
|
|
|
final protected String _headerLabel;
|
|
|
|
final protected int _columnInsertIndex;
|
|
|
|
|
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"),
|
|
|
|
obj.getString("headerLabel"),
|
|
|
|
obj.getInt("columnInsertIndex")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-02-05 00:38:40 +01:00
|
|
|
public ColumnAdditionOperation(
|
|
|
|
JSONObject engineConfig,
|
2010-02-17 01:26:38 +01:00
|
|
|
String baseColumnName,
|
2010-02-05 00:38:40 +01:00
|
|
|
String expression,
|
|
|
|
String headerLabel,
|
|
|
|
int columnInsertIndex
|
|
|
|
) {
|
|
|
|
super(engineConfig);
|
|
|
|
|
2010-02-17 01:26:38 +01:00
|
|
|
_baseColumnName = baseColumnName;
|
2010-02-05 00:38:40 +01:00
|
|
|
_expression = expression;
|
|
|
|
|
|
|
|
_headerLabel = headerLabel;
|
|
|
|
_columnInsertIndex = columnInsertIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
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("headerLabel"); writer.value(_headerLabel);
|
|
|
|
writer.key("columnInsertIndex"); writer.value(_columnInsertIndex);
|
|
|
|
writer.key("baseColumnName"); writer.value(_baseColumnName);
|
|
|
|
writer.key("expression"); writer.value(_expression);
|
|
|
|
writer.endObject();
|
2010-02-05 00:38:40 +01:00
|
|
|
}
|
|
|
|
|
2010-02-19 00:27:40 +01:00
|
|
|
protected String getBriefDescription() {
|
2010-02-22 05:23:25 +01:00
|
|
|
return "Create column " + _headerLabel +
|
|
|
|
" at index " + _columnInsertIndex +
|
|
|
|
" based on column " + _baseColumnName +
|
|
|
|
" using expression " + _expression;
|
2010-02-19 00:27:40 +01:00
|
|
|
}
|
|
|
|
|
2010-02-05 00:38:40 +01:00
|
|
|
protected String createDescription(Column column, List<CellAtRow> cellsAtRows) {
|
|
|
|
return "Create new column " + _headerLabel +
|
|
|
|
" based on column " + column.getHeaderLabel() +
|
|
|
|
" 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);
|
|
|
|
|
|
|
|
Change change = new ColumnAdditionChange(_headerLabel, _columnInsertIndex, cellsAtRows);
|
|
|
|
|
|
|
|
return new HistoryEntry(
|
|
|
|
project, description, this, change);
|
|
|
|
}
|
|
|
|
|
2010-02-05 00:38:40 +01:00
|
|
|
protected RowVisitor createRowVisitor(Project project, List<CellAtRow> cellsAtRows) throws Exception {
|
2010-02-17 01:26:38 +01:00
|
|
|
Column column = project.columnModel.getColumnByName(_baseColumnName);
|
|
|
|
|
2010-02-05 00:38:40 +01:00
|
|
|
Evaluable eval = new Parser(_expression).getExpression();
|
|
|
|
Properties bindings = ExpressionUtils.createBindings(project);
|
|
|
|
|
|
|
|
return new RowVisitor() {
|
|
|
|
int cellIndex;
|
|
|
|
Properties bindings;
|
|
|
|
List<CellAtRow> cellsAtRows;
|
|
|
|
Evaluable eval;
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
ExpressionUtils.bind(bindings, row, cell);
|
|
|
|
|
|
|
|
Object v = eval.evaluate(bindings);
|
|
|
|
if (v != null) {
|
|
|
|
Cell newCell = new Cell(v, null);
|
|
|
|
|
|
|
|
cellsAtRows.add(new CellAtRow(rowIndex, newCell));
|
|
|
|
}
|
|
|
|
|
2010-02-05 00:38:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 01:26:38 +01:00
|
|
|
}.init(column.getCellIndex(), bindings, cellsAtRows, eval);
|
2010-02-05 00:38:40 +01:00
|
|
|
}
|
|
|
|
}
|