Search in sources :

Example 6 with ColumnGroup

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

the class ColumnRemovalChange method apply.

@Override
public void apply(Project project) {
    synchronized (project) {
        int columnGroupCount = project.columnModel.columnGroups.size();
        _oldColumnGroups = new ArrayList<ColumnGroup>(columnGroupCount);
        for (int i = columnGroupCount - 1; i >= 0; i--) {
            ColumnGroup columnGroup = project.columnModel.columnGroups.get(i);
            _oldColumnGroups.add(columnGroup);
            if (columnGroup.startColumnIndex <= _oldColumnIndex) {
                if (columnGroup.startColumnIndex + columnGroup.columnSpan > _oldColumnIndex) {
                    if (columnGroup.keyColumnIndex == _oldColumnIndex) {
                        // the key column is removed, so we remove the whole group
                        project.columnModel.columnGroups.remove(i);
                    } else {
                        // otherwise, the group's span has been reduced by 1
                        project.columnModel.columnGroups.set(i, new ColumnGroup(columnGroup.startColumnIndex, columnGroup.columnSpan - 1, columnGroup.keyColumnIndex < _oldColumnIndex ? columnGroup.keyColumnIndex : (columnGroup.keyColumnIndex - 1)));
                    }
                }
            } else {
                // the column removed precedes this whole group
                project.columnModel.columnGroups.set(i, new ColumnGroup(columnGroup.startColumnIndex - 1, columnGroup.columnSpan, columnGroup.keyColumnIndex - 1));
            }
        }
        _oldColumn = project.columnModel.columns.remove(_oldColumnIndex);
        _oldCells = new CellAtRow[project.rows.size()];
        int cellIndex = _oldColumn.getCellIndex();
        for (int i = 0; i < _oldCells.length; i++) {
            Row row = project.rows.get(i);
            Cell oldCell = null;
            if (cellIndex < row.cells.size()) {
                oldCell = row.cells.get(cellIndex);
            }
            _oldCells[i] = new CellAtRow(i, oldCell);
            row.setCell(cellIndex, null);
        }
        project.update();
    }
}
Also used : Row(com.google.refine.model.Row) ColumnGroup(com.google.refine.model.ColumnGroup) Cell(com.google.refine.model.Cell)

Example 7 with ColumnGroup

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

the class ColumnRemovalChange method load.

public static Change load(LineNumberReader reader, Pool pool) throws Exception {
    int oldColumnIndex = -1;
    Column oldColumn = null;
    CellAtRow[] oldCells = null;
    List<ColumnGroup> oldColumnGroups = null;
    String line;
    while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
        int equal = line.indexOf('=');
        CharSequence field = line.subSequence(0, equal);
        if ("oldColumnIndex".equals(field)) {
            oldColumnIndex = Integer.parseInt(line.substring(equal + 1));
        } else if ("oldColumn".equals(field)) {
            oldColumn = Column.load(line.substring(equal + 1));
        } else if ("oldCellCount".equals(field)) {
            int oldCellCount = Integer.parseInt(line.substring(equal + 1));
            oldCells = new CellAtRow[oldCellCount];
            for (int i = 0; i < oldCellCount; i++) {
                line = reader.readLine();
                if (line != null) {
                    oldCells[i] = CellAtRow.load(line, pool);
                }
            }
        } else if ("oldColumnGroupCount".equals(field)) {
            int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
            oldColumnGroups = readOldColumnGroups(reader, oldColumnGroupCount);
        }
    }
    ColumnRemovalChange change = new ColumnRemovalChange(oldColumnIndex);
    change._oldColumn = oldColumn;
    change._oldCells = oldCells;
    change._oldColumnGroups = oldColumnGroups != null ? oldColumnGroups : new LinkedList<ColumnGroup>();
    return change;
}
Also used : Column(com.google.refine.model.Column) ColumnGroup(com.google.refine.model.ColumnGroup) LinkedList(java.util.LinkedList)

Example 8 with ColumnGroup

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

the class ColumnSplitChange method apply.

