Search in sources :

Example 11 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class ExcelRenderer method renderToStream.

public <E extends ManagedExecution<?> & SingleTableResult> void renderToStream(List<ResultInfo> idHeaders, E exec, OutputStream outputStream) throws IOException {
    final List<ResultInfo> resultInfosExec = exec.getResultInfos();
    setMetaData(exec);
    SXSSFSheet sheet = workbook.createSheet(C10N.get(ExcelSheetNameC10n.class, I18n.LOCALE.get()).result());
    try {
        sheet.setDefaultColumnWidth(config.getDefaultColumnWidth());
        // Create a table environment inside the excel sheet
        XSSFTable table = createTableEnvironment(exec, sheet);
        writeHeader(sheet, idHeaders, resultInfosExec, table);
        int writtenLines = writeBody(sheet, resultInfosExec, exec.streamResults());
        postProcessTable(sheet, table, writtenLines, idHeaders.size());
        workbook.write(outputStream);
    } finally {
        workbook.dispose();
    }
}
Also used : ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) SXSSFSheet(org.apache.poi.xssf.streaming.SXSSFSheet) XSSFTable(org.apache.poi.xssf.usermodel.XSSFTable)

Example 12 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class ExcelRenderer method writeHeader.

/**
 * Write the header and initialize the columns for the table environment.
 * Also autosize the columns according to the header width.
 */
private void writeHeader(SXSSFSheet sheet, List<ResultInfo> idHeaders, List<ResultInfo> infos, XSSFTable table) {
    CTTableColumns columns = table.getCTTable().addNewTableColumns();
    columns.setCount(idHeaders.size() + infos.size());
    UniqueNamer uniqueNamer = new UniqueNamer(cfg);
    {
        Row header = sheet.createRow(0);
        // First to create the columns and track them for auto size before the first row is written
        int currentColumn = 0;
        for (ResultInfo idHeader : idHeaders) {
            CTTableColumn column = columns.addNewTableColumn();
            // Table column ids MUST be set and MUST start at 1, excel will fail otherwise
            column.setId(currentColumn + 1);
            final String uniqueName = uniqueNamer.getUniqueName(idHeader);
            column.setName(uniqueName);
            Cell headerCell = header.createCell(currentColumn);
            headerCell.setCellValue(uniqueName);
            // Track column explicitly, because sheet.trackAllColumnsForAutoSizing() does not work with
            // sheet.getTrackedColumnsForAutoSizing(), if no flush has happened
            sheet.trackColumnForAutoSizing(currentColumn);
            currentColumn++;
        }
        for (ResultInfo info : infos) {
            final String columnName = uniqueNamer.getUniqueName(info);
            CTTableColumn column = columns.addNewTableColumn();
            column.setId(currentColumn + 1);
            column.setName(columnName);
            Cell headerCell = header.createCell(currentColumn);
            headerCell.setCellValue(columnName);
            sheet.trackColumnForAutoSizing(currentColumn);
            currentColumn++;
        }
    }
}
Also used : CTTableColumns(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns) UniqueNamer(com.bakdata.conquery.models.query.resultinfo.UniqueNamer) Row(org.apache.poi.ss.usermodel.Row) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn) Cell(org.apache.poi.ss.usermodel.Cell)

Example 13 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class ExcelRenderer method writeRowsForEntity.

/**
 * Writes the result lines for each entity.
 */
private int writeRowsForEntity(List<ResultInfo> infos, EntityResult internalRow, final AtomicInteger currentRow, PrintSettings settings, SXSSFSheet sheet) {
    String[] ids = settings.getIdMapper().map(internalRow).getExternalId();
    int writtenLines = 0;
    for (Object[] resultValues : internalRow.listResultLines()) {
        final int thisRow = currentRow.getAndIncrement();
        Row row = sheet.createRow(thisRow);
        // Write id cells
        int currentColumn = 0;
        for (String id : ids) {
            Cell idCell = row.createCell(currentColumn);
            idCell.setCellValue(id);
            currentColumn++;
        }
        // Write data cells
        for (int i = 0; i < infos.size(); i++) {
            ResultInfo resultInfo = infos.get(i);
            Object resultValue = resultValues[i];
            Cell dataCell = row.createCell(currentColumn);
            currentColumn++;
            if (resultValue == null) {
                continue;
            }
            // Fallback to string if type is not explicitly registered
            TypeWriter typeWriter = TYPE_WRITER_MAP.getOrDefault(resultInfo.getType().getClass(), ExcelRenderer::writeStringCell);
            typeWriter.writeCell(resultInfo, settings, dataCell, resultValue, styles);
        }
        if (thisRow == config.getLastRowToAutosize()) {
            // Last row rows to track for auto sizing the column width is reached. Untrack to remove the performance penalty.
            setColumnWidthsAndUntrack(sheet);
        }
        writtenLines++;
    }
    return writtenLines;
}
Also used : Row(org.apache.poi.ss.usermodel.Row) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) Cell(org.apache.poi.ss.usermodel.Cell)

Example 14 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class ExcelResultRenderTest method generateExpectedTSV.

