Compare commits
10 commits
3dd943141f
...
361cb7dec2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
361cb7dec2 | ||
|
|
88cb9de5fa | ||
|
|
e94f7820fb | ||
|
|
cf983c57d6 | ||
|
|
e138171039 | ||
|
|
e80a46c532 | ||
|
|
6596561e6c | ||
|
|
ddad4dc73b | ||
|
|
b7f23d885a | ||
|
|
238eb6e4c2 |
3 changed files with 347 additions and 226 deletions
|
|
@ -0,0 +1,230 @@
|
|||
package ru.micord.ervu_dashboard.service;
|
||||
|
||||
import model.FileModel;
|
||||
import model.column.GridDisplayColumn;
|
||||
import model.grid.GridColumnAggregationValues;
|
||||
import model.grid.GridDataExportFormat;
|
||||
import model.grid.GridRow;
|
||||
import model.grid.GridRows;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
import property.enums.GridDisplayType;
|
||||
import property.grid.Formatter;
|
||||
import property.grid.GridColumn;
|
||||
import ru.cg.webbpm.modules.database.bean.entity_graph.EntityColumn;
|
||||
import service.GridV2ServiceImpl;
|
||||
import utils.ComplexColumnUtils;
|
||||
import utils.GridUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
//todo: Убрать этот костыльный сервис, обсудить и реализовать аналогичную логику в платформе
|
||||
public class ExportGridV2Service extends GridV2ServiceImpl {
|
||||
|
||||
protected FileModel getFileModelWithContent(String[] columnIdsToExport,
|
||||
GridDataExportFormat exportFormat, GridRows gridRows) {
|
||||
FileModel fileModel = new FileModel();
|
||||
fileModel.setFileId(this.getObjectId());
|
||||
|
||||
switch (exportFormat) {
|
||||
case XLS:
|
||||
fileModel.setFileExtension(".xls");
|
||||
fileModel.setFileContent(getXlsContent(columnIdsToExport, gridRows));
|
||||
break;
|
||||
case XLSX:
|
||||
fileModel.setFileExtension(".xlsx");
|
||||
fileModel.setFileContent(getXlsxContent(columnIdsToExport, gridRows));
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown export format = " + exportFormat);
|
||||
}
|
||||
fileModel.setFileName(
|
||||
LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))
|
||||
+ fileModel.fileExtension);
|
||||
fileModel.setIsImage(false);
|
||||
|
||||
return fileModel;
|
||||
}
|
||||
|
||||
|
||||
private String getXlsxContent(String[] columnIdsToExport, GridRows gridRows) {
|
||||
return getExcelContent(columnIdsToExport, gridRows, new SXSSFWorkbook());
|
||||
}
|
||||
|
||||
private String getXlsContent(String[] columnIdsToExport, GridRows gridRows) {
|
||||
return getExcelContent(columnIdsToExport, gridRows, new HSSFWorkbook());
|
||||
}
|
||||
|
||||
private String getExcelContent(String[] columnIdsToExport, GridRows gridRows,
|
||||
Workbook workbook) {
|
||||
List<GridColumn> orderedColumns = getOrderedColumns(columnIdsToExport);
|
||||
Sheet sheet = workbook.createSheet();
|
||||
writeHeaderLine(workbook, sheet, orderedColumns, gridRows.getAggregationValues() != null);
|
||||
writeDataLines(workbook, sheet, orderedColumns, gridRows);
|
||||
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
|
||||
workbook.write(bos);
|
||||
return Base64.getEncoder().encodeToString(bos.toByteArray());
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void autosizeByHeader(Sheet sheet, int columnIndex, String header, int minChars, int padding) {
|
||||
int length = header != null ? header.length() : 0;
|
||||
|
||||
// Берём максимум из (минимальная ширина, длина заголовка + отступ)
|
||||
int finalWidth = Math.max(minChars, length + padding);
|
||||
|
||||
sheet.setColumnWidth(columnIndex, finalWidth * 256);
|
||||
}
|
||||
|
||||
private void writeHeaderLine(Workbook workbook, Sheet sheet,
|
||||
List<GridColumn> orderedColumns, boolean hasAggregationValues) {
|
||||
Row row = sheet.createRow(0);
|
||||
CellStyle style = createBoldStyle(workbook);
|
||||
int minWidth = 20;
|
||||
int padding = 5;
|
||||
|
||||
createCell(row, 0, "№", style);
|
||||
autosizeByHeader(sheet, 0, "№", 5, 3);
|
||||
|
||||
int colIndex = hasAggregationValues ? 2 : 1;
|
||||
for (GridColumn column : orderedColumns) {
|
||||
createCell(row, colIndex, column.displayName, style);
|
||||
autosizeByHeader(sheet, colIndex, column.displayName, minWidth, padding);
|
||||
colIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
private void createCell(Row row, int columnCount, Object value, CellStyle style) {
|
||||
Cell cell = row.createCell(columnCount);
|
||||
|
||||
if (value == null) {
|
||||
cell.setBlank();
|
||||
}
|
||||
else if (value instanceof Number) {
|
||||
cell.setCellValue(((Number) value).doubleValue());
|
||||
}
|
||||
else if (value instanceof Boolean) {
|
||||
cell.setCellValue((Boolean) value);
|
||||
}
|
||||
else if (value instanceof Date) {
|
||||
cell.setCellValue((Date) value);
|
||||
}
|
||||
else if (value instanceof LocalDate) {
|
||||
cell.setCellValue((LocalDate) value);
|
||||
}
|
||||
else if (value instanceof LocalDateTime) {
|
||||
cell.setCellValue((LocalDateTime) value);
|
||||
}
|
||||
else {
|
||||
cell.setCellValue(value.toString());
|
||||
}
|
||||
cell.setCellStyle(style);
|
||||
}
|
||||
|
||||
private void writeDataLines(Workbook workbook, Sheet sheet,
|
||||
List<GridColumn> orderedColumns, GridRows gridRows) {
|
||||
int rowCount = 1;
|
||||
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
style.setWrapText(true);
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
GridColumnAggregationValues aggregationValues = gridRows.getAggregationValues();
|
||||
|
||||
for (GridRow gridRow : gridRows.getRows()) {
|
||||
Row row = sheet.createRow(rowCount);
|
||||
createCell(row, 0, rowCount, style);
|
||||
int columnCount = aggregationValues != null ? 2 : 1;
|
||||
row.setHeight((short) -1);
|
||||
for (GridColumn gridColumn : orderedColumns) {
|
||||
createCell(row, columnCount,
|
||||
gridRow.get(getClientColumnId(gridColumn)), style
|
||||
);
|
||||
columnCount++;
|
||||
}
|
||||
rowCount++;
|
||||
}
|
||||
|
||||
if (aggregationValues != null) {
|
||||
Row aggregationRow = sheet.createRow(rowCount);
|
||||
CellStyle boldStyle = createBoldStyle(workbook);
|
||||
createCell(aggregationRow, 0, aggregationHeaderForExport, boldStyle);
|
||||
|
||||
int columnCount = 2;
|
||||
for (GridColumn gridColumn : orderedColumns) {
|
||||
createCell(aggregationRow, columnCount,
|
||||
aggregationValues.get(getClientColumnId(gridColumn)), boldStyle
|
||||
);
|
||||
columnCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private CellStyle createBoldStyle(Workbook workbook) {
|
||||
CellStyle boldStyle = workbook.createCellStyle();
|
||||
Font font = workbook.createFont();
|
||||
font.setBold(true);
|
||||
boldStyle.setFont(font);
|
||||
boldStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
return boldStyle;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map.Entry<String, Object> getValueEntry(GridColumn column, Map<EntityColumn, Object> columnToDataMap) {
|
||||
if (column.isMultiColumn()) {
|
||||
return getEntryFromMultiColumn(column, columnToDataMap);
|
||||
}
|
||||
else return super.getValueEntry(column, columnToDataMap);
|
||||
|
||||
}
|
||||
|
||||
private Map.Entry<String, Object> getEntryFromMultiColumn(final GridColumn column,
|
||||
final Map<EntityColumn, Object> columnToDataMap) {
|
||||
String multiColumnName = evaluateMultiColumnName(column);
|
||||
String multiColumnValue = evaluateMultiColumnValue(column, columnToDataMap);
|
||||
return new AbstractMap.SimpleEntry<>(multiColumnName, multiColumnValue);
|
||||
}
|
||||
|
||||
private String evaluateMultiColumnName(final GridColumn column) {
|
||||
List<EntityColumn> entityColumns = Arrays.stream(column.displayColumns)
|
||||
.map(GridDisplayColumn::getEntityColumn)
|
||||
.collect(Collectors.toList());
|
||||
return GridUtils.toGridColumnName(entityColumns, GridDisplayType.MULTI_COLUMN);
|
||||
}
|
||||
|
||||
private String evaluateMultiColumnValue(final GridColumn column,
|
||||
final Map<EntityColumn, Object> columnToDataMap) {
|
||||
GridDisplayColumn[] displayColumns = column.displayColumns;
|
||||
return Arrays.stream(displayColumns)
|
||||
.map(displayColumn -> getMultiColFormattedValue(displayColumn, columnToDataMap))
|
||||
.filter(value -> !value.equals(""))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private String getMultiColFormattedValue(final GridDisplayColumn column,
|
||||
final Map<EntityColumn, Object> columnToDataMap) {
|
||||
EntityColumn entityColumn = column.getEntityColumn();
|
||||
Object formattedValue = GridUtils.getFormattedValue(
|
||||
columnToDataMap.get(entityColumn), entityColumn.getType(), column.getFormatter()
|
||||
);
|
||||
String formattedResult = formattedValue == null || formattedValue.equals("")
|
||||
? column.getEmptyValue()
|
||||
: column.getPrefix() + formattedValue + column.getPostfix();
|
||||
return HtmlUtils.htmlEscape(formattedResult, StandardCharsets.UTF_8.name());
|
||||
}
|
||||
|
||||
}
|
||||
2
pom.xml
2
pom.xml
|
|
@ -19,7 +19,7 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<enable.version.in.url>false</enable.version.in.url>
|
||||
<joda-time.version>2.9.2</joda-time.version>
|
||||
<webbpm-platform.version>3.192.31</webbpm-platform.version>
|
||||
<webbpm-platform.version>3.192.32</webbpm-platform.version>
|
||||
<wbp.overall-timeout>72000</wbp.overall-timeout>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue