Search in sources :

Example 36 with Cell

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

the class CellTuple method getField.

@Override
public Object getField(String name, Properties bindings) {
    Column column = project.columnModel.getColumnByName(name);
    if (column != null) {
        int cellIndex = column.getCellIndex();
        Cell cell = row.getCell(cellIndex);
        if (cell != null) {
            return new WrappedCell(project, name, cell);
        }
    }
    return null;
}
Also used : Column(com.google.refine.model.Column) Cell(com.google.refine.model.Cell)

Example 37 with Cell

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

the class TreeImportUtilities method addCell.

protected static void addCell(Project project, ImportColumnGroup columnGroup, ImportRecord record, String columnLocalName, Serializable value) {
    ImportColumn column = getColumn(project, columnGroup, columnLocalName);
    int cellIndex = column.cellIndex;
    int rowIndex = Math.max(columnGroup.nextRowIndex, column.nextRowIndex);
    List<Cell> row = record.rows.get(rowIndex);
    if (row == null) {
        row = new ArrayList<Cell>();
        record.rows.set(rowIndex, row);
    }
    while (cellIndex >= row.size()) {
        row.add(null);
    }
    row.set(cellIndex, new Cell(value, null));
    column.nextRowIndex = rowIndex + 1;
    // TODO: Only increment for first instance in record?
    column.nonBlankCount++;
}
Also used : Cell(com.google.refine.model.Cell)

Example 38 with Cell

use of com.google.refine.model.Cell 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 39 with Cell

use of com.google.refine.model.Cell 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 40 with Cell

use of com.google.refine.model.Cell 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)

Aggregations

Cell (com.google.refine.model.Cell)58 Row (com.google.refine.model.Row)36 Column (com.google.refine.model.Column)19 Test (org.testng.annotations.Test)16 RefineTest (com.google.refine.tests.RefineTest)15 BeforeTest (org.testng.annotations.BeforeTest)15 JSONObject (org.json.JSONObject)13 ArrayList (java.util.ArrayList)12 Project (com.google.refine.model.Project)11 IOException (java.io.IOException)11 Properties (java.util.Properties)11 JSONException (org.json.JSONException)9 RowVisitor (com.google.refine.browsing.RowVisitor)7 HistoryEntry (com.google.refine.history.HistoryEntry)7 Serializable (java.io.Serializable)7 Recon (com.google.refine.model.Recon)6 CellChange (com.google.refine.model.changes.CellChange)6 HashMap (java.util.HashMap)6 Evaluable (com.google.refine.expr.Evaluable)5 JSONArray (org.json.JSONArray)4