Skip to content

refactor: refactor package protected attributes "grid" and "propertySet" #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
<artifactId>vaadin-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
Expand Down
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 All @@ -181,7 +183,9 @@ private Stream<T> obtainFlattenedHierarchicalDataStream(final Grid<T> grid) {
private ArrayList<T> fetchDataRecursive(final Grid<T> grid, T parent) {
ArrayList<T> result = new ArrayList<>();

if (parent != null) result.add(parent);
if (parent != null) {
result.add(parent);
}

HierarchicalDataProvider<T, ?> hDataProvider =
(HierarchicalDataProvider<T, ?>) grid.getDataProvider();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* Grid Exporter Add-on
* %%
* Copyright (C) 2022 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.flowingcode.vaadin.addons.gridexporter;

import java.util.EventObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* Grid Exporter Add-on
* %%
* Copyright (C) 2022 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.flowingcode.vaadin.addons.gridexporter;

import com.vaadin.flow.component.UI;
Expand Down Expand Up @@ -268,4 +287,4 @@ public final void accept(OutputStream stream, VaadinSession session) throws IOEx
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
package com.flowingcode.vaadin.addons.gridexporter;

import com.opencsv.CSVWriter;
import com.vaadin.flow.data.binder.BeanPropertySet;
import com.vaadin.flow.data.binder.PropertySet;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.server.VaadinSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
Expand All @@ -57,16 +55,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 All @@ -90,12 +89,11 @@ public void accept(OutputStream out, VaadinSession session) throws IOException {
}
}

@SuppressWarnings("unchecked")
private String[] buildRow(T item) {
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");

String[] result = new String[exporter.getColumns().size()];
int[] currentColumn = new int[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
* limitations under the License.
* #L%
*/
/** */
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;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.server.VaadinSession;
import java.io.IOException;
Expand Down Expand Up @@ -80,8 +78,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 +128,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 @@ -178,17 +177,16 @@ private void fillData(XWPFTable table, XWPFTableCell dataCell, DataProvider<T, ?
});
}

@SuppressWarnings("unchecked")
private void buildRow(
T item,
XWPFTableRow row,
XWPFTableCell startingCell,
CTTcPr tcpr,
XWPFTableCell templateCell) {
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 +198,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 +226,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 +350,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 @@ -20,6 +20,13 @@
/** */
package com.flowingcode.vaadin.addons.gridexporter;

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.provider.DataProvider;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.server.VaadinSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -52,14 +59,6 @@
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.grid.ColumnTextAlign;
import com.vaadin.flow.component.grid.Grid.Column;
import com.vaadin.flow.data.binder.BeanPropertySet;
import com.vaadin.flow.data.binder.PropertySet;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.server.VaadinSession;
/**
* @author mlopez
*/
Expand All @@ -70,7 +69,7 @@ class ExcelStreamResourceWriter<T> extends BaseStreamResourceWriter<T> {
private static final String DEFAULT_TEMPLATE = "/template.xlsx";
private static final String COLUMN_CELLSTYLE_MAP = "colum-cellstyle-map";
private static enum ExcelCellType {HEADER,CELL,FOOTER};

public ExcelStreamResourceWriter(GridExporter<T> exporter, String template) {
super(exporter, template, DEFAULT_TEMPLATE);
}
Expand All @@ -83,7 +82,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 +95,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 +114,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 +123,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 @@ -235,12 +235,11 @@ private int fillData(
return dataRange.getLastRow();
}

@SuppressWarnings("unchecked")
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
Loading
Loading