Search in sources :

Example 1 with MassRowChange

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

the class DenormalizeOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    List<Row> newRows = new ArrayList<Row>();
    List<Row> oldRows = project.rows;
    for (int r = 0; r < oldRows.size(); r++) {
        Row oldRow = oldRows.get(r);
        Row newRow = null;
        RowDependency rd = project.recordModel.getRowDependency(r);
        if (rd.cellDependencies != null) {
            newRow = oldRow.dup();
            for (CellDependency cd : rd.cellDependencies) {
                if (cd != null) {
                    int contextRowIndex = cd.rowIndex;
                    int contextCellIndex = cd.cellIndex;
                    if (contextRowIndex >= 0 && contextRowIndex < oldRows.size()) {
                        Row contextRow = oldRows.get(contextRowIndex);
                        Cell contextCell = contextRow.getCell(contextCellIndex);
                        newRow.setCell(contextCellIndex, contextCell);
                    }
                }
            }
        }
        newRows.add(newRow != null ? newRow : oldRow);
    }
    return new HistoryEntry(historyEntryID, project, getBriefDescription(project), DenormalizeOperation.this, new MassRowChange(newRows));
}
Also used : MassRowChange(com.google.refine.model.changes.MassRowChange) CellDependency(com.google.refine.model.RecordModel.CellDependency) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) Row(com.google.refine.model.Row) RowDependency(com.google.refine.model.RecordModel.RowDependency) Cell(com.google.refine.model.Cell)

Example 2 with MassRowChange

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

the class MultiValuedCellJoinOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    if (column == null) {
        throw new Exception("No column named " + _columnName);
    }
    int cellIndex = column.getCellIndex();
    Column keyColumn = project.columnModel.getColumnByName(_keyColumnName);
    if (keyColumn == null) {
        throw new Exception("No key column named " + _keyColumnName);
    }
    int keyCellIndex = keyColumn.getCellIndex();
    List<Row> newRows = new ArrayList<Row>();
    int oldRowCount = project.rows.size();
    for (int r = 0; r < oldRowCount; r++) {
        Row oldRow = project.rows.get(r);
        if (oldRow.isCellBlank(keyCellIndex)) {
            newRows.add(oldRow.dup());
            continue;
        }
        int r2 = r + 1;
        while (r2 < oldRowCount && project.rows.get(r2).isCellBlank(keyCellIndex)) {
            r2++;
        }
        if (r2 == r + 1) {
            newRows.add(oldRow.dup());
            continue;
        }
        StringBuffer sb = new StringBuffer();
        for (int r3 = r; r3 < r2; r3++) {
            Object value = project.rows.get(r3).getCellValue(cellIndex);
            if (ExpressionUtils.isNonBlankData(value)) {
                if (sb.length() > 0) {
                    sb.append(_separator);
                }
                sb.append(value.toString());
            }
        }
        for (int r3 = r; r3 < r2; r3++) {
            Row newRow = project.rows.get(r3).dup();
            if (r3 == r) {
                newRow.setCell(cellIndex, new Cell(sb.toString(), null));
            } else {
                newRow.setCell(cellIndex, null);
            }
            if (!newRow.isEmpty()) {
                newRows.add(newRow);
            }
        }
        // r will be incremented by the for loop anyway
        r = r2 - 1;
    }
    return new HistoryEntry(historyEntryID, project, getBriefDescription(null), this, new MassRowChange(newRows));
}
Also used : MassRowChange(com.google.refine.model.changes.MassRowChange) Column(com.google.refine.model.Column) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell) JSONException(org.json.JSONException)

Example 3 with MassRowChange

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

the class MultiValuedCellSplitOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    if (column == null) {
        throw new Exception("No column named " + _columnName);
    }
    int cellIndex = column.getCellIndex();
    Column keyColumn = project.columnModel.getColumnByName(_keyColumnName);
    if (keyColumn == null) {
        throw new Exception("No key column named " + _keyColumnName);
    }
    int keyCellIndex = keyColumn.getCellIndex();
    List<Row> newRows = new ArrayList<Row>();
    int oldRowCount = project.rows.size();
    for (int r = 0; r < oldRowCount; r++) {
        Row oldRow = project.rows.get(r);
        if (oldRow.isCellBlank(cellIndex)) {
            newRows.add(oldRow.dup());
            continue;
        }
        Object value = oldRow.getCellValue(cellIndex);
        String s = value instanceof String ? ((String) value) : value.toString();
        String[] values = null;
        if (_mode.equals("regex")) {
            values = s.split(_separator);
        } else {
            values = StringUtils.splitByWholeSeparatorPreserveAllTokens(s, _separator);
        }
        if (values.length < 2) {
            newRows.add(oldRow.dup());
            continue;
        }
        // First value goes into the same row
        {
            Row firstNewRow = oldRow.dup();
            firstNewRow.setCell(cellIndex, new Cell(values[0].trim(), null));
            newRows.add(firstNewRow);
        }
        int r2 = r + 1;
        for (int v = 1; v < values.length; v++) {
            Cell newCell = new Cell(values[v].trim(), null);
            if (r2 < project.rows.size()) {
                Row oldRow2 = project.rows.get(r2);
                if (oldRow2.isCellBlank(cellIndex) && oldRow2.isCellBlank(keyCellIndex)) {
                    Row newRow = oldRow2.dup();
                    newRow.setCell(cellIndex, newCell);
                    newRows.add(newRow);
                    r2++;
                    continue;
                }
            }
            Row newRow = new Row(cellIndex + 1);
            newRow.setCell(cellIndex, newCell);
            newRows.add(newRow);
        }
        // r will be incremented by the for loop anyway
        r = r2 - 1;
    }
    return new HistoryEntry(historyEntryID, project, getBriefDescription(null), this, new MassRowChange(newRows));
}
Also used : MassRowChange(com.google.refine.model.changes.MassRowChange) Column(com.google.refine.model.Column) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell) JSONException(org.json.JSONException)

Aggregations

HistoryEntry (com.google.refine.history.HistoryEntry)3 Cell (com.google.refine.model.Cell)3 Row (com.google.refine.model.Row)3 MassRowChange (com.google.refine.model.changes.MassRowChange)3 ArrayList (java.util.ArrayList)3 Column (com.google.refine.model.Column)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 CellDependency (com.google.refine.model.RecordModel.CellDependency)1 RowDependency (com.google.refine.model.RecordModel.RowDependency)1