Search in sources :

Example 41 with Cell

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

the class StandardReconConfig method createJob.

@Override
public ReconJob createJob(Project project, int rowIndex, Row row, String columnName, Cell cell) {
    StandardReconJob job = new StandardReconJob();
    try {
        StringWriter stringWriter = new StringWriter();
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        jsonWriter.object();
        jsonWriter.key("query");
        jsonWriter.value(cell.value.toString());
        if (typeID != null) {
            jsonWriter.key("type");
            jsonWriter.value(typeID);
            jsonWriter.key("type_strict");
            jsonWriter.value("should");
        }
        if (columnDetails.size() > 0) {
            jsonWriter.key("properties");
            jsonWriter.array();
            for (ColumnDetail c : columnDetails) {
                int detailCellIndex = project.columnModel.getColumnByName(c.columnName).getCellIndex();
                Cell cell2 = row.getCell(detailCellIndex);
                if (cell2 == null || !ExpressionUtils.isNonBlankData(cell2.value)) {
                    int cellIndex = project.columnModel.getColumnByName(columnName).getCellIndex();
                    RowDependency rd = project.recordModel.getRowDependency(rowIndex);
                    if (rd != null && rd.cellDependencies != null) {
                        int contextRowIndex = rd.cellDependencies[cellIndex].rowIndex;
                        if (contextRowIndex >= 0 && contextRowIndex < project.rows.size()) {
                            Row row2 = project.rows.get(contextRowIndex);
                            cell2 = row2.getCell(detailCellIndex);
                        }
                    }
                }
                if (cell2 != null && ExpressionUtils.isNonBlankData(cell2.value)) {
                    jsonWriter.object();
                    jsonWriter.key("pid");
                    jsonWriter.value(c.propertyID);
                    jsonWriter.key("v");
                    if (cell2.recon != null && cell2.recon.match != null) {
                        jsonWriter.object();
                        jsonWriter.key("id");
                        jsonWriter.value(cell2.recon.match.id);
                        jsonWriter.key("name");
                        jsonWriter.value(cell2.recon.match.name);
                        jsonWriter.endObject();
                    } else if (cell2.value instanceof Calendar) {
                        jsonWriter.value(ParsingUtilities.dateToString(((Calendar) cell2.value).getTime()));
                    } else if (cell2.value instanceof Date) {
                        jsonWriter.value(ParsingUtilities.dateToString((Date) cell2.value));
                    } else {
                        jsonWriter.value(cell2.value.toString());
                    }
                    jsonWriter.endObject();
                }
            }
            jsonWriter.endArray();
        }
        // services which might choke on this
        if (limit != 0) {
            jsonWriter.key("limit");
            jsonWriter.value(limit);
        }
        jsonWriter.endObject();
        job.text = cell.value.toString();
        job.code = stringWriter.toString();
    } catch (JSONException e) {
    //
    }
    return job;
}
Also used : JSONWriter(org.json.JSONWriter) StringWriter(java.io.StringWriter) Calendar(java.util.Calendar) JSONException(org.json.JSONException) RowDependency(com.google.refine.model.RecordModel.RowDependency) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell) Date(java.util.Date)

Example 42 with Cell

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

the class CellChange method load.

public static CellChange load(LineNumberReader reader, Pool pool) throws Exception {
    int row = -1;
    int cellIndex = -1;
    Cell oldCell = null;
    Cell newCell = 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 ("row".equals(field)) {
            row = Integer.parseInt(value);
        } else if ("cell".equals(field)) {
            cellIndex = Integer.parseInt(value);
        } else if ("new".equals(field) && value.length() > 0) {
            newCell = Cell.loadStreaming(value, pool);
        } else if ("old".equals(field) && value.length() > 0) {
            oldCell = Cell.loadStreaming(value, pool);
        }
    }
    return new CellChange(row, cellIndex, oldCell, newCell);
}
Also used : Cell(com.google.refine.model.Cell)

Example 43 with Cell

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

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

Example 45 with Cell

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

the class ReconJudgeSimilarCellsOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    return new RowVisitor() {

        int _cellIndex;

        List<CellChange> _cellChanges;

        Recon _sharedNewRecon = null;

        Map<Long, Recon> _dupReconMap = new HashMap<Long, Recon>();

        long _historyEntryID;

        public RowVisitor init(int cellIndex, List<CellChange> cellChanges, long historyEntryID) {
            _cellIndex = cellIndex;
            _cellChanges = cellChanges;
            _historyEntryID = historyEntryID;
            return this;
        }

        @Override
        public void start(Project project) {
        // nothing to do
        }

        @Override
        public void end(Project project) {
        // nothing to do
        }

        @Override
        public boolean visit(Project project, int rowIndex, Row row) {
            Cell cell = row.getCell(_cellIndex);
            if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) {
                String value = cell.value instanceof String ? ((String) cell.value) : cell.value.toString();
                if (_similarValue.equals(value)) {
                    Recon recon = null;
                    if (_judgment == Judgment.New && _shareNewTopics) {
                        if (_sharedNewRecon == null) {
                            _sharedNewRecon = new Recon(_historyEntryID, null, null);
                            _sharedNewRecon.judgment = Judgment.New;
                            _sharedNewRecon.judgmentBatchSize = 0;
                            _sharedNewRecon.judgmentAction = "similar";
                        }
                        _sharedNewRecon.judgmentBatchSize++;
                        recon = _sharedNewRecon;
                    } else {
                        if (_dupReconMap.containsKey(cell.recon.id)) {
                            recon = _dupReconMap.get(cell.recon.id);
                            recon.judgmentBatchSize++;
                        } else {
                            recon = cell.recon.dup(_historyEntryID);
                            recon.judgmentBatchSize = 1;
                            recon.matchRank = -1;
                            recon.judgmentAction = "similar";
                            if (_judgment == Judgment.Matched) {
                                recon.judgment = Recon.Judgment.Matched;
                                recon.match = _match;
                                if (recon.candidates != null) {
                                    for (int m = 0; m < recon.candidates.size(); m++) {
                                        if (recon.candidates.get(m).id.equals(_match.id)) {
                                            recon.matchRank = m;
                                            break;
                                        }
                                    }
                                }
                            } else if (_judgment == Judgment.New) {
                                recon.judgment = Recon.Judgment.New;
                                recon.match = null;
                            } else if (_judgment == Judgment.None) {
                                recon.judgment = Recon.Judgment.None;
                                recon.match = null;
                            }
                            _dupReconMap.put(cell.recon.id, recon);
                        }
                    }
                    Cell newCell = new Cell(cell.value, recon);
                    CellChange cellChange = new CellChange(rowIndex, _cellIndex, cell, newCell);
                    _cellChanges.add(cellChange);
                }
            }
            return false;
        }
    }.init(column.getCellIndex(), cellChanges, historyEntryID);
}
Also used : HashMap(java.util.HashMap) CellChange(com.google.refine.model.changes.CellChange) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Recon(com.google.refine.model.Recon) Cell(com.google.refine.model.Cell)

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