Skip to content

Commit 6d7a018

Browse files
committed
[KNOWAGE-9376] dashboard pdf export third commit
1 parent bb90d76 commit 6d7a018

3 files changed

Lines changed: 187 additions & 149 deletions

File tree

knowage-export/src/main/java/it/eng/knowage/engine/api/export/dashboard/DashboardExporter.java

Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@
1111
import it.eng.spagobi.tools.dataset.bo.VersionedDataSet;
1212
import it.eng.spagobi.utilities.exceptions.SpagoBIRuntimeException;
1313
import lombok.Getter;
14-
import org.apache.commons.lang3.StringUtils;
1514
import org.apache.logging.log4j.LogManager;
1615
import org.apache.logging.log4j.Logger;
17-
import org.apache.poi.ss.usermodel.*;
18-
import org.apache.poi.xssf.streaming.SXSSFSheet;
16+
import org.apache.poi.ss.usermodel.CellStyle;
17+
import org.apache.poi.ss.usermodel.HorizontalAlignment;
18+
import org.apache.poi.ss.usermodel.Sheet;
19+
import org.apache.poi.ss.usermodel.VerticalAlignment;
1920
import org.json.JSONArray;
2021
import org.json.JSONException;
2122
import org.json.JSONObject;
2223

2324
import java.util.*;
2425

