Search in sources :

Example 11 with Row

use of com.google.refine.model.Row in project OpenRefine by OpenRefine.

the class MassRowColumnChange method save.

@Override
public void save(Writer writer, Properties options) throws IOException {
    writer.write("newColumnCount=");
    writer.write(Integer.toString(_newColumns.size()));
    writer.write('\n');
    for (Column column : _newColumns) {
        column.save(writer);
        writer.write('\n');
    }
    writer.write("oldColumnCount=");
    writer.write(Integer.toString(_oldColumns.size()));
    writer.write('\n');
    for (Column column : _oldColumns) {
        column.save(writer);
        writer.write('\n');
    }
    writer.write("newRowCount=");
    writer.write(Integer.toString(_newRows.size()));
    writer.write('\n');
    for (Row row : _newRows) {
        row.save(writer, options);
        writer.write('\n');
    }
    writer.write("oldRowCount=");
    writer.write(Integer.toString(_oldRows.size()));
    writer.write('\n');
    for (Row row : _oldRows) {
        row.save(writer, options);
        writer.write('\n');
    }
    ColumnChange.writeOldColumnGroups(writer, options, _oldColumnGroups);
    // end of change marker
    writer.write("/ec/\n");
}
Also used : Column(com.google.refine.model.Column) Row(com.google.refine.model.Row)

Example 12 with Row

use of com.google.refine.model.Row in project OpenRefine by OpenRefine.

the class ColumnAdditionChange method revert.

@Override
public void revert(Project project) {
    synchronized (project) {
        for (CellAtRow cell : _newCells) {
            Row row = project.rows.get(cell.row);
            row.setCell(_newCellIndex, null);
        }
        project.columnModel.columns.remove(_columnIndex);
        project.columnModel.columnGroups.clear();
        project.columnModel.columnGroups.addAll(_oldColumnGroups);
        project.update();
    }
}
Also used : Row(com.google.refine.model.Row)

Example 13 with Row

use of com.google.refine.model.Row in project OpenRefine by OpenRefine.

the class MassEditOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    Evaluable eval = MetaParser.parse(_expression);
    Properties bindings = ExpressionUtils.createBindings(project);
    Map<String, Serializable> fromTo = new HashMap<String, Serializable>();
    Serializable fromBlankTo = null;
    Serializable fromErrorTo = null;
    for (Edit edit : _edits) {
        for (String s : edit.from) {
            fromTo.put(s, edit.to);
        }
        // the last edit wins
        if (edit.fromBlank) {
            fromBlankTo = edit.to;
        }
        if (edit.fromError) {
            fromErrorTo = edit.to;
        }
    }
    return new RowVisitor() {

        int cellIndex;

        Properties bindings;

        List<CellChange> cellChanges;

        Evaluable eval;

        Map<String, Serializable> fromTo;

        Serializable fromBlankTo;

        Serializable fromErrorTo;

        public RowVisitor init(int cellIndex, Properties bindings, List<CellChange> cellChanges, Evaluable eval, Map<String, Serializable> fromTo, Serializable fromBlankTo, Serializable fromErrorTo) {
            this.cellIndex = cellIndex;
            this.bindings = bindings;
            this.cellChanges = cellChanges;
            this.eval = eval;
            this.fromTo = fromTo;
            this.fromBlankTo = fromBlankTo;
            this.fromErrorTo = fromErrorTo;
            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, _columnName, cell);
            Object v = eval.evaluate(bindings);
            if (ExpressionUtils.isError(v)) {
                if (fromErrorTo != null) {
                    newCell = new Cell(fromErrorTo, (cell != null) ? cell.recon : null);
                }
            } else if (ExpressionUtils.isNonBlankData(v)) {
                String from = v.toString();
                Serializable to = fromTo.get(from);
                if (to != null) {
                    newCell = new Cell(to, (cell != null) ? cell.recon : null);
                }
            } else {
                if (fromBlankTo != null) {
                    newCell = new Cell(fromBlankTo, (cell != null) ? cell.recon : null);
                }
            }
            if (newCell != null) {
                CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                cellChanges.add(cellChange);
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellChanges, eval, fromTo, fromBlankTo, fromErrorTo);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) CellChange(com.google.refine.model.changes.CellChange) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell)

Example 14 with Row

use of com.google.refine.model.Row in project OpenRefine by OpenRefine.

the class TextTransformOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    Evaluable eval = MetaParser.parse(_expression);
    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;
        }

        @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;
            Object oldValue = cell != null ? cell.value : null;
            ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
            Object o = eval.evaluate(bindings);
            if (o == null) {
                if (oldValue != null) {
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, null);
                    cellChanges.add(cellChange);
                }
            } else {
                if (o instanceof Cell) {
                    newCell = (Cell) o;
                } else if (o instanceof WrappedCell) {
                    newCell = ((WrappedCell) o).cell;
                } else {
                    Serializable newValue = ExpressionUtils.wrapStorable(o);
                    if (ExpressionUtils.isError(newValue)) {
                        if (_onError == OnError.KeepOriginal) {
                            return false;
                        } else if (_onError == OnError.SetToBlank) {
                            newValue = null;
                        }
                    }
                    if (!ExpressionUtils.sameValue(oldValue, newValue)) {
                        newCell = new Cell(newValue, (cell != null) ? cell.recon : null);
                        if (_repeat) {
                            for (int i = 0; i < _repeatCount; i++) {
                                ExpressionUtils.bind(bindings, row, rowIndex, _columnName, newCell);
                                newValue = ExpressionUtils.wrapStorable(eval.evaluate(bindings));
                                if (ExpressionUtils.isError(newValue)) {
                                    break;
                                } else if (ExpressionUtils.sameValue(newCell.value, newValue)) {
                                    break;
                                }
                                newCell = new Cell(newValue, newCell.recon);
                            }
                        }
                    }
                }
                if (newCell != null) {
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                    cellChanges.add(cellChange);
                }
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellChanges, eval);
}
Also used : Serializable(java.io.Serializable) CellChange(com.google.refine.model.changes.CellChange) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) WrappedCell(com.google.refine.expr.WrappedCell) Column(com.google.refine.model.Column) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Example 15 with Row

use of com.google.refine.model.Row in project OpenRefine by OpenRefine.

the class TransposeRowsIntoColumnsOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    List<Column> newColumns = new ArrayList<Column>();
    List<Column> oldColumns = project.columnModel.columns;
    int columnIndex = project.columnModel.getColumnIndexByName(_columnName);
    int columnCount = oldColumns.size();
    for (int i = 0; i < columnCount; i++) {
        Column column = oldColumns.get(i);
        if (i == columnIndex) {
            int newIndex = 1;
            for (int n = 0; n < _rowCount; n++) {
                String columnName = _columnName + " " + newIndex++;
                while (project.columnModel.getColumnByName(columnName) != null) {
                    columnName = _columnName + " " + newIndex++;
                }
                newColumns.add(new Column(i + n, columnName));
            }
        } else if (i < columnIndex) {
            newColumns.add(new Column(i, column.getName()));
        } else {
            newColumns.add(new Column(i + _rowCount - 1, column.getName()));
        }
    }
    List<Row> oldRows = project.rows;
    List<Row> newRows = new ArrayList<Row>(oldRows.size() / _rowCount);
    for (int r = 0; r < oldRows.size(); r += _rowCount) {
        Row firstNewRow = new Row(newColumns.size());
        for (int r2 = 0; r2 < _rowCount && r + r2 < oldRows.size(); r2++) {
            Row oldRow = oldRows.get(r + r2);
            Row newRow = r2 == 0 ? firstNewRow : new Row(newColumns.size());
            boolean hasData = r2 == 0;
            for (int c = 0; c < oldColumns.size(); c++) {
                Column column = oldColumns.get(c);
                Cell cell = oldRow.getCell(column.getCellIndex());
                if (cell != null && cell.value != null) {
                    if (c == columnIndex) {
                        firstNewRow.setCell(columnIndex + r2, cell);
                    } else if (c < columnIndex) {
                        newRow.setCell(c, cell);
                        hasData = true;
                    } else {
                        newRow.setCell(c + _rowCount - 1, cell);
                        hasData = true;
                    }
                }
            }
            if (hasData) {
                newRows.add(newRow);
            }
        }
    }
    return new HistoryEntry(historyEntryID, project, getBriefDescription(null), this, new MassRowColumnChange(newColumns, newRows));
}
Also used : Column(com.google.refine.model.Column) MassRowColumnChange(com.google.refine.model.changes.MassRowColumnChange) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell)

Aggregations

Row (com.google.refine.model.Row)88 Cell (com.google.refine.model.Cell)36 Test (org.testng.annotations.Test)29 BeforeTest (org.testng.annotations.BeforeTest)28 Column (com.google.refine.model.Column)25 RefineTest (com.google.refine.tests.RefineTest)17 Project (com.google.refine.model.Project)15 ArrayList (java.util.ArrayList)15 JSONObject (org.json.JSONObject)14 Properties (java.util.Properties)12 RowVisitor (com.google.refine.browsing.RowVisitor)8 Serializable (java.io.Serializable)8 JSONException (org.json.JSONException)8 HistoryEntry (com.google.refine.history.HistoryEntry)7 File (java.io.File)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Evaluable (com.google.refine.expr.Evaluable)5 CellChange (com.google.refine.model.changes.CellChange)5 ColumnModel (com.google.refine.model.ColumnModel)4