Skip to content

Commit

Permalink
ExcelExporter: Add predicate to OneQueryStrategy
Browse files Browse the repository at this point in the history
Extends the exporter strategy to export a query document with exactly
one query by an alternate constructor that allows configuring a field
predicate that is passed on to .addSheet().

This allows for single-query document with filtered fields.
  • Loading branch information
Sven Helmberger committed Jun 20, 2023
1 parent 51ff471 commit 81b8c70
Showing 1 changed file with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
import de.quinscape.automaton.runtime.export.ExportResult;
import de.quinscape.automaton.runtime.export.GraphQLExporter;
import de.quinscape.automaton.runtime.export.GraphQLQueryContext;
import de.quinscape.automaton.runtime.util.SchemaReference;
import de.quinscape.domainql.DomainQL;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.function.Predicate;


/**
* GraphQLExport implementation for Excel sheets. Can be configured via builder including a strategy of how many
* queries to export into how many excel sheets.
*
* @see #newExporter(DomainQL)
* @see #newExporter(DomainQL)
*/
public class ExcelExporter
implements GraphQLExporter<byte[]>
Expand All @@ -27,14 +29,15 @@ public class ExcelExporter
public final static String EXCEL_MEDIA_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";



private final DomainQL domainQL;

private final String metaHeadingName;

private final String mediaType;

private final ExportStrategy exportStrategy;


ExcelExporter(
DomainQL domainQL,
ExportStrategy exportStrategy,
Expand All @@ -48,6 +51,7 @@ public class ExcelExporter
this.mediaType = mediaType;
}


@Override
public ExportResult<byte[]> export(GraphQLQueryContext ctx) throws IOException
{
Expand All @@ -74,7 +78,6 @@ public ExportResult<byte[]> export(GraphQLQueryContext ctx) throws IOException
}



/**
* Expects a single result that is converted into an Excel sheet name with a configured name, exporting the Excel
* file under a configured name.
Expand All @@ -84,28 +87,57 @@ public static class OneQueryStrategy
{
public final static OneQueryStrategy DEFAULT = new OneQueryStrategy(
"Automaton Export",
"automaton-$now.xlsx");
"automaton-$now.xlsx"
);

private final static Predicate<SchemaReference> ALL_FILES = ref -> true;

private final String sheetName;

private final String fileName;

private final Predicate<SchemaReference> fieldPredicate;


/**
* Creates a new OneQueryStrategy with the given sheet name and the given file name and a predicate to filter
* the fields.
*
* @param sheetName sheet name
* @param fileName filename (with optional "$now" placeholder)
*/
public OneQueryStrategy(String sheetName, String fileName)
{
this(sheetName, fileName, ALL_FILES);
}


/**
* Creates a new OneQueryStrategy with the given sheet name and the given file name and a predicate to filter
* the fields.
*
* @param sheetName sheet name
* @param fileName filename (with optional "$now" placeholder)
* @param fieldPredicate field predicate
*/
public OneQueryStrategy(String sheetName, String fileName, Predicate<SchemaReference> fieldPredicate)
{
this.sheetName = sheetName;
this.fileName = fileName;
this.fieldPredicate = fieldPredicate;
}


@Override
public String generateExport(ExcelExporterContext ctx, GraphQLQueryContext queryContext)
{
final GraphQLQueryContext.MethodResult mr = queryContext.getOnlyResult();
ctx.addSheet(sheetName, mr);
ctx.addSheet(sheetName, mr, fieldPredicate);
return fileName;
}
}


public static ExcelExporterBuilder newExporter(DomainQL domainQL)
{
return new ExcelExporterBuilder(domainQL);
Expand Down

0 comments on commit 81b8c70

Please sign in to comment.