25-
import static it.eng.knowage.engine.api.export.dashboard.StaticLiterals.EXCEL_ERROR;
26-
2726
@Getter
2827
public class DashboardExporter {
2928

@@ -519,23 +518,11 @@ protected String replaceWithCustomHeaderIfPresent(JSONObject settings, String co
519518
return columnName;
520519
}
521520

522-
public void adjustColumnWidth(Sheet sheet, String imageB64) {
521+
protected boolean isSummaryColumnVisible(List<String> columnsToHide, JSONObject column) {
523522
try {
524-
((SXSSFSheet) sheet).trackAllColumnsForAutoSizing();
525-
Row row = sheet.getRow(sheet.getLastRowNum());
526-
if (row != null) {
527-
for (int i = 0; i < row.getLastCellNum(); i++) {
528-
sheet.autoSizeColumn(i);
529-
if (StringUtils.isNotEmpty(imageB64) && (i == 0 || i == 1)) {
530-
// first or second column
531-
int colWidth = 25;
532-
if (sheet.getColumnWidthInPixels(i) < (colWidth * 256))
533-
sheet.setColumnWidth(i, colWidth * 256);
534-
}
535-
}
536-
}
537-
} catch (Exception e) {
538-
throw new SpagoBIRuntimeException(EXCEL_ERROR, e);
523+
return !columnsToHide.contains(column.getString("id"));
524+
} catch (JSONException e) {
525+
throw new SpagoBIRuntimeException(e);
539526
}
540527
}
541528

@@ -544,34 +531,6 @@ public void adjustColumnWidth(Sheet sheet, String imageB64) {
544531
* START OF STYLE METHODS
545532
*/
546533

547-
public CellStyle buildCellStyle(Sheet sheet, boolean bold, HorizontalAlignment alignment, VerticalAlignment verticalAlignment, short headerFontSizeShort) {
548-
549-
// CELL
550-
CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
551-
552-
// alignment
553-
cellStyle.setAlignment(alignment);
554-
cellStyle.setVerticalAlignment(verticalAlignment);
555-
556-
// FONT
557-
Font font = sheet.getWorkbook().createFont();
558-
559-
font.setFontHeightInPoints(headerFontSizeShort);
560-
561-
font.setBold(bold);
562-
563-
cellStyle.setFont(font);
564-
return cellStyle;
565-
}
566-
567-
protected void applyWholeRowStyle(int c, List<Boolean> styleCanBeOverriddenByWholeRowStyle, Row row, CellStyle cellStyle) {
568-
for (int previousCell = c - 1; previousCell >= 0; previousCell--) {
569-
if (styleCanBeOverriddenByWholeRowStyle.get(previousCell).equals(Boolean.TRUE)) {
570-
row.getCell(previousCell).setCellStyle(cellStyle);
571-
}
572-
}
573-
}
574-
575534
protected JSONObject getRowStyle(JSONObject settings) {
576535
JSONObject style = jsonObjectUtils.getStyleFromSettings(settings);
577536
if (style != null && style.has("rows")) {
@@ -673,16 +632,16 @@ private JSONArray getTarget(JSONArray styles, int i) throws JSONException {
673632
return target;
674633
}
675634

676-
protected JSONObject getTheRightStyleByColumnIdAndValue(Map<String, JSONArray> styles, String stringifiedValue, String columnId) throws JSONException {
677-
JSONObject customStyle = getTheStyleByValueAndColumnId(styles, stringifiedValue, columnId);
635+
protected JSONObject getTheRightStyleByColumnIdAndValue(Map<String, JSONArray> styles, String stringifiedValue, String columnId, String currentAlternateRowColor) throws JSONException {
636+
JSONObject customStyle = getTheStyleByValueAndColumnId(styles, stringifiedValue, columnId, currentAlternateRowColor);
678637

679638
if (customStyle.has("properties") && customStyle.getJSONObject("properties").length() == 0) {
680-
return getTheStyleByValueAndColumnId(styles, stringifiedValue, ALL_COLUMNS_STYLE);
639+
return getTheStyleByValueAndColumnId(styles, stringifiedValue, ALL_COLUMNS_STYLE, currentAlternateRowColor);
681640
}
682641
return customStyle;
683642
}
684643

685-
private JSONObject getTheStyleByValueAndColumnId(Map<String, JSONArray> styles, String stringifiedValue, String columnId) {
644+
private JSONObject getTheStyleByValueAndColumnId(Map<String, JSONArray> styles, String stringifiedValue, String columnId, String currentAlternateRowColor) throws JSONException {
686645
try {
687646
JSONObject nonConditionalProps = new JSONObject();
688647
if (styles != null) {
@@ -693,6 +652,12 @@ private JSONObject getTheStyleByValueAndColumnId(Map<String, JSONArray> styles,
693652
return getStyleObject(nonConditionalProps, STATIC_CUSTOM_STYLE, 0, false);
694653
} else {
695654
columnStyles = styles.get(ALL_COLUMNS_STYLE);
655+
if (currentAlternateRowColor != null) {
656+
for (int i = 0; i < columnStyles.length(); i++) {
657+
JSONObject style = columnStyles.getJSONObject(i);
658+
style.optJSONObject("properties").put("background-color", currentAlternateRowColor);
659+
}
660+
}
696661
}
697662
}
698663

knowage-export/src/main/java/it/eng/knowage/engine/api/export/dashboard/excel/DashboardExcelExporter.java

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import it.eng.knowage.commons.security.PathTraversalChecker;
55
import it.eng.knowage.engine.api.export.IWidgetExporter;
66
import it.eng.knowage.engine.api.export.dashboard.DashboardExporter;
7-
import it.eng.knowage.engine.api.export.dashboard.excel.exporters.DashboardWidgetExporterFactory;
87
import it.eng.knowage.engine.api.export.dashboard.Style;
8+
import it.eng.knowage.engine.api.export.dashboard.excel.exporters.DashboardWidgetExporterFactory;
99
import it.eng.spagobi.commons.SingletonConfig;
1010
import it.eng.spagobi.tenant.TenantManager;
1111
import it.eng.spagobi.utilities.exceptions.SpagoBIRuntimeException;
@@ -1034,7 +1034,7 @@ private CellStyle buildCols(Workbook wb, Sheet sheet, JSONObject settings, JSONA
10341034
CellStyle summaryCellStyle = buildPoiCellStyle(getStyleCustomObjFromProps(sheet, settings.getJSONObject("style").getJSONObject("summary"), ""), (XSSFFont) wb.createFont(), wb);
10351035
cell.setCellStyle(summaryCellStyle);
10361036
} else {
1037-
JSONObject theRightStyle = getTheRightStyleByColumnIdAndValue(columnStylesMap, stringifiedValue, column.optString("id"));
1037+
JSONObject theRightStyle = getTheRightStyleByColumnIdAndValue(columnStylesMap, stringifiedValue, column.optString("id"), defaultRowBackgroundColor);
10381038

10391039
styleCanBeOverriddenByWholeRowStyle.add(c, styleCanBeOverridden(theRightStyle));
10401040
if (theRightStyle.has("applyToWholeRow") && theRightStyle.getBoolean("applyToWholeRow")) {
@@ -1071,14 +1071,6 @@ private CellStyle buildCols(Workbook wb, Sheet sheet, JSONObject settings, JSONA
10711071
return cellStyle;
10721072
}
10731073

1074-
private boolean isSummaryColumnVisible(List<String> columnsToHide, JSONObject column) {
1075-
try {
1076-
return !columnsToHide.contains(column.getString("id"));
1077-
} catch (JSONException e) {
1078-
throw new SpagoBIRuntimeException(e);
1079-
}
1080-
}
1081-
10821074
private String getCellType(JSONObject column, String colName) {
10831075
try {
10841076
return column.getString("type");
@@ -1277,10 +1269,14 @@ public CellStyle buildPoiCellStyle(Style style, XSSFFont font, Workbook wb) {
12771269

12781270
if (!stringIsEmpty(style.getAlignItems())) {
12791271
cellStyle.setAlignment(getHorizontalAlignment(style.getAlignItems().toUpperCase()));
1272+
} else {
1273+
cellStyle.setAlignment(HorizontalAlignment.CENTER);
12801274
}
12811275

12821276
if (!stringIsEmpty(style.getJustifyContent())) {
12831277
cellStyle.setVerticalAlignment(getVerticalAlignment(style.getJustifyContent().toUpperCase()));
1278+
} else {
1279+
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
12841280
}
12851281

12861282
cellStyle.setFont(font);
@@ -1327,4 +1323,53 @@ private CellStyle getCellStyleByStyleKey(Workbook wb, Sheet sheet, String styleK
13271323
return cellStyle;
13281324
}
13291325

1326+
protected void applyWholeRowStyle(int c, List<Boolean> styleCanBeOverriddenByWholeRowStyle, Row row, CellStyle cellStyle) {
1327+
for (int previousCell = c - 1; previousCell >= 0; previousCell--) {
1328+
if (styleCanBeOverriddenByWholeRowStyle.get(previousCell).equals(Boolean.TRUE)) {
1329+
row.getCell(previousCell).setCellStyle(cellStyle);
1330+
}
1331+
}
1332+
}
1333+
1334+
public void adjustColumnWidth(Sheet sheet, String imageB64) {
1335+
try {
1336+
((SXSSFSheet) sheet).trackAllColumnsForAutoSizing();
1337+
Row row = sheet.getRow(sheet.getLastRowNum());
1338+
if (row != null) {
1339+
for (int i = 0; i < row.getLastCellNum(); i++) {
1340+
sheet.autoSizeColumn(i);
1341+
if (StringUtils.isNotEmpty(imageB64) && (i == 0 || i == 1)) {
1342+
// first or second column
1343+
int colWidth = 25;
1344+
if (sheet.getColumnWidthInPixels(i) < (colWidth * 256))
1345+
sheet.setColumnWidth(i, colWidth * 256);
1346+
}
1347+
}
1348+
}
1349+
} catch (Exception e) {
1350+
throw new SpagoBIRuntimeException(EXCEL_ERROR, e);
1351+
}
1352+
}
1353+
1354+
public CellStyle buildCellStyle(Sheet sheet, boolean bold, HorizontalAlignment alignment, VerticalAlignment verticalAlignment, short headerFontSizeShort) {
1355+
1356+
// CELL
1357+
CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
1358+
1359+
// alignment
1360+
cellStyle.setAlignment(alignment);
1361+
cellStyle.setVerticalAlignment(verticalAlignment);
1362+
1363+
// FONT
1364+
Font font = sheet.getWorkbook().createFont();
1365+
1366+
font.setFontHeightInPoints(headerFontSizeShort);
1367+
1368+
font.setBold(bold);
1369+
1370+
cellStyle.setFont(font);
1371+
return cellStyle;
1372+
}
1373+
1374+
13301375
}

0 commit comments

Comments
 (0)