Search in sources :

Example 16 with HistoryEntry

use of com.google.refine.history.HistoryEntry 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)

Example 17 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class TransposeColumnsIntoRowsOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    if (_combinedColumnName != null) {
        if (project.columnModel.getColumnByName(_combinedColumnName) != null) {
            throw new Exception("Another column already named " + _combinedColumnName);
        }
    } else {
        if (project.columnModel.getColumnByName(_keyColumnName) != null) {
            throw new Exception("Another column already named " + _keyColumnName);
        }
        if (project.columnModel.getColumnByName(_valueColumnName) != null) {
            throw new Exception("Another column already named " + _valueColumnName);
        }
    }
    List<Column> newColumns = new ArrayList<Column>();
    List<Column> oldColumns = project.columnModel.columns;
    int startColumnIndex = oldColumns.size();
    int columnCount = _columnCount;
    if (_columnCount > 0) {
        int columnsLeftToTranspose = _columnCount;
        for (int c = 0; c < oldColumns.size(); c++) {
            Column column = oldColumns.get(c);
            if (columnsLeftToTranspose == 0) {
                // This column is beyond the columns to transpose
                Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
                newColumn.setName(column.getName());
                newColumns.add(newColumn);
            } else if (columnsLeftToTranspose < _columnCount) {
                // This column is a column to transpose, but not the first
                // nothing to do
                columnsLeftToTranspose--;
            } else if (_startColumnName.equals(column.getName())) {
                // This is the first column to transpose
                startColumnIndex = c;
                if (_combinedColumnName != null) {
                    newColumns.add(new Column(newColumns.size(), _combinedColumnName));
                } else {
                    newColumns.add(new Column(newColumns.size(), _keyColumnName));
                    newColumns.add(new Column(newColumns.size(), _valueColumnName));
                }
                columnsLeftToTranspose--;
            } else {
                // This column is before all columns to transpose
                Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
                newColumn.setName(column.getName());
                newColumns.add(newColumn);
            }
        }
    } else {
        for (int c = 0; c < oldColumns.size(); c++) {
            Column column = oldColumns.get(c);
            if (_startColumnName.equals(column.getName())) {
                // This is the first column to transpose
                startColumnIndex = c;
                if (_combinedColumnName != null) {
                    newColumns.add(new Column(newColumns.size(), _combinedColumnName));
                } else {
                    newColumns.add(new Column(newColumns.size(), _keyColumnName));
                    newColumns.add(new Column(newColumns.size(), _valueColumnName));
                }
                break;
            } else {
                // This column is before all columns to transpose
                Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
                newColumn.setName(column.getName());
                newColumns.add(newColumn);
            }
        }
        columnCount = oldColumns.size() - startColumnIndex;
    }
    List<Row> oldRows = project.rows;
    List<Row> newRows = new ArrayList<Row>(oldRows.size() * columnCount);
    for (int r = 0; r < oldRows.size(); r++) {
        Row oldRow = project.rows.get(r);
        Row firstNewRow = new Row(newColumns.size());
        int firstNewRowIndex = newRows.size();
        newRows.add(firstNewRow);
        int transposedCells = 0;
        for (int c = 0; c < oldColumns.size(); c++) {
            Column column = oldColumns.get(c);
            Cell cell = oldRow.getCell(column.getCellIndex());
            if (c < startColumnIndex) {
                firstNewRow.setCell(c, cell);
            } else if (c == startColumnIndex || c < startColumnIndex + columnCount) {
                if (_combinedColumnName != null) {
                    Cell newCell;
                    if (cell == null || cell.value == null) {
                        if (_prependColumnName && !_ignoreBlankCells) {
                            newCell = new Cell(column.getName() + _separator, null);
                        } else {
                            continue;
                        }
                    } else if (_prependColumnName) {
                        newCell = new Cell(column.getName() + _separator + cell.value, null);
                    } else {
                        newCell = cell;
                    }
                    Row rowToModify;
                    if (transposedCells == 0) {
                        rowToModify = firstNewRow;
                    } else {
                        rowToModify = new Row(newColumns.size());
                        newRows.add(rowToModify);
                    }
                    rowToModify.setCell(startColumnIndex, newCell);
                    transposedCells++;
                } else {
                    if (_ignoreBlankCells && (cell == null || cell.value == null)) {
                        continue;
                    }
                    Row rowToModify;
                    if (transposedCells == 0) {
                        rowToModify = firstNewRow;
                    } else {
                        rowToModify = new Row(newColumns.size());
                        newRows.add(rowToModify);
                    }
                    rowToModify.setCell(startColumnIndex, new Cell(column.getName(), null));
                    rowToModify.setCell(startColumnIndex + 1, cell);
                    transposedCells++;
                }
            } else {
                firstNewRow.setCell(c - columnCount + (_combinedColumnName != null ? 1 : 2), cell);
            }
        }
        if (_fillDown) {
            for (int r2 = firstNewRowIndex + 1; r2 < newRows.size(); r2++) {
                Row newRow = newRows.get(r2);
                for (int c = 0; c < newColumns.size(); c++) {
                    if (c < startColumnIndex || (_combinedColumnName != null ? c > startColumnIndex : c > startColumnIndex + 1)) {
                        Column column = newColumns.get(c);
                        int cellIndex = column.getCellIndex();
                        Cell cellToCopy = firstNewRow.getCell(cellIndex);
                        if (cellToCopy != null && newRow.getCell(cellIndex) == null) {
                            newRow.setCell(cellIndex, cellToCopy);
                        }
                    }
                }
            }
        }
    }
    return new HistoryEntry(historyEntryID, project, getBriefDescription(), 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) JSONException(org.json.JSONException)

Example 18 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class ColumnRemovalOperation 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);
    }
    String description = "Remove column " + column.getName();
    Change change = new ColumnRemovalChange(project.columnModel.columns.indexOf(column));
    return new HistoryEntry(historyEntryID, project, description, ColumnRemovalOperation.this, change);
}
Also used : ColumnRemovalChange(com.google.refine.model.changes.ColumnRemovalChange) Column(com.google.refine.model.Column) HistoryEntry(com.google.refine.history.HistoryEntry) Change(com.google.refine.history.Change) ColumnRemovalChange(com.google.refine.model.changes.ColumnRemovalChange) JSONException(org.json.JSONException)

Example 19 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class EngineDependentMassCellOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Engine engine = createEngine(project);
    Column column = project.columnModel.getColumnByName(_columnName);
    if (column == null) {
        throw new Exception("No column named " + _columnName);
    }
    List<CellChange> cellChanges = new ArrayList<CellChange>(project.rows.size());
    FilteredRows filteredRows = engine.getAllFilteredRows();
    try {
        filteredRows.accept(project, createRowVisitor(project, cellChanges, historyEntryID));
    } catch (Exception e) {
        e.printStackTrace();
    }
    String description = createDescription(column, cellChanges);
    return new HistoryEntry(historyEntryID, project, description, this, createChange(project, column, cellChanges));
}
Also used : Column(com.google.refine.model.Column) CellChange(com.google.refine.model.changes.CellChange) MassCellChange(com.google.refine.model.changes.MassCellChange) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) FilteredRows(com.google.refine.browsing.FilteredRows) Engine(com.google.refine.browsing.Engine)

Example 20 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class TransposeTests method keyValueComumnize.

@Test
public void keyValueComumnize() throws Exception {
    String input = "ID;Cat;Val\n" + "1;a;1\n" + "1;b;3\n" + "2;b;4\n" + "2;c;5\n" + "3;a;2\n" + "3;b;5\n" + "3;d;3\n";
    prepareOptions(";", -1, 0, 0, 1, false, false);
    List<Exception> exceptions = new ArrayList<Exception>();
    importer.parseOneFile(project, metadata, job, "filesource", new StringReader(input), -1, options, exceptions);
    project.update();
    ProjectManager.singleton.registerProject(project, metadata);
    AbstractOperation op = new KeyValueColumnizeOperation("Cat", "Val", null);
    Process process = op.createProcess(project, new Properties());
    HistoryEntry historyEntry = process.performImmediate();
    // Expected output from the GUI. 
    // ID;a;b;c;d
    // 1;1;3;;
    // 2;;4;5;
    // 3;2;5;;3
    Assert.assertEquals(project.columnModel.columns.size(), 5);
    Assert.assertEquals(project.columnModel.columns.get(0).getName(), "ID");
    Assert.assertEquals(project.columnModel.columns.get(1).getName(), "a");
    Assert.assertEquals(project.columnModel.columns.get(2).getName(), "b");
    Assert.assertEquals(project.columnModel.columns.get(3).getName(), "c");
    Assert.assertEquals(project.columnModel.columns.get(4).getName(), "d");
    Assert.assertEquals(project.rows.size(), 3);
    // The actual row data structure has to leave the columns model untouched for redo/undo purpose.
    // So we have 2 empty columns(column 1,2) on the row level.
    // 1;1;3;;
    Assert.assertEquals(project.rows.get(0).cells.get(0).value, "1");
    Assert.assertEquals(project.rows.get(0).cells.get(3).value, "1");
    Assert.assertEquals(project.rows.get(0).cells.get(4).value, "3");
    // 2;;4;5;
    Assert.assertEquals(project.rows.get(1).cells.get(0).value, "2");
    Assert.assertEquals(project.rows.get(1).cells.get(4).value, "4");
    Assert.assertEquals(project.rows.get(1).cells.get(5).value, "5");
    // 3;2;5;;3
    Assert.assertEquals(project.rows.get(2).cells.get(0).value, "3");
    Assert.assertEquals(project.rows.get(2).cells.get(3).value, "2");
    Assert.assertEquals(project.rows.get(2).cells.get(4).value, "5");
    Assert.assertEquals(project.rows.get(2).cells.get(6).value, "3");
}
Also used : AbstractOperation(com.google.refine.model.AbstractOperation) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) HistoryEntry(com.google.refine.history.HistoryEntry) Process(com.google.refine.process.Process) Properties(java.util.Properties) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) RefineTest(com.google.refine.tests.RefineTest)

Aggregations

HistoryEntry (com.google.refine.history.HistoryEntry)22 ArrayList (java.util.ArrayList)15 Column (com.google.refine.model.Column)10 Engine (com.google.refine.browsing.Engine)8 JSONException (org.json.JSONException)8 FilteredRows (com.google.refine.browsing.FilteredRows)7 Cell (com.google.refine.model.Cell)7 Row (com.google.refine.model.Row)7 Project (com.google.refine.model.Project)6 Properties (java.util.Properties)6 Change (com.google.refine.history.Change)5 JSONWriter (org.json.JSONWriter)5 JSONObject (org.json.JSONObject)4 RowVisitor (com.google.refine.browsing.RowVisitor)3 MassChange (com.google.refine.model.changes.MassChange)3 MassRowChange (com.google.refine.model.changes.MassRowChange)3 MassRowColumnChange (com.google.refine.model.changes.MassRowColumnChange)3 Pool (com.google.refine.util.Pool)3 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3