Skip to content

Commit ebfc26e

Browse files
committed
[KNOWAGE-9304] added logic to handle hidden summary columns and evaluate conditions dynamically.
1 parent 2aa0936 commit ebfc26e

2 files changed

Lines changed: 60 additions & 27 deletions

File tree

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

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class DashboardExcelExporter extends Common {
5252
private static final String[] WIDGETS_TO_IGNORE = {"image", "text", "selector", "selection", "html"};
5353
private static final int SHEET_NAME_MAX_LEN = 31;
5454
protected static final String DATE_FORMAT = "dd/MM/yyyy";
55-
public static final String TIMESTAMP_FORMAT = "dd/MM/yyyy HH:mm:ss.SSS";
5655

5756
private final StyleProvider styleProvider;
5857
private final JSONObjectUtils jsonObjectUtils;
@@ -141,9 +140,6 @@ public byte[] getScheduledBinaryData(String documentLabel) throws IOException, I
141140
LOGGER.info("CONFIG label=\"KNOWAGE.DASHBOARD.EXTERNAL_PROCESS_NAME\": " + cockpitExportExternalProcessName);
142141

143142
String stringifiedRequestUrl = url.toString();
144-
// replace localhost:8080 with 127.0.0.1:3000
145-
// stringifiedRequestUrl = stringifiedRequestUrl.replace("localhost:8080", "127.0.0.1:3000");
146-
147143
ProcessBuilder processBuilder = new ProcessBuilder(cockpitExportExternalProcessName, exportScriptFullPath.toString(),
148144
encodedUserId, outputDir.toString(), stringifiedRequestUrl);
149145

@@ -977,7 +973,7 @@ private JSONArray getDashboardWidgetsJson(String templateString) {
977973
}
978974
}
979975