private List<String> generateExpectedTSV(List<EntityResult> results, List<ResultInfo> resultInfos, PrintSettings settings) {
    List<String> expected = new ArrayList<>();
    expected.add(String.join("\t", printIdFields) + "\t" + getResultTypes().stream().map(ResultType::typeInfo).collect(Collectors.joining("\t")));
    results.stream().map(EntityResult.class::cast).forEach(res -> {
        for (Object[] line : res.listResultLines()) {
            StringJoiner valueJoiner = new StringJoiner("\t");
            valueJoiner.add(String.valueOf(res.getEntityId()));
            valueJoiner.add(String.valueOf(res.getEntityId()));
            for (int lIdx = 0; lIdx < line.length; lIdx++) {
                Object val = line[lIdx];
                if (val == null) {
                    valueJoiner.add("null");
                    continue;
                }
                ResultInfo info = resultInfos.get(lIdx);
                joinValue(settings, valueJoiner, val, info);
            }
            expected.add(valueJoiner.toString());
        }
    });
    return expected;
}
Also used : ArrayList(java.util.ArrayList) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) StringJoiner(java.util.StringJoiner)

Example 15 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class ExcelResultRenderTest method writeAndRead.

@Test
void writeAndRead() throws IOException {
    // Prepare every input data
    PrintSettings printSettings = new PrintSettings(true, Locale.GERMAN, null, CONFIG, (cer) -> EntityPrintId.from(Integer.toString(cer.getEntityId()), Integer.toString(cer.getEntityId())), (selectInfo) -> selectInfo.getSelect().getLabel());
    // The Shard nodes send Object[] but since Jackson is used for deserialization, nested collections are always a list because they are not further specialized
    List<EntityResult> results = getTestEntityResults();
    ManagedQuery mquery = new ManagedQuery(null, null, null) {

        public List<ResultInfo> getResultInfos() {
            return getResultTypes().stream().map(ResultTestUtil.TypedSelectDummy::new).map(select -> new SelectResultInfo(select, new CQConcept())).collect(Collectors.toList());
        }

        @Override
        public Stream<EntityResult> streamResults() {
            return results.stream();
        }
    };
    // First we write to the buffer, than we read from it and parse it as TSV
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ExcelRenderer renderer = new ExcelRenderer(new ExcelConfig(), printSettings);
    renderer.renderToStream(ResultTestUtil.ID_FIELDS, mquery, output);
    InputStream inputStream = new ByteArrayInputStream(output.toByteArray());
    List<String> computed = readComputed(inputStream, printSettings);
    List<String> expected = generateExpectedTSV(results, mquery.getResultInfos(), printSettings);
    log.info("Wrote and than read this excel data: {}", computed);
    assertThat(computed).isNotEmpty();
    assertThat(computed).isEqualTo(expected);
}
Also used : ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) ArrayList(java.util.ArrayList) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) EntityPrintId(com.bakdata.conquery.models.identifiable.mapping.EntityPrintId) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) Cell(org.apache.poi.ss.usermodel.Cell) I18n(com.bakdata.conquery.models.i18n.I18n) ResultType(com.bakdata.conquery.models.externalservice.ResultType) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) ResultTestUtil.getTestEntityResults(com.bakdata.conquery.io.result.ResultTestUtil.getTestEntityResults) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) ResultTestUtil.getResultTypes(com.bakdata.conquery.io.result.ResultTestUtil.getResultTypes) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ExcelConfig(com.bakdata.conquery.models.config.ExcelConfig) Test(org.junit.jupiter.api.Test) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ResultTestUtil(com.bakdata.conquery.io.result.ResultTestUtil) StringJoiner(java.util.StringJoiner) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Row(org.apache.poi.ss.usermodel.Row) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) NotNull(org.jetbrains.annotations.NotNull) InputStream(java.io.InputStream) NonPersistentStoreFactory(com.bakdata.conquery.util.NonPersistentStoreFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) ExcelConfig(com.bakdata.conquery.models.config.ExcelConfig) Test(org.junit.jupiter.api.Test)

Aggregations

ResultInfo (com.bakdata.conquery.models.query.resultinfo.ResultInfo)17 ArrayList (java.util.ArrayList)10 ManagedQuery (com.bakdata.conquery.models.query.ManagedQuery)6 PrintSettings (com.bakdata.conquery.models.query.PrintSettings)6 List (java.util.List)6 ConqueryConfig (com.bakdata.conquery.models.config.ConqueryConfig)5 ResultType (com.bakdata.conquery.models.externalservice.ResultType)5 I18n (com.bakdata.conquery.models.i18n.I18n)5 Locale (java.util.Locale)5 SelectResultInfo (com.bakdata.conquery.models.query.resultinfo.SelectResultInfo)4 UniqueNamer (com.bakdata.conquery.models.query.resultinfo.UniqueNamer)4 EntityResult (com.bakdata.conquery.models.query.results.EntityResult)4 StringJoiner (java.util.StringJoiner)4 Slf4j (lombok.extern.slf4j.Slf4j)4 CQConcept (com.bakdata.conquery.apiv1.query.concept.specific.CQConcept)3 ResultTestUtil (com.bakdata.conquery.io.result.ResultTestUtil)3 EntityPrintId (com.bakdata.conquery.models.identifiable.mapping.EntityPrintId)3 IOException (java.io.IOException)3 Stream (java.util.stream.Stream)3 Cell (org.apache.poi.ss.usermodel.Cell)3