diff --git a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/BaseStreamResourceWriter.java b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/BaseStreamResourceWriter.java index 2616d73..5c0a964 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/BaseStreamResourceWriter.java +++ b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/BaseStreamResourceWriter.java @@ -82,12 +82,12 @@ protected boolean isExportable(Grid.Column column) { @SuppressWarnings({"unchecked", "rawtypes"}) protected Stream getDataStream(Query newQuery) { - Stream stream = exporter.grid.getDataProvider().fetch(newQuery); + Stream stream = exporter.getGrid().getDataProvider().fetch(newQuery); if (stream.isParallel()) { LoggerFactory.getLogger(DataCommunicator.class) .debug( "Data provider {} has returned " + "parallel stream on 'fetch' call", - exporter.grid.getDataProvider().getClass()); + exporter.getGrid().getDataProvider().getClass()); stream = stream.collect(Collectors.toList()).stream(); assert !stream.isParallel(); } @@ -142,11 +142,13 @@ private String renderCellTextContent(Grid grid, Column column, String colu } protected Stream obtainDataStream(DataProvider dataProvider) { + Grid grid = exporter.getGrid(); + Object filter = null; try { Method method = DataCommunicator.class.getDeclaredMethod("getFilter"); method.setAccessible(true); - filter = method.invoke(exporter.grid.getDataCommunicator()); + filter = method.invoke(grid.getDataCommunicator()); } catch (Exception e) { LOGGER.error("Unable to get filter from DataCommunicator", e); } @@ -154,19 +156,19 @@ protected Stream obtainDataStream(DataProvider dataProvider) { Stream dataStream; // special handling for hierarchical data provider - if (exporter.grid.getDataProvider() instanceof HierarchicalDataProvider) { - return obtainFlattenedHierarchicalDataStream(exporter.grid); + if (grid.getDataProvider() instanceof HierarchicalDataProvider) { + return obtainFlattenedHierarchicalDataStream(grid); } else if (dataProvider instanceof AbstractBackEndDataProvider) { - GridLazyDataView gridLazyDataView = exporter.grid.getLazyDataView(); + GridLazyDataView gridLazyDataView = grid.getLazyDataView(); dataStream = gridLazyDataView.getItems(); } else { @SuppressWarnings({"rawtypes", "unchecked"}) Query streamQuery = new Query<>( 0, - exporter.grid.getDataProvider().size(new Query(filter)), - exporter.grid.getDataCommunicator().getBackEndSorting(), - exporter.grid.getDataCommunicator().getInMemorySorting(), + grid.getDataProvider().size(new Query(filter)), + grid.getDataCommunicator().getBackEndSorting(), + grid.getDataCommunicator().getInMemorySorting(), filter); dataStream = getDataStream(streamQuery); } diff --git a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/CsvStreamResourceWriter.java b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/CsvStreamResourceWriter.java index dd470b1..6a9a450 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/CsvStreamResourceWriter.java +++ b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/CsvStreamResourceWriter.java @@ -21,6 +21,7 @@ package com.flowingcode.vaadin.addons.gridexporter; import com.opencsv.CSVWriter; +import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.data.binder.BeanPropertySet; import com.vaadin.flow.data.binder.PropertySet; import com.vaadin.flow.server.VaadinSession; @@ -57,16 +58,17 @@ public void accept(OutputStream out, VaadinSession session) throws IOException { session.lock(); try { + Grid grid = exporter.getGrid(); exporter.setColumns( - exporter.grid.getColumns().stream() + grid.getColumns().stream() .filter(this::isExportable) .collect(Collectors.toList())); - headers = getGridHeaders(exporter.grid).stream().map(Pair::getLeft).toArray(String[]::new); - data = obtainDataStream(exporter.grid.getDataProvider()) + headers = getGridHeaders(grid).stream().map(Pair::getLeft).toArray(String[]::new); + data = obtainDataStream(grid.getDataProvider()) .map(this::buildRow) .collect(Collectors.toList()); - footers = getGridFooters(exporter.grid).stream() + footers = getGridFooters(grid).stream() .filter(pair -> StringUtils.isNotBlank(pair.getKey())) .map(Pair::getLeft) .toArray(String[]::new); diff --git a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/DocxStreamResourceWriter.java b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/DocxStreamResourceWriter.java index 5a37bfc..1c19908 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/DocxStreamResourceWriter.java +++ b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/DocxStreamResourceWriter.java @@ -21,6 +21,7 @@ package com.flowingcode.vaadin.addons.gridexporter; import com.vaadin.flow.component.grid.ColumnTextAlign; +import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.Grid.Column; import com.vaadin.flow.data.binder.BeanPropertySet; import com.vaadin.flow.data.binder.PropertySet; @@ -80,8 +81,9 @@ protected XWPFDocument createDoc(VaadinSession session) throws IOException { } private XWPFDocument createDoc() throws IOException { + Grid grid = exporter.getGrid(); exporter.setColumns( - exporter.grid.getColumns().stream() + grid.getColumns().stream() .filter(this::isExportable) .collect(Collectors.toList())); @@ -129,17 +131,17 @@ private XWPFDocument createDoc() throws IOException { cctblgridcol, "" + Math.round(9638 / exporter.getColumns().size())); }); - List>> headers = getGridHeaders(exporter.grid); + List>> headers = getGridHeaders(grid); XWPFTableCell cell = findCellWithPlaceHolder(table, exporter.headersPlaceHolder); if (cell != null) { fillHeaderOrFooter(table, cell, headers, true, exporter.headersPlaceHolder); } cell = findCellWithPlaceHolder(table, exporter.dataPlaceHolder); - fillData(table, cell, exporter.grid.getDataProvider()); + fillData(table, cell, grid.getDataProvider()); cell = findCellWithPlaceHolder(table, exporter.footersPlaceHolder); - List>> footers = getGridFooters(exporter.grid); + List>> footers = getGridFooters(grid); if (cell != null) { fillHeaderOrFooter(table, cell, footers, false, exporter.footersPlaceHolder); } @@ -188,7 +190,9 @@ private void buildRow( if (exporter.propertySet == null) { exporter.propertySet = (PropertySet) BeanPropertySet.get(item.getClass()); } - if (exporter.getColumns().isEmpty()) throw new IllegalStateException("Grid has no columns"); + if (exporter.getColumns().isEmpty()) { + throw new IllegalStateException("Grid has no columns"); + } int[] currentColumn = new int[1]; currentColumn[0] = row.getTableCells().indexOf(startingCell); @@ -200,7 +204,9 @@ private void buildRow( XWPFTableCell currentCell = startingCell; if (row.getTableCells().indexOf(startingCell) < currentColumn[0]) { currentCell = startingCell.getTableRow().getCell(currentColumn[0]); - if (currentCell == null) currentCell = startingCell.getTableRow().createCell(); + if (currentCell == null) { + currentCell = startingCell.getTableRow().createCell(); + } } PoiHelper.setWidth(currentCell, "" + Math.round(9638 / exporter.getColumns().size())); currentCell.getCTTc().setTcPr(tcpr); @@ -226,12 +232,12 @@ private void buildCell(Object value, XWPFTableCell cell, CTPPr ctpPr, CTRPr ctrP if (value == null) { setCellValue("", cell, exporter.dataPlaceHolder, ctpPr, ctrPr); } else if (value instanceof Boolean) { - setCellValue("" + (Boolean) value, cell, exporter.dataPlaceHolder, ctpPr, ctrPr); + setCellValue("" + value, cell, exporter.dataPlaceHolder, ctpPr, ctrPr); } else if (value instanceof Calendar) { Calendar calendar = (Calendar) value; setCellValue("" + calendar.getTime(), cell, exporter.dataPlaceHolder, ctpPr, ctrPr); } else if (value instanceof Double) { - setCellValue("" + (Double) value, cell, exporter.dataPlaceHolder, ctpPr, ctrPr); + setCellValue("" + value, cell, exporter.dataPlaceHolder, ctpPr, ctrPr); } else { setCellValue("" + value.toString(), cell, exporter.dataPlaceHolder, ctpPr, ctrPr); } @@ -350,9 +356,12 @@ private XWPFTable findTable(XWPFDocument doc) { .forEach( row -> { XWPFTableCell cell = row.getCell(0); - if (cell.getText().equals(exporter.headersPlaceHolder)) + if (cell.getText().equals(exporter.headersPlaceHolder)) { foundHeaders[0] = true; - if (cell.getText().equals(exporter.dataPlaceHolder)) foundData[0] = true; + } + if (cell.getText().equals(exporter.dataPlaceHolder)) { + foundData[0] = true; + } }); if (foundHeaders[0] && foundData[0]) { result[0] = table; diff --git a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/ExcelStreamResourceWriter.java b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/ExcelStreamResourceWriter.java index 7a3587a..dab01be 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/ExcelStreamResourceWriter.java +++ b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/ExcelStreamResourceWriter.java @@ -54,6 +54,7 @@ import org.slf4j.LoggerFactory; import com.vaadin.flow.component.ComponentUtil; import com.vaadin.flow.component.grid.ColumnTextAlign; +import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.Grid.Column; import com.vaadin.flow.data.binder.BeanPropertySet; import com.vaadin.flow.data.binder.PropertySet; @@ -83,7 +84,8 @@ public void accept(OutputStream out, VaadinSession session) throws IOException { private Workbook createWorkbook(VaadinSession session) { session.lock(); try { - exporter.setColumns(exporter.grid.getColumns().stream().filter(this::isExportable) + Grid grid = exporter.getGrid(); + exporter.setColumns(grid.getColumns().stream().filter(this::isExportable) .peek(col -> ComponentUtil.setData(col, COLUMN_CELLSTYLE_MAP, null)) .collect(Collectors.toList())); Workbook wb = getBaseTemplateWorkbook(); @@ -95,7 +97,7 @@ private Workbook createWorkbook(VaadinSession session) { } Cell cell = findCellWithPlaceHolder(sheet, exporter.headersPlaceHolder); - List>> headers = getGridHeaders(exporter.grid); + List>> headers = getGridHeaders(grid); fillHeaderOrFooter(sheet, cell, headers, true); if (exporter.autoMergeTitle && titleCell != null && exporter.getColumns().size()>1) { @@ -114,7 +116,7 @@ private Workbook createWorkbook(VaadinSession session) { Sheet tempSheet = wb.cloneSheet(exporter.sheetNumber); int lastRow = - fillData(sheet, cell, exporter.grid.getDataProvider(), dataRange, titleCell != null); + fillData(sheet, cell, grid.getDataProvider(), dataRange, titleCell != null); applyConditionalFormattings(sheet, dataRange); @@ -123,7 +125,7 @@ private Workbook createWorkbook(VaadinSession session) { wb.removeSheetAt(exporter.sheetNumber + 1); cell = findCellWithPlaceHolder(sheet, exporter.footersPlaceHolder); - List>> footers = getGridFooters(exporter.grid); + List>> footers = getGridFooters(grid); if (cell != null) { fillHeaderOrFooter(sheet, cell, footers, false); } @@ -240,7 +242,9 @@ private void buildRow(T item, Sheet sheet, Cell startingCell) { if (exporter.propertySet == null) { exporter.propertySet = (PropertySet) BeanPropertySet.get(item.getClass()); } - if (exporter.getColumns().isEmpty()) throw new IllegalStateException("Grid has no columns"); + if (exporter.getColumns().isEmpty()) { + throw new IllegalStateException("Grid has no columns"); + } int[] currentColumn = new int[1]; currentColumn[0] = startingCell.getColumnIndex(); diff --git a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/GridExporter.java b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/GridExporter.java index d02474b..5578f62 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/gridexporter/GridExporter.java +++ b/src/main/java/com/flowingcode/vaadin/addons/gridexporter/GridExporter.java @@ -59,6 +59,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.Stream; +import lombok.Getter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -100,7 +101,8 @@ public class GridExporter implements Serializable { static final String COLUMN_FOOTER = "column-footer"; static final String COLUMN_POSITION = "column-position"; - Grid grid; + @Getter + private Grid grid; String titlePlaceHolder = "${title}"; String headersPlaceHolder = "${headers}";