use of com.google.refine.exporters.TabularSerializer.CellData 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.exporters.TabularSerializer.CellData in project OpenRefine by OpenRefine.
the class SpreadsheetSerializerTests method test30columns.
@Test
public void test30columns() {
// options is null, but unused
SUT.startFile(options);
List<CellData> cells = new ArrayList<>();
for (int i = 0; i < 30; i++) {
String colnum = Integer.toString(i);
CellData cell = new CellData("col" + colnum, "text" + colnum, "text" + colnum, null);
cells.add(cell);
}
SUT.addRow(cells, true);
SUT.addRow(cells, false);
List<Request> requests = SUT.prepareBatch(SUT.getRows());
assertEquals(requests.size(), 2);
for (Request request : requests) {
if (request.getAppendDimension() instanceof AppendDimensionRequest) {
return;
}
}
fail("Failed to find AppendDimensionRequest for columns > 26");
}
use of com.google.refine.exporters.TabularSerializer.CellData in project OpenRefine by OpenRefine.
the class SpreadsheetSerializerTests method testDataTypes.
@Test
public void testDataTypes() {
// options is null, but unused
SUT.startFile(options);
List<CellData> row = new ArrayList<>();
row.add(new CellData("null value", null, "null value", null));
row.add(new CellData("string value", "a string", "a string as string", null));
row.add(new CellData("integer value", 42, "42", null));
row.add(new CellData("double value", new Double(42), "42.0", null));
row.add(new CellData("boolean value", true, "true", null));
OffsetDateTime now = OffsetDateTime.now(ZoneId.of("Z"));
row.add(new CellData("datetime value", now, now.toString(), null));
SUT.addRow(row, false);
List<Request> requests = SUT.prepareBatch(SUT.getRows());
assertEquals(requests.size(), 1);
List<RowData> rows = requests.get(0).getAppendCells().getRows();
assertEquals(rows.size(), 1);
List<com.google.api.services.sheets.v4.model.CellData> values = rows.get(0).getValues();
assertEquals(values.size(), 6);
ExtendedValue value = values.get(0).getUserEnteredValue();
assertEquals(value.getStringValue(), "");
value = values.get(1).getUserEnteredValue();
assertEquals(value.getStringValue(), "a string");
value = values.get(2).getUserEnteredValue();
assertEquals(value.getNumberValue(), new Double(42));
value = values.get(3).getUserEnteredValue();
assertEquals(value.getNumberValue(), new Double(42));
value = values.get(4).getUserEnteredValue();
assertEquals(value.getBoolValue(), Boolean.TRUE);
value = values.get(5).getUserEnteredValue();
assertEquals(value.getStringValue(), now.toString());
}
Aggregations