980-
private void doTypeLogic(Workbook wb, int precision, String type, Cell cell, String stringifiedValue) {
976+
private void setFormattedCellValue(Workbook wb, int precision, String type, Cell cell, String stringifiedValue) {
981977
CreationHelper creationHelper = wb.getCreationHelper();
982978

983979
DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, getLocale());
@@ -1005,6 +1001,15 @@ private void doTypeLogic(Workbook wb, int precision, String type, Cell cell, Str
10051001
LOGGER.debug("Date will be exported as string due to error: ", e);
10061002
cell.setCellValue(stringifiedValue);
10071003
}
1004+
case "string":
1005+
if (!stringifiedValue.trim().isEmpty()) {
1006+
try {
1007+
cell.getCellStyle().setDataFormat(getFormat(precision, creationHelper));
1008+
cell.setCellValue(Integer.parseInt(stringifiedValue));
1009+
} catch (Exception e) {
1010+
cell.setCellValue(stringifiedValue);
1011+
}
1012+
}
10081013
break;
10091014
default:
10101015
cell.setCellValue(stringifiedValue);
@@ -1058,7 +1063,7 @@ public void fillTableSheetWithData(JSONObject dataStore, Workbook wb, Sheet shee
10581063
JSONArray columnsOrdered;
10591064
List<String> hiddenColumns;
10601065
if (widgetData.has("columns") && widgetData.getJSONArray("columns").length() > 0) {
1061-
hiddenColumns = getDashboardHiddenColumnsList(settings);
1066+
hiddenColumns = getDashboardHiddenColumnsList(settings, "hide");
10621067
columnsOrdered = getDashboardTableOrderedColumns(columnSelectedOfDataset, hiddenColumns, columns);
10631068
} else {
10641069
columnsOrdered = columns;
@@ -1265,7 +1270,7 @@ private CellStyle buildCols(Workbook wb, Sheet sheet, JSONObject settings, JSONA
12651270
String stringifiedValue = value != null ? value.toString() : "";
12661271

12671272
String styleKey;
1268-
if (r >= rows.length() - numberOfSummaryRows) {
1273+
if (r >= rows.length() - numberOfSummaryRows && !styleProvider.styleIsEmpty(styleProvider.getStyleCustomObjFromProps(sheet, settings.getJSONObject("style").getJSONObject("summary"), ""))) {
12691274
CellStyle summaryCellStyle = styleProvider.buildPoiCellStyle(styleProvider.getStyleCustomObjFromProps(sheet, settings.getJSONObject("style").getJSONObject("summary"), ""), (XSSFFont) wb.createFont(), wb);
12701275
cell.setCellStyle(summaryCellStyle);
12711276
} else {
@@ -1288,13 +1293,30 @@ private CellStyle buildCols(Workbook wb, Sheet sheet, JSONObject settings, JSONA
12881293
}
12891294
cell.setCellStyle(cellStyle);
12901295
}
1291-
doTypeLogic(wb, getPrecisionByColumn(settings, column), type, cell, stringifiedValue);
1296+
int precision = getPrecisionByColumn(settings, column);
12921297

1293-
if (r >= rows.length() - numberOfSummaryRows) {
1294-
cell.setCellValue(summaryRowsLabels.get(r - (rows.length() - numberOfSummaryRows)).concat(": ").concat(stringifiedValue));
1298+
if (r < rows.length() - numberOfSummaryRows) {
1299+
setFormattedCellValue(wb, precision, type, cell, stringifiedValue);
1300+
} else {
1301+
if (isSummaryColumnVisible(getDashboardHiddenColumnsList(settings, "hideFromSummary"), column)) {
1302+
String label = "";
1303+
if (colIndex.equals("column_1")) {
1304+
label = summaryRowsLabels.get(r - (rows.length() - numberOfSummaryRows)).concat(" ");
1305+
}
1306+
cell.setCellStyle(cellStyle);
1307+
setFormattedCellValue(wb, precision, type, cell, label.concat(stringifiedValue));
1308+
}
1309+
}
12951310
}
1311+
return cellStyle;
1312+
}
1313+
1314+
private boolean isSummaryColumnVisible(List<String> columnsToHide, JSONObject column) {
1315+
try {
1316+
return !columnsToHide.contains(column.getString("id"));
1317+
} catch (JSONException e) {
1318+
throw new SpagoBIRuntimeException(e);
12961319
}
1297-
return cellStyle;
12981320
}
12991321

13001322
private String getCellType(JSONObject column, String colName) {
@@ -1440,7 +1462,7 @@ private DataConsolidateFunction getAggregationFunction(String aggregation) {
14401462
};
14411463
}
14421464

1443-
protected List<String> getDashboardHiddenColumnsList(JSONObject settings) {
1465+
protected List<String> getDashboardHiddenColumnsList(JSONObject settings, String conditionToEvaluate) {
14441466
try {
14451467
List<String> hiddenColumns = new ArrayList<>();
14461468
if (areVisibilityConditionsEnabled(settings)) {
@@ -1450,7 +1472,7 @@ protected List<String> getDashboardHiddenColumnsList(JSONObject settings) {
14501472

14511473
for (int i = 0; i < conditions.length(); i++) {
14521474
JSONObject condition = conditions.getJSONObject(i);
1453-
if (columnMustBeHidden(condition)) {
1475+
if (columnMustBeHidden(condition, conditionToEvaluate)) {
14541476
JSONArray target;
14551477
try {
14561478
target = condition.getJSONArray("target");
@@ -1472,22 +1494,22 @@ protected List<String> getDashboardHiddenColumnsList(JSONObject settings) {
14721494
}
14731495
}
14741496

1475-
private boolean columnMustBeHidden(JSONObject condition) {
1497+
private boolean columnMustBeHidden(JSONObject condition, String conditionToEvaluate) {
14761498
try {
14771499
JSONObject conditionDefinition = condition.getJSONObject("condition");
14781500

1501+
14791502
return (conditionDefinition.getString("type").equals("always") &&
1480-
condition.getBoolean("hide"))
1503+
condition.getBoolean(conditionToEvaluate))
14811504
||
14821505
(conditionDefinition.getString("type").equals("variable") &&
1483-
condition.getBoolean("hide") && conditionIsApplicable(conditionDefinition.getString("variableValue"), conditionDefinition.getString("operator"), conditionDefinition.getString("value")));
1506+
condition.getBoolean(conditionToEvaluate) && conditionIsApplicable(conditionDefinition.getString("variableValue"), conditionDefinition.getString("operator"), conditionDefinition.getString("value")));
14841507

14851508
} catch (JSONException jsonException) {
14861509
LOGGER.error("Error while evaluating if column must be hidden according to variable.", jsonException);
14871510
return false;
14881511
}
14891512
}
1490-
14911513
private boolean areVisibilityConditionsEnabled(JSONObject settings) throws JSONException {
14921514
JSONObject visualization = jsonObjectUtils.getVisualizationFromSettings(settings);
14931515
return settings.has("visualization") &&

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,40 +286,40 @@ void buildHeaderCellStyle(Workbook wb, Sheet sheet, JSONObject settings, XSSFFon
286286
public CellStyle buildPoiCellStyle(Style style, XSSFFont font, Workbook wb) {
287287
CellStyle cellStyle = wb.createCellStyle();
288288

289-
if (stringIsNotEmpty(style.getFontSize())) {
289+
if (!stringIsEmpty(style.getFontSize())) {
290290
font.setFontHeightInPoints(Short.parseShort(getOnlyTheNumericValueFromString(style.getFontSize())));
291291
} else {
292292
font.setFontHeightInPoints(DEFAULT_FONT_SIZE);
293293
}
294294

295-
if (stringIsNotEmpty(style.getColor())) {
295+
if (!stringIsEmpty(style.getColor())) {
296296
font.setColor(getXSSFColorFromRGBA(style.getColor()));
297297
}
298298

299-
if (stringIsNotEmpty(style.getBackgroundColor())) {
299+
if (!stringIsEmpty(style.getBackgroundColor())) {
300300
cellStyle.setFillForegroundColor(getXSSFColorFromRGBA(style.getBackgroundColor()));
301301
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
302302
}
303303

304-
if (stringIsNotEmpty(style.getFontFamily())) {
304+
if (!stringIsEmpty(style.getFontFamily())) {
305305
font.setFontName(style.getFontFamily());
306306
} else {
307307
font.setFontName(DEFAULT_FONT_NAME);
308308
}
309309

310-
if (stringIsNotEmpty(style.getFontWeight())) {
310+
if (!stringIsEmpty(style.getFontWeight())) {
311311
font.setBold(style.getFontWeight().equals("bold"));
312312
}
313313

314-
if (stringIsNotEmpty(style.getFontStyle())) {
314+
if (!stringIsEmpty(style.getFontStyle())) {
315315
font.setItalic(style.getFontStyle().equals("italic"));
316316
}
317317

318-
if (stringIsNotEmpty(style.getAlignItems())) {
318+
if (!stringIsEmpty(style.getAlignItems())) {
319319
cellStyle.setAlignment(getHorizontalAlignment(style.getAlignItems().toUpperCase()));
320320
}
321321

322-
if (stringIsNotEmpty(style.getJustifyContent())) {
322+
if (!stringIsEmpty(style.getJustifyContent())) {
323323
cellStyle.setVerticalAlignment(getVerticalAlignment(style.getJustifyContent().toUpperCase()));
324324
}
325325

@@ -374,7 +374,18 @@ private XSSFColor getXSSFColorFromRGBA(String colorStr) {
374374
return new XSSFColor(new java.awt.Color(red, green, blue), new DefaultIndexedColorMap());
375375
}
376376

377-
protected boolean stringIsNotEmpty(String str) {
378-
return str != null && !str.isEmpty();
377+
protected boolean stringIsEmpty(String str) {
378+
return str == null || str.isEmpty();
379+
}
380+
381+
public boolean styleIsEmpty(Style style) {
382+
return stringIsEmpty(style.getBackgroundColor()) &&
383+
stringIsEmpty(style.getColor()) &&
384+
stringIsEmpty(style.getFontSize()) &&
385+
stringIsEmpty(style.getFontFamily()) &&
386+
stringIsEmpty(style.getFontWeight()) &&
387+
stringIsEmpty(style.getFontStyle()) &&
388+
stringIsEmpty(style.getAlignItems()) &&
389+
stringIsEmpty(style.getJustifyContent());
379390
}
380391
}

0 commit comments

Comments
 (0)