Skip to content

Commit

Permalink
refactor: refactor package protected attribute "grid"
Browse files Browse the repository at this point in the history
Close #145
  • Loading branch information
javier-godoy committed Oct 15, 2024
1 parent c223806 commit 10d735e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ protected boolean isExportable(Grid.Column<T> column) {

@SuppressWarnings({"unchecked", "rawtypes"})
protected Stream<T> getDataStream(Query newQuery) {
Stream<T> stream = exporter.grid.getDataProvider().fetch(newQuery);
Stream<T> 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();
}
Expand Down Expand Up @@ -142,31 +142,33 @@ private String renderCellTextContent(Grid<T> grid, Column<T> column, String colu
}

protected Stream<T> obtainDataStream(DataProvider<T, ?> dataProvider) {
Grid<T> 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);
}

Stream<T> 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<T> gridLazyDataView = exporter.grid.getLazyDataView();
GridLazyDataView<T> gridLazyDataView = grid.getLazyDataView();
dataStream = gridLazyDataView.getItems();
} else {
@SuppressWarnings({"rawtypes", "unchecked"})
Query<T, ?> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,16 +58,17 @@ public void accept(OutputStream out, VaadinSession session) throws IOException {

session.lock();
try {
Grid<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,8 +81,9 @@ protected XWPFDocument createDoc(VaadinSession session) throws IOException {
}

private XWPFDocument createDoc() throws IOException {
Grid<T> grid = exporter.getGrid();
exporter.setColumns(
exporter.grid.getColumns().stream()
grid.getColumns().stream()
.filter(this::isExportable)
.collect(Collectors.toList()));

Expand Down Expand Up @@ -129,17 +131,17 @@ private XWPFDocument createDoc() throws IOException {
cctblgridcol, "" + Math.round(9638 / exporter.getColumns().size()));
});

List<Pair<String, Column<T>>> headers = getGridHeaders(exporter.grid);
List<Pair<String, Column<T>>> 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<Pair<String, Column<T>>> footers = getGridFooters(exporter.grid);
List<Pair<String, Column<T>>> footers = getGridFooters(grid);
if (cell != null) {
fillHeaderOrFooter(table, cell, footers, false, exporter.footersPlaceHolder);
}
Expand Down Expand Up @@ -188,7 +190,9 @@ private void buildRow(
if (exporter.propertySet == null) {
exporter.propertySet = (PropertySet<T>) 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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T> 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();
Expand All @@ -95,7 +97,7 @@ private Workbook createWorkbook(VaadinSession session) {
}

Cell cell = findCellWithPlaceHolder(sheet, exporter.headersPlaceHolder);
List<Pair<String, Column<T>>> headers = getGridHeaders(exporter.grid);
List<Pair<String, Column<T>>> headers = getGridHeaders(grid);

fillHeaderOrFooter(sheet, cell, headers, true);
if (exporter.autoMergeTitle && titleCell != null && exporter.getColumns().size()>1) {
Expand All @@ -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);

Expand All @@ -123,7 +125,7 @@ private Workbook createWorkbook(VaadinSession session) {
wb.removeSheetAt(exporter.sheetNumber + 1);

cell = findCellWithPlaceHolder(sheet, exporter.footersPlaceHolder);
List<Pair<String, Column<T>>> footers = getGridFooters(exporter.grid);
List<Pair<String, Column<T>>> footers = getGridFooters(grid);
if (cell != null) {
fillHeaderOrFooter(sheet, cell, footers, false);
}
Expand Down Expand Up @@ -240,7 +242,9 @@ private void buildRow(T item, Sheet sheet, Cell startingCell) {
if (exporter.propertySet == null) {
exporter.propertySet = (PropertySet<T>) 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -100,7 +101,8 @@ public class GridExporter<T> implements Serializable {
static final String COLUMN_FOOTER = "column-footer";
static final String COLUMN_POSITION = "column-position";

Grid<T> grid;
@Getter
private Grid<T> grid;

String titlePlaceHolder = "${title}";
String headersPlaceHolder = "${headers}";
Expand Down

0 comments on commit 10d735e

Please sign in to comment.