@Override
public void apply(Project project) {
    synchronized (project) {
        if (_firstNewCellIndex < 0) {
            _firstNewCellIndex = project.columnModel.allocateNewCellIndex();
            for (int i = 1; i < _columnNames.size(); i++) {
                project.columnModel.allocateNewCellIndex();
            }
            ProjectManager.singleton.getInterProjectModel().flushJoinsInvolvingProjectColumn(project.id, _columnName);
            _column = project.columnModel.getColumnByName(_columnName);
            _columnIndex = project.columnModel.getColumnIndexByName(_columnName);
            _oldRows = new ArrayList<Row>(_rowIndices.size());
            _newRows = new ArrayList<Row>(_rowIndices.size());
            int cellIndex = _column.getCellIndex();
            for (int i = 0; i < _rowIndices.size(); i++) {
                int r = _rowIndices.get(i);
                List<Serializable> tuple = _tuples.get(i);
                Row oldRow = project.rows.get(r);
                Row newRow = oldRow.dup();
                _oldRows.add(oldRow);
                _newRows.add(newRow);
                for (int c = 0; c < tuple.size(); c++) {
                    Serializable value = tuple.get(c);
                    if (value != null) {
                        newRow.setCell(_firstNewCellIndex + c, new Cell(value, null));
                    }
                }
                if (_removeOriginalColumn) {
                    newRow.setCell(cellIndex, null);
                }
            }
        }
        int columnGroupCount = project.columnModel.columnGroups.size();
        int columnCountChange = _columnNames.size() - (_removeOriginalColumn ? 1 : 0);
        _oldColumnGroups = new ArrayList<ColumnGroup>(columnGroupCount);
        for (int i = columnGroupCount - 1; i >= 0; i--) {
            ColumnGroup columnGroup = project.columnModel.columnGroups.get(i);
            _oldColumnGroups.add(columnGroup);
            if (columnGroup.startColumnIndex <= _columnIndex) {
                if (columnGroup.startColumnIndex + columnGroup.columnSpan > _columnIndex) {
                    if (columnGroup.keyColumnIndex == _columnIndex) {
                        if (_removeOriginalColumn) {
                            // the key column is being split and removed
                            project.columnModel.columnGroups.remove(i);
                        } else {
                            project.columnModel.columnGroups.set(i, new ColumnGroup(columnGroup.startColumnIndex, columnGroup.columnSpan + columnCountChange, columnGroup.keyColumnIndex));
                        }
                    } else {
                        project.columnModel.columnGroups.set(i, new ColumnGroup(columnGroup.startColumnIndex, columnGroup.columnSpan + columnCountChange, columnGroup.keyColumnIndex < _columnIndex ? columnGroup.keyColumnIndex : (columnGroup.keyColumnIndex + columnCountChange)));
                    }
                }
            } else {
                // the new column precedes this whole group
                project.columnModel.columnGroups.set(i, new ColumnGroup(columnGroup.startColumnIndex + columnCountChange, columnGroup.columnSpan, columnGroup.keyColumnIndex + columnCountChange));
            }
        }
        for (int i = 0; i < _rowIndices.size(); i++) {
            int r = _rowIndices.get(i);
            Row newRow = _newRows.get(i);
            project.rows.set(r, newRow);
        }
        for (int i = 0; i < _columnNames.size(); i++) {
            String name = _columnNames.get(i);
            int cellIndex = _firstNewCellIndex + i;
            Column column = new Column(cellIndex, name);
            project.columnModel.columns.add(_columnIndex + 1 + i, column);
        }
        if (_removeOriginalColumn) {
            project.columnModel.columns.remove(_columnIndex);
        }
        project.update();
    }
}
Also used : Serializable(java.io.Serializable) Column(com.google.refine.model.Column) Row(com.google.refine.model.Row) ColumnGroup(com.google.refine.model.ColumnGroup) Cell(com.google.refine.model.Cell)

Aggregations

ColumnGroup (com.google.refine.model.ColumnGroup)8 Column (com.google.refine.model.Column)6 Row (com.google.refine.model.Row)3 Cell (com.google.refine.model.Cell)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 LinkedList (java.util.LinkedList)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1 JSONWriter (org.json.JSONWriter)1