Search in sources :

Example 6 with Column

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

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

the class MassCellChange method apply.

@Override
public void apply(Project project) {
    synchronized (project) {
        List<Row> rows = project.rows;
        for (CellChange cellChange : _cellChanges) {
            rows.get(cellChange.row).setCell(cellChange.cellIndex, cellChange.newCell);
        }
        if (_commonColumnName != null) {
            Column column = project.columnModel.getColumnByName(_commonColumnName);
            column.clearPrecomputes();
            ProjectManager.singleton.getInterProjectModel().flushJoinsInvolvingProjectColumn(project.id, _commonColumnName);
        }
        if (_updateRowContextDependencies) {
            project.update();
        }
    }
}
Also used : Column(com.google.refine.model.Column) Row(com.google.refine.model.Row)

Example 8 with Column

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

the class MassRowColumnChange method save.

@Override
public void save(Writer writer, Properties options) throws IOException {
    writer.write("newColumnCount=");
    writer.write(Integer.toString(_newColumns.size()));
    writer.write('\n');
    for (Column column : _newColumns) {
        column.save(writer);
        writer.write('\n');
    }
    writer.write("oldColumnCount=");
    writer.write(Integer.toString(_oldColumns.size()));
    writer.write('\n');
    for (Column column : _oldColumns) {
        column.save(writer);
        writer.write('\n');
    }
    writer.write("newRowCount=");
    writer.write(Integer.toString(_newRows.size()));
    writer.write('\n');
    for (Row row : _newRows) {
        row.save(writer, options);
        writer.write('\n');
    }
    writer.write("oldRowCount=");
    writer.write(Integer.toString(_oldRows.size()));
    writer.write('\n');
    for (Row row : _oldRows) {
        row.save(writer, options);
        writer.write('\n');
    }
    ColumnChange.writeOldColumnGroups(writer, options, _oldColumnGroups);
    // end of change marker
    writer.write("/ec/\n");
}
Also used : Column(com.google.refine.model.Column) Row(com.google.refine.model.Row)

Example 9 with Column

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

the class ImporterUtilities method setupColumns.

public static void setupColumns(Project project, List<String> columnNames) {
    Map<String, Integer> nameToIndex = new HashMap<String, Integer>();
    for (int c = 0; c < columnNames.size(); c++) {
        String cell = columnNames.get(c).trim();
        if (cell.isEmpty()) {
            cell = "Column";
        } else if (cell.startsWith("\"") && cell.endsWith("\"")) {
            // FIXME: is trimming quotation marks appropriate?
            cell = cell.substring(1, cell.length() - 1).trim();
        }
        if (nameToIndex.containsKey(cell)) {
            int index = nameToIndex.get(cell);
            nameToIndex.put(cell, index + 1);
            cell = cell.contains(" ") ? (cell + " " + index) : (cell + index);
        } else {
            nameToIndex.put(cell, 2);
        }
        columnNames.set(c, cell);
        if (project.columnModel.getColumnByName(cell) == null) {
            Column column = new Column(project.columnModel.allocateNewCellIndex(), cell);
            try {
                project.columnModel.addColumn(project.columnModel.columns.size(), column, false);
            } catch (ModelException e) {
            // Ignore: shouldn't get in here since we just checked for duplicate names.
            }
        }
    }
}
Also used : ModelException(com.google.refine.model.ModelException) HashMap(java.util.HashMap) Column(com.google.refine.model.Column)

Example 10 with Column

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

the class CellChange method apply.

@Override
public void apply(Project project) {
    project.rows.get(row).setCell(cellIndex, newCell);
    Column column = project.columnModel.getColumnByCellIndex(cellIndex);
    column.clearPrecomputes();
    ProjectManager.singleton.getInterProjectModel().flushJoinsInvolvingProjectColumn(project.id, column.getName());
}
Also used : Column(com.google.refine.model.Column)

Aggregations

Column (com.google.refine.model.Column)62 Row (com.google.refine.model.Row)25 Cell (com.google.refine.model.Cell)19 Project (com.google.refine.model.Project)16 ArrayList (java.util.ArrayList)14 JSONObject (org.json.JSONObject)14 JSONException (org.json.JSONException)11 HistoryEntry (com.google.refine.history.HistoryEntry)10 RowVisitor (com.google.refine.browsing.RowVisitor)9 Engine (com.google.refine.browsing.Engine)7 ParsingException (com.google.refine.expr.ParsingException)7 CellChange (com.google.refine.model.changes.CellChange)7 Serializable (java.io.Serializable)7 Properties (java.util.Properties)7 FilteredRows (com.google.refine.browsing.FilteredRows)6 NumericBinIndex (com.google.refine.browsing.util.NumericBinIndex)6 ColumnGroup (com.google.refine.model.ColumnGroup)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Evaluable (com.google.refine.expr.Evaluable)5