Search in sources :

Example 1 with RowVisitor

use of com.google.refine.browsing.RowVisitor in project OpenRefine by OpenRefine.

the class RowReorderOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Engine engine = new Engine(project);
    engine.setMode(_mode);
    List<Integer> rowIndices = new ArrayList<Integer>();
    if (_mode == Mode.RowBased) {
        RowVisitor visitor = new IndexingVisitor(rowIndices);
        if (_sorting != null) {
            SortingRowVisitor srv = new SortingRowVisitor(visitor);
            srv.initializeFromConfig(project, _sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        engine.getAllRows().accept(project, visitor);
    } else {
        RecordVisitor visitor = new IndexingVisitor(rowIndices);
        if (_sorting != null) {
            SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
            srv.initializeFromConfig(project, _sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        engine.getAllRecords().accept(project, visitor);
    }
    return new HistoryEntry(historyEntryID, project, "Reorder rows", this, new RowReorderChange(rowIndices));
}
Also used : RowReorderChange(com.google.refine.model.changes.RowReorderChange) ArrayList(java.util.ArrayList) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) HistoryEntry(com.google.refine.history.HistoryEntry) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) RecordVisitor(com.google.refine.browsing.RecordVisitor) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) Engine(com.google.refine.browsing.Engine) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor)

Example 2 with RowVisitor

use of com.google.refine.browsing.RowVisitor in project OpenRefine by OpenRefine.

the class ColumnAdditionOperation method createRowVisitor.

protected RowVisitor createRowVisitor(Project project, List<CellAtRow> cellsAtRows) throws Exception {
    Column column = project.columnModel.getColumnByName(_baseColumnName);
    Evaluable eval = MetaParser.parse(_expression);
    Properties bindings = ExpressionUtils.createBindings(project);
    return new RowVisitor() {

        int cellIndex;

        Properties bindings;

        List<CellAtRow> cellsAtRows;

        Evaluable eval;

        public RowVisitor init(int cellIndex, Properties bindings, List<CellAtRow> cellsAtRows, Evaluable eval) {
            this.cellIndex = cellIndex;
            this.bindings = bindings;
            this.cellsAtRows = cellsAtRows;
            this.eval = eval;
            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);
            Cell newCell = null;
            ExpressionUtils.bind(bindings, row, rowIndex, _baseColumnName, cell);
            Object o = eval.evaluate(bindings);
            if (o != null) {
                if (o instanceof Cell) {
                    newCell = (Cell) o;
                } else if (o instanceof WrappedCell) {
                    newCell = ((WrappedCell) o).cell;
                } else {
                    Serializable v = ExpressionUtils.wrapStorable(o);
                    if (ExpressionUtils.isError(v)) {
                        if (_onError == OnError.SetToBlank) {
                            return false;
                        } else if (_onError == OnError.KeepOriginal) {
                            v = cell != null ? cell.value : null;
                        }
                    }
                    if (v != null) {
                        newCell = new Cell(v, null);
                    }
                }
            }
            if (newCell != null) {
                cellsAtRows.add(new CellAtRow(rowIndex, newCell));
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellsAtRows, eval);
}
Also used : Serializable(java.io.Serializable) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) CellAtRow(com.google.refine.model.changes.CellAtRow) WrappedCell(com.google.refine.expr.WrappedCell) Column(com.google.refine.model.Column) Row(com.google.refine.model.Row) CellAtRow(com.google.refine.model.changes.CellAtRow) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Example 3 with RowVisitor

use of com.google.refine.browsing.RowVisitor 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) 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) 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) Judgment(com.google.refine.model.Recon.Judgment) HashSet(java.util.HashSet)

Example 4 with RowVisitor

use of com.google.refine.browsing.RowVisitor in project OpenRefine by OpenRefine.

the class CustomizableTabularExporterUtilities method exportRows.

