Search in sources :

Example 1 with ColumnGroup

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

the class ColumnReorderChange method apply.

@Override
public void apply(Project project) {
    synchronized (project) {
        if (_newColumns == null) {
            _newColumns = new ArrayList<Column>();
            _oldColumns = new ArrayList<Column>(project.columnModel.columns);
            for (String n : _columnNames) {
                Column column = project.columnModel.getColumnByName(n);
                if (column != null) {
                    _newColumns.add(column);
                }
            }
            _oldColumnGroups = new ArrayList<ColumnGroup>(project.columnModel.columnGroups);
        }
        project.columnModel.columns.clear();
        project.columnModel.columns.addAll(_newColumns);
        project.columnModel.columnGroups.clear();
        project.update();
    }
}
Also used : Column(com.google.refine.model.Column) ColumnGroup(com.google.refine.model.ColumnGroup)

Example 2 with ColumnGroup

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

the class ColumnSplitChange method load.

public static Change load(LineNumberReader reader, Pool pool) throws Exception {
    String columnName = null;
    List<String> columnNames = null;
    List<Integer> rowIndices = null;
    List<List<Serializable>> tuples = null;
    boolean removeOriginalColumn = false;
    Column column = null;
    int columnIndex = -1;
    int firstNewCellIndex = -1;
    List<Row> oldRows = null;
    List<Row> newRows = 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);
        String value = line.substring(equal + 1);
        if ("columnName".equals(field)) {
            columnName = value;
        } else if ("columnNameCount".equals(field)) {
            int count = Integer.parseInt(value);
            columnNames = new ArrayList<String>(count);
            for (int i = 0; i < count; i++) {
                line = reader.readLine();
                if (line != null) {
                    columnNames.add(line);
                }
            }
        } else if ("rowIndexCount".equals(field)) {
            int count = Integer.parseInt(value);
            rowIndices = new ArrayList<Integer>(count);
            for (int i = 0; i < count; i++) {
                line = reader.readLine();
                if (line != null) {
                    rowIndices.add(Integer.parseInt(line));
                }
            }
        } else if ("tupleCount".equals(field)) {
            int count = Integer.parseInt(value);
            tuples = new ArrayList<List<Serializable>>(count);
            for (int i = 0; i < count; i++) {
                line = reader.readLine();
                if (line == null) {
                    continue;
                }
                int valueCount = Integer.parseInt(line);
                List<Serializable> tuple = new ArrayList<Serializable>(valueCount);
                for (int r = 0; r < valueCount; r++) {
                    line = reader.readLine();
                    JSONTokener t = new JSONTokener(line);
                    Object o = t.nextValue();
                    tuple.add((o != JSONObject.NULL) ? (Serializable) o : null);
                }
                tuples.add(tuple);
            }
        } else if ("removeOriginalColumn".equals(field)) {
            removeOriginalColumn = Boolean.parseBoolean(value);
        } else if ("column".equals(field)) {
            column = Column.load(value);
        } else if ("columnIndex".equals(field)) {
            columnIndex = Integer.parseInt(value);
        } else if ("firstNewCellIndex".equals(field)) {
            firstNewCellIndex = Integer.parseInt(value);
        } else if ("oldRowCount".equals(field)) {
            int count = Integer.parseInt(value);
            oldRows = new ArrayList<Row>(count);
            for (int i = 0; i < count; i++) {
                line = reader.readLine();
                if (line != null) {
                    oldRows.add(Row.load(line, pool));
                }
            }
        } else if ("newRowCount".equals(field)) {
            int count = Integer.parseInt(value);
            newRows = new ArrayList<Row>(count);
            for (int i = 0; i < count; i++) {
                line = reader.readLine();
                if (line != null) {
                    newRows.add(Row.load(line, pool));
                }
            }
        } else if ("oldColumnGroupCount".equals(field)) {
            int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
            oldColumnGroups = ColumnChange.readOldColumnGroups(reader, oldColumnGroupCount);
        }
    }
    ColumnSplitChange change = new ColumnSplitChange(columnName, columnNames, rowIndices, tuples, removeOriginalColumn, column, columnIndex, firstNewCellIndex, oldRows, newRows);
    change._oldColumnGroups = oldColumnGroups != null ? oldColumnGroups : new LinkedList<ColumnGroup>();
    return change;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) JSONTokener(org.json.JSONTokener) Column(com.google.refine.model.Column) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) ColumnGroup(com.google.refine.model.ColumnGroup)

Example 3 with ColumnGroup

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

the class ColumnAdditionChange method apply.

@Override
public void apply(Project project) {
    synchronized (project) {
        if (_newCellIndex < 0) {
            _newCellIndex = project.columnModel.allocateNewCellIndex();
        }
        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 <= _columnIndex) {
                if (columnGroup.startColumnIndex + columnGroup.columnSpan > _columnIndex) {
                    // the new column is inserted right in the middle of the group
                    project.columnModel.columnGroups.set(i, new ColumnGroup(columnGroup.startColumnIndex, columnGroup.columnSpan + 1, columnGroup.keyColumnIndex < _columnIndex ? columnGroup.keyColumnIndex : (columnGroup.keyColumnIndex + 1)));
                }
            } else {
                // the new column precedes this whole group
                project.columnModel.columnGroups.set(i, new ColumnGroup(columnGroup.startColumnIndex + 1, columnGroup.columnSpan, columnGroup.keyColumnIndex + 1));
            }
        }
        Column column = new Column(_newCellIndex, _columnName);
        project.columnModel.columns.add(_columnIndex, column);
        try {
            for (CellAtRow cell : _newCells) {
                project.rows.get(cell.row).setCell(_newCellIndex, cell.cell);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        project.update();
    }
}
Also used : Column(com.google.refine.model.Column) ColumnGroup(com.google.refine.model.ColumnGroup) IOException(java.io.IOException)

Example 4 with ColumnGroup

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

the class ColumnChange method writeOldColumnGroups.

public static void writeOldColumnGroups(Writer writer, Properties options, List<ColumnGroup> oldColumnGroups) throws IOException {
    writer.write("oldColumnGroupCount=");
    writer.write(Integer.toString(oldColumnGroups.size()));
    writer.write('\n');
    for (ColumnGroup cg : oldColumnGroups) {
        JSONWriter jsonWriter = new JSONWriter(writer);
        try {
            cg.write(jsonWriter, options);
        } catch (JSONException e) {
            throw new IOException(e);
        }
        writer.write('\n');
    }
}
Also used : JSONWriter(org.json.JSONWriter) JSONException(org.json.JSONException) IOException(java.io.IOException) ColumnGroup(com.google.refine.model.ColumnGroup)

Example 5 with ColumnGroup

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

the class ColumnMoveChange method apply.

@Override
public void apply(Project project) {
    synchronized (project) {
        _oldColumnIndex = project.columnModel.getColumnIndexByName(_columnName);
        if (_oldColumnIndex < 0 || _newColumnIndex < 0 || _newColumnIndex > project.columnModel.getMaxCellIndex()) {
            throw new RuntimeException("Column index out of range");
        }
        if (_oldColumnGroups == null) {
            _oldColumnGroups = new ArrayList<ColumnGroup>(project.columnModel.columnGroups);
        }
        Column column = project.columnModel.columns.remove(_oldColumnIndex);
        project.columnModel.columns.add(_newColumnIndex, column);
        project.columnModel.columnGroups.clear();
        project.update();
    }
}
Also used : Column(com.google.refine.model.Column) ColumnGroup(com.google.refine.model.ColumnGroup)

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