Search in sources :

Example 1 with MassChange

use of com.google.refine.model.changes.MassChange in project OpenRefine by OpenRefine.

the class ReconCopyAcrossColumnsOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(final Project project, final long historyEntryID) throws Exception {
    Engine engine = createEngine(project);
    final Column fromColumn = project.columnModel.getColumnByName(_fromColumnName);
    final List<Column> toColumns = new ArrayList<Column>(_toColumnNames.length);
    for (String c : _toColumnNames) {
        Column toColumn = project.columnModel.getColumnByName(c);
        if (toColumn != null) {
            toColumns.add(toColumn);
        }
    }
    final Set<Recon.Judgment> judgments = new HashSet<Recon.Judgment>(_judgments.length);
    for (String j : _judgments) {
        judgments.add(Recon.stringToJudgment(j));
    }
    final List<CellChange> cellChanges = new ArrayList<CellChange>(project.rows.size());
    if (fromColumn != null && toColumns.size() > 0) {
        final Map<Object, Recon> cellValueToRecon = new HashMap<Object, Recon>();
        FilteredRows filteredRows = engine.getAllFilteredRows();
        try {
            filteredRows.accept(project, new RowVisitor() {

                @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(fromColumn.getCellIndex());
                    if (cell != null && cell.value != null && cell.recon != null) {
                        if (judgments.contains(cell.recon.judgment)) {
                            cellValueToRecon.put(cell.value, cell.recon);
                        }
                    }
                    return false;
                }
            });
            filteredRows.accept(project, new RowVisitor() {

                @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) {
                    for (Column column : toColumns) {
                        int cellIndex = column.getCellIndex();
                        Cell cell = row.getCell(cellIndex);
                        if (cell != null && cell.value != null) {
                            Recon reconToCopy = cellValueToRecon.get(cell.value);
                            boolean judged = cell.recon != null && cell.recon.judgment != Judgment.None;
                            if (reconToCopy != null && (!judged || _applyToJudgedCells)) {
                                Cell newCell = new Cell(cell.value, reconToCopy);
                                CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                                cellChanges.add(cellChange);
                            }
                        }
                    }
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String description = "Copy " + cellChanges.size() + " recon judgments from column " + _fromColumnName + " to " + StringUtils.join(_toColumnNames);
    return new HistoryEntry(historyEntryID, project, description, this, new MassChange(cellChanges, false));
}
Also used : HashMap(java.util.HashMap) CellChange(com.google.refine.model.changes.CellChange) ArrayList(java.util.ArrayList) FilteredRows(com.google.refine.browsing.FilteredRows) JSONException(org.json.JSONException) MassChange(com.google.refine.model.changes.MassChange) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) HistoryEntry(com.google.refine.history.HistoryEntry) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) Recon(com.google.refine.model.Recon) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) Engine(com.google.refine.browsing.Engine) HashSet(java.util.HashSet) Judgment(com.google.refine.model.Recon.Judgment)

Example 2 with MassChange

use of com.google.refine.model.changes.MassChange in project OpenRefine by OpenRefine.

the class RowFlagOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Engine engine = createEngine(project);
    List<Change> changes = new ArrayList<Change>(project.rows.size());
    FilteredRows filteredRows = engine.getAllFilteredRows();
    filteredRows.accept(project, createRowVisitor(project, changes));
    return new HistoryEntry(historyEntryID, project, (_flagged ? "Flag" : "Unflag") + " " + changes.size() + " rows", this, new MassChange(changes, false));
}
Also used : MassChange(com.google.refine.model.changes.MassChange) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) RowFlagChange(com.google.refine.model.changes.RowFlagChange) Change(com.google.refine.history.Change) MassChange(com.google.refine.model.changes.MassChange) FilteredRows(com.google.refine.browsing.FilteredRows) Engine(com.google.refine.browsing.Engine)

Example 3 with MassChange

use of com.google.refine.model.changes.MassChange in project OpenRefine by OpenRefine.

the class RowStarOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Engine engine = createEngine(project);
    List<Change> changes = new ArrayList<Change>(project.rows.size());
    FilteredRows filteredRows = engine.getAllFilteredRows();
    filteredRows.accept(project, createRowVisitor(project, changes));
    return new HistoryEntry(historyEntryID, project, (_starred ? "Star" : "Unstar") + " " + changes.size() + " rows", this, new MassChange(changes, false));
}
Also used : MassChange(com.google.refine.model.changes.MassChange) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) RowStarChange(com.google.refine.model.changes.RowStarChange) Change(com.google.refine.history.Change) MassChange(com.google.refine.model.changes.MassChange) FilteredRows(com.google.refine.browsing.FilteredRows) Engine(com.google.refine.browsing.Engine)

Example 4 with MassChange

use of com.google.refine.model.changes.MassChange in project OpenRefine by OpenRefine.

the class MassChangeTests method testWrongReverseOrder.

/**
     * Test case for #914 - Demonstrates MassChange revert doesn't work by
     * adding two columns to a project with a MassChange and then reverting.
     * Without the fix, column "a" will be removed before column "b", causing
     * column "b" removal to fail because it won't be found at index 1 as
     * expected.
     */
@Test
public void testWrongReverseOrder() throws Exception {
    List<Change> changes = new ArrayList<Change>();
    changes.add(new ColumnAdditionChange("a", 0, new ArrayList<CellAtRow>()));
    changes.add(new ColumnAdditionChange("b", 1, new ArrayList<CellAtRow>()));
    MassChange massChange = new MassChange(changes, false);
    massChange.apply(project);
    massChange.revert(project);
    assertTrue(project.columnModel.columns.isEmpty());
}
Also used : MassChange(com.google.refine.model.changes.MassChange) ColumnAdditionChange(com.google.refine.model.changes.ColumnAdditionChange) ArrayList(java.util.ArrayList) Change(com.google.refine.history.Change) ColumnAdditionChange(com.google.refine.model.changes.ColumnAdditionChange) MassChange(com.google.refine.model.changes.MassChange) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) RefineTest(com.google.refine.tests.RefineTest)

Aggregations

MassChange (com.google.refine.model.changes.MassChange)4 ArrayList (java.util.ArrayList)4 Engine (com.google.refine.browsing.Engine)3 FilteredRows (com.google.refine.browsing.FilteredRows)3 Change (com.google.refine.history.Change)3 HistoryEntry (com.google.refine.history.HistoryEntry)3 RowVisitor (com.google.refine.browsing.RowVisitor)1 Cell (com.google.refine.model.Cell)1 Column (com.google.refine.model.Column)1 Project (com.google.refine.model.Project)1 Recon (com.google.refine.model.Recon)1 Judgment (com.google.refine.model.Recon.Judgment)1 Row (com.google.refine.model.Row)1 CellChange (com.google.refine.model.changes.CellChange)1 ColumnAdditionChange (com.google.refine.model.changes.ColumnAdditionChange)1 RowFlagChange (com.google.refine.model.changes.RowFlagChange)1 RowStarChange (com.google.refine.model.changes.RowStarChange)1 RefineTest (com.google.refine.tests.RefineTest)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1