public static void exportRows(final Project project, final Engine engine, Properties params, final TabularSerializer serializer) {
    String optionsString = (params != null) ? params.getProperty("options") : null;
    JsonNode optionsTemp = null;
    if (optionsString != null) {
        try {
            optionsTemp = ParsingUtilities.mapper.readTree(optionsString);
        } catch (IOException e) {
        // Ignore and keep options null.
        }
    }
    final JsonNode options = optionsTemp;
    final boolean outputColumnHeaders = options == null ? true : JSONUtilities.getBoolean(options, "outputColumnHeaders", true);
    final boolean outputEmptyRows = options == null ? false : JSONUtilities.getBoolean(options, "outputBlankRows", true);
    final int limit = options == null ? -1 : JSONUtilities.getInt(options, "limit", -1);
    final List<String> columnNames;
    final Map<String, CellFormatter> columnNameToFormatter = new HashMap<String, CustomizableTabularExporterUtilities.CellFormatter>();
    List<JsonNode> columnOptionArray = options == null ? null : JSONUtilities.getArray(options, "columns");
    if (columnOptionArray == null) {
        List<Column> columns = project.columnModel.columns;
        columnNames = new ArrayList<String>(columns.size());
        for (Column column : columns) {
            String name = column.getName();
            columnNames.add(name);
            columnNameToFormatter.put(name, new CellFormatter());
        }
    } else {
        int count = columnOptionArray.size();
        columnNames = new ArrayList<String>(count);
        for (int i = 0; i < count; i++) {
            JsonNode columnOptions = columnOptionArray.get(i);
            if (columnOptions != null) {
                String name = JSONUtilities.getString(columnOptions, "name", null);
                if (name != null) {
                    columnNames.add(name);
                    try {
                        columnNameToFormatter.put(name, ParsingUtilities.mapper.treeToValue(columnOptions, ColumnOptions.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    RowVisitor visitor = new RowVisitor() {

        int rowCount = 0;

        @Override
        public void start(Project project) {
            serializer.startFile(options);
            if (outputColumnHeaders) {
                List<CellData> cells = new ArrayList<TabularSerializer.CellData>(columnNames.size());
                for (String name : columnNames) {
                    cells.add(new CellData(name, name, name, null));
                }
                serializer.addRow(cells, true);
            }
        }

        @Override
        public boolean visit(Project project, int rowIndex, Row row) {
            List<CellData> cells = new ArrayList<TabularSerializer.CellData>(columnNames.size());
            int nonNullCount = 0;
            for (String columnName : columnNames) {
                Column column = project.columnModel.getColumnByName(columnName);
                CellFormatter formatter = columnNameToFormatter.get(columnName);
                CellData cellData = formatter.format(project, column, row.getCell(column.getCellIndex()));
                cells.add(cellData);
                if (cellData != null) {
                    nonNullCount++;
                }
            }
            if (nonNullCount > 0 || outputEmptyRows) {
                serializer.addRow(cells, false);
                rowCount++;
            }
            return limit > 0 && rowCount >= limit;
        }

        @Override
        public void end(Project project) {
            serializer.endFile();
        }
    };
    FilteredRows filteredRows = engine.getAllFilteredRows();
    filteredRows.accept(project, visitor);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) CellData(com.google.refine.exporters.TabularSerializer.CellData) FilteredRows(com.google.refine.browsing.FilteredRows) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) Row(com.google.refine.model.Row) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RowVisitor(com.google.refine.browsing.RowVisitor)

Example 5 with RowVisitor

use of com.google.refine.browsing.RowVisitor in project OpenRefine by OpenRefine.

the class TemplatingExporter method export.

@Override
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
    String limitString = options.getProperty("limit");
    int limit = limitString != null ? Integer.parseInt(limitString) : -1;
    String sortingJson = options.getProperty("sorting");
    String templateString = options.getProperty("template");
    String prefixString = options.getProperty("prefix");
    String suffixString = options.getProperty("suffix");
    String separatorString = options.getProperty("separator");
    Template template;
    try {
        template = Parser.parse(templateString);
    } catch (ParsingException e) {
        throw new IOException("Missing or bad template", e);
    }
    template.setPrefix(prefixString);
    template.setSuffix(suffixString);
    template.setSeparator(separatorString);
    if (!"true".equals(options.getProperty("preview"))) {
        TemplateConfig config = new TemplateConfig(templateString, prefixString, suffixString, separatorString);
        project.getMetadata().getPreferenceStore().put("exporters.templating.template", ParsingUtilities.defaultWriter.writeValueAsString(config));
    }
    if (engine.getMode() == Mode.RowBased) {
        FilteredRows filteredRows = engine.getAllFilteredRows();
        RowVisitor visitor = template.getRowVisitor(writer, limit);
        if (sortingJson != null) {
            SortingConfig sorting = SortingConfig.reconstruct(sortingJson);
            SortingRowVisitor srv = new SortingRowVisitor(visitor);
            srv.initializeFromConfig(project, sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        filteredRows.accept(project, visitor);
    } else {
        FilteredRecords filteredRecords = engine.getFilteredRecords();
        RecordVisitor visitor = template.getRecordVisitor(writer, limit);
        if (sortingJson != null) {
            SortingConfig sorting = SortingConfig.reconstruct(sortingJson);
            SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
            srv.initializeFromConfig(project, sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        filteredRecords.accept(project, visitor);
    }
}
Also used : SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) IOException(java.io.IOException) FilteredRecords(com.google.refine.browsing.FilteredRecords) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) RecordVisitor(com.google.refine.browsing.RecordVisitor) FilteredRows(com.google.refine.browsing.FilteredRows) Template(com.google.refine.templating.Template) ParsingException(com.google.refine.expr.ParsingException) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) SortingConfig(com.google.refine.sorting.SortingConfig)

Aggregations

RowVisitor (com.google.refine.browsing.RowVisitor)15 Column (com.google.refine.model.Column)12 Project (com.google.refine.model.Project)12 Cell (com.google.refine.model.Cell)11 Row (com.google.refine.model.Row)11 CellChange (com.google.refine.model.changes.CellChange)9 HashMap (java.util.HashMap)6 Engine (com.google.refine.browsing.Engine)5 FilteredRows (com.google.refine.browsing.FilteredRows)5 Recon (com.google.refine.model.Recon)4 Serializable (java.io.Serializable)4 ArrayList (java.util.ArrayList)4 RecordVisitor (com.google.refine.browsing.RecordVisitor)3 Evaluable (com.google.refine.expr.Evaluable)3 HistoryEntry (com.google.refine.history.HistoryEntry)3 SortingRecordVisitor (com.google.refine.sorting.SortingRecordVisitor)3 SortingRowVisitor (com.google.refine.sorting.SortingRowVisitor)3 IOException (java.io.IOException)3 Properties (java.util.Properties)3 Mode (com.google.refine.browsing.Engine.Mode)2