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));
}
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);
}
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));
}
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);
}
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);
}
}
Aggregations