Search in sources :

Example 1 with CellAtRow

use of com.google.refine.model.changes.CellAtRow in project OpenRefine by OpenRefine.

the class ColumnAdditionOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Engine engine = createEngine(project);
    Column column = project.columnModel.getColumnByName(_baseColumnName);
    if (column == null) {
        throw new Exception("No column named " + _baseColumnName);
    }
    if (project.columnModel.getColumnByName(_newColumnName) != null) {
        throw new Exception("Another column already named " + _newColumnName);
    }
    List<CellAtRow> cellsAtRows = new ArrayList<CellAtRow>(project.rows.size());
    FilteredRows filteredRows = engine.getAllFilteredRows();
    filteredRows.accept(project, createRowVisitor(project, cellsAtRows));
    String description = createDescription(column, cellsAtRows);
    Change change = new ColumnAdditionChange(_newColumnName, _columnInsertIndex, cellsAtRows);
    return new HistoryEntry(historyEntryID, project, description, this, change);
}
Also used : CellAtRow(com.google.refine.model.changes.CellAtRow) ColumnAdditionChange(com.google.refine.model.changes.ColumnAdditionChange) Column(com.google.refine.model.Column) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) Change(com.google.refine.history.Change) ColumnAdditionChange(com.google.refine.model.changes.ColumnAdditionChange) FilteredRows(com.google.refine.browsing.FilteredRows) Engine(com.google.refine.browsing.Engine) JSONException(org.json.JSONException)

Example 2 with CellAtRow

use of com.google.refine.model.changes.CellAtRow in project OpenRefine by OpenRefine.

the class ColumnAdditionOperation method createRowVisitor.

protected RowVisitor createRowVisitor(Project project, List<CellAtRow> cellsAtRows) throws Exception {
    Column column = project.columnModel.getColumnByName(_baseColumnName);
    Evaluable eval = MetaParser.parse(_expression);
    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;
        }

        @Override
        public void start(Project project) {
        // nothing to do
        }

        @Override
        public void end(Project project) {
        // nothing to do
        }

        @Override
        public boolean visit(Project project, int rowIndex, Row row) {
            Cell cell = row.getCell(cellIndex);
            Cell newCell = null;
            ExpressionUtils.bind(bindings, row, rowIndex, _baseColumnName, cell);
            Object o = eval.evaluate(bindings);
            if (o != null) {
                if (o instanceof Cell) {
                    newCell = (Cell) o;
                } else if (o instanceof WrappedCell) {
                    newCell = ((WrappedCell) o).cell;
                } else {
                    Serializable v = ExpressionUtils.wrapStorable(o);
                    if (ExpressionUtils.isError(v)) {
                        if (_onError == OnError.SetToBlank) {
                            return false;
                        } else if (_onError == OnError.KeepOriginal) {
                            v = cell != null ? cell.value : null;
                        }
                    }
                    if (v != null) {
                        newCell = new Cell(v, null);
                    }
                }
            }
            if (newCell != null) {
                cellsAtRows.add(new CellAtRow(rowIndex, newCell));
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellsAtRows, eval);
}
Also used : Serializable(java.io.Serializable) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) CellAtRow(com.google.refine.model.changes.CellAtRow) WrappedCell(com.google.refine.expr.WrappedCell) Column(com.google.refine.model.Column) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) CellAtRow(com.google.refine.model.changes.CellAtRow) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Aggregations

Column (com.google.refine.model.Column)2 CellAtRow (com.google.refine.model.changes.CellAtRow)2 Engine (com.google.refine.browsing.Engine)1 FilteredRows (com.google.refine.browsing.FilteredRows)1 RowVisitor (com.google.refine.browsing.RowVisitor)1 Evaluable (com.google.refine.expr.Evaluable)1 WrappedCell (com.google.refine.expr.WrappedCell)1 Change (com.google.refine.history.Change)1 HistoryEntry (com.google.refine.history.HistoryEntry)1 Cell (com.google.refine.model.Cell)1 Project (com.google.refine.model.Project)1 Row (com.google.refine.model.Row)1 ColumnAdditionChange (com.google.refine.model.changes.ColumnAdditionChange)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1