Search in sources :

Example 1 with CellData

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);
}
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 2 with CellData

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");
}
Also used : AppendDimensionRequest(com.google.api.services.sheets.v4.model.AppendDimensionRequest) ArrayList(java.util.ArrayList) Request(com.google.api.services.sheets.v4.model.Request) AppendDimensionRequest(com.google.api.services.sheets.v4.model.AppendDimensionRequest) CellData(com.google.refine.exporters.TabularSerializer.CellData) Test(org.testng.annotations.Test)

Example 3 with CellData

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());
}
Also used : ArrayList(java.util.ArrayList) Request(com.google.api.services.sheets.v4.model.Request) AppendDimensionRequest(com.google.api.services.sheets.v4.model.AppendDimensionRequest) ExtendedValue(com.google.api.services.sheets.v4.model.ExtendedValue) CellData(com.google.refine.exporters.TabularSerializer.CellData) RowData(com.google.api.services.sheets.v4.model.RowData) OffsetDateTime(java.time.OffsetDateTime) Test(org.testng.annotations.Test)

Aggregations

CellData (com.google.refine.exporters.TabularSerializer.CellData)3 ArrayList (java.util.ArrayList)3 AppendDimensionRequest (com.google.api.services.sheets.v4.model.AppendDimensionRequest)2 Request (com.google.api.services.sheets.v4.model.Request)2 Test (org.testng.annotations.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ExtendedValue (com.google.api.services.sheets.v4.model.ExtendedValue)1 RowData (com.google.api.services.sheets.v4.model.RowData)1 FilteredRows (com.google.refine.browsing.FilteredRows)1 RowVisitor (com.google.refine.browsing.RowVisitor)1 Column (com.google.refine.model.Column)1 Project (com.google.refine.model.Project)1 Row (com.google.refine.model.Row)1 IOException (java.io.IOException)1 OffsetDateTime (java.time.OffsetDateTime)1 HashMap (java.util.HashMap)1