Skip to content

Commit aa39c92

Browse files
committed
fix dashboard export
1 parent 57410f0 commit aa39c92

3 files changed

Lines changed: 112 additions & 50 deletions

File tree

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ protected int doSummaryRowsLogic(JSONObject settings, int numberOfSummaryRows, L
489489
numberOfSummaryRows = list.length();
490490

491491
for (int i = 0; i < numberOfSummaryRows; i++) {
492-
summaryRowsLabels.add(list.getJSONObject(i).getString("label"));
492+
summaryRowsLabels.add(list.getJSONObject(i).optString("label", ""));
493493
}
494494

495495
}
@@ -1481,9 +1481,46 @@ public void fillDashboardDriversSheetWithData(JSONArray drivers, Sheet sheet) {
14811481
LOGGER.error("Cannot fill drivers sheet", e);
14821482
throw new SpagoBIRuntimeException("Cannot fill drivers sheet", e);
14831483
}
1484+
}
1485+
1486+
protected JSONObject getVisualizationTypeByColumn(JSONObject settings, JSONObject column) {
1487+
try {
1488+
JSONObject visualization = getJsonObjectUtils().getVisualizationFromSettings(settings);
1489+
1490+
if (visualization == null || !visualization.has("visualizationTypes"))
1491+
return null;
14841492

1493+
JSONObject visualizationTypes = visualization.getJSONObject("visualizationTypes");
1494+
1495+
JSONArray types = visualizationTypes.getJSONArray("types");
1496+
1497+
JSONObject allVisualizationType = null;
1498+
for (int i = 0; i < types.length(); i++) {
1499+
JSONObject type = types.getJSONObject(i);
1500+
JSONArray target;
1501+
try {
1502+
target = type.getJSONArray("target");
1503+
} catch (JSONException e) {
1504+
target = new JSONArray();
1505+
target.put(type.getString("target"));
1506+
}
1507+
1508+
1509+
if (target.toString().contains("all")) {
1510+
allVisualizationType = type;
1511+
}
1512+
1513+
if (target.toString().contains(column.getString("id"))) {
1514+
return type;
1515+
}
1516+
}
1517+
return allVisualizationType;
1518+
} catch (Exception ignored) {
1519+
return null;
1520+
}
14851521
}
14861522

14871523

14881524

1525+
14891526
}

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

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -450,38 +450,75 @@ private JSONArray getDashboardWidgetsJson(String templateString) {
450450
}
451451
}
452452

453-
private void setFormattedCellValue(Workbook wb, int precision, String type, Cell cell, String stringifiedValue) {
453+
private void setFormattedCellValue(Workbook wb, JSONObject visualizationType, String type, Cell cell, String stringifiedValue) throws JSONException {
454454
CreationHelper creationHelper = wb.getCreationHelper();
455455

456456
DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, getLocale());
457457

458458
switch (type) {
459459
case "int":
460460
if (!stringifiedValue.trim().isEmpty()) {
461-
cell.getCellStyle().setDataFormat(getFormat(precision, creationHelper));
462-
cell.setCellValue(Integer.parseInt(stringifiedValue));
461+
cell.getCellStyle().setDataFormat(getFormat(visualizationType, creationHelper));
462+
if (visualizationType.has("prefix") || visualizationType.has("suffix")) {
463+
stringifiedValue = visualizationType.optString("prefix", "") + stringifiedValue;
464+
if (visualizationType.has("precision")) {
465+
int precision = visualizationType.getInt("precision");
466+
if (precision > 0) {
467+
stringifiedValue += "." + "0".repeat(precision);
468+
}
469+
stringifiedValue += visualizationType.optString("suffix", "");
470+
}
471+
cell.setCellValue(stringifiedValue);
472+
} else {
473+
cell.setCellValue(Integer.parseInt(stringifiedValue));
474+
}
463475
}
464476
break;
465477
case "float":
466478
if (!stringifiedValue.trim().isEmpty()) {
467-
cell.getCellStyle().setDataFormat(getFormat(precision, creationHelper));
468-
cell.setCellValue(Double.parseDouble(stringifiedValue));
479+
cell.getCellStyle().setDataFormat(getFormat(visualizationType, creationHelper));
480+
if (visualizationType.has("prefix") || visualizationType.has("suffix")) {
481+
if (visualizationType.has("precision")) {
482+
int precision = visualizationType.getInt("precision");
483+
double doubleValue = Double.parseDouble(stringifiedValue);
484+
double factor = Math.pow(10, precision);
485+
doubleValue = Math.round(doubleValue * factor) / factor;
486+
stringifiedValue = String.format(String.format("%%.%df", precision), doubleValue);
487+
}
488+
stringifiedValue = visualizationType.optString("prefix", "") + stringifiedValue;
489+
stringifiedValue = stringifiedValue + visualizationType.optString("suffix", "");
490+
cell.setCellValue(stringifiedValue);
491+
} else {
492+
cell.setCellValue(Double.parseDouble(stringifiedValue));
493+
}
469494
}
470495
break;
471496
case "date":
472497
try {
473498
if (!stringifiedValue.trim().isEmpty()) {
474-
Date date = dateFormat.parse(stringifiedValue);
475-
cell.setCellValue(date);
499+
cell.getCellStyle().setDataFormat(getFormat(visualizationType, creationHelper));
500+
if (visualizationType.has("prefix") || visualizationType.has("suffix")) {
501+
stringifiedValue = visualizationType.optString("prefix", "") + stringifiedValue;
502+
stringifiedValue = stringifiedValue + visualizationType.optString("suffix", "");
503+
cell.setCellValue(stringifiedValue);
504+
} else {
505+
Date date = dateFormat.parse(stringifiedValue);
506+
cell.setCellValue(date);
507+
}
476508
}
477509
} catch (Exception e) {
478510
LOGGER.debug("Date will be exported as string due to error: ", e);
479511
cell.setCellValue(stringifiedValue);
480512
}
513+
break;
481514
case "string":
482515
if (!stringifiedValue.trim().isEmpty()) {
516+
if (visualizationType.has("prefix") || visualizationType.has("suffix")) {
517+
stringifiedValue = visualizationType.optString("prefix", "") + stringifiedValue;
518+
stringifiedValue = stringifiedValue + visualizationType.optString("suffix", "");
519+
}
483520
try {
484-
cell.getCellStyle().setDataFormat(getFormat(precision, creationHelper));
521+
cell.getCellStyle().setDataFormat(getFormat(visualizationType, creationHelper));
485522
cell.setCellValue(Integer.parseInt(stringifiedValue));
486523
} catch (Exception e) {
487524
cell.setCellValue(stringifiedValue);
@@ -494,10 +531,12 @@ private void setFormattedCellValue(Workbook wb, int precision, String type, Cell
494531
}
495532
}
496533

497-
private short getFormat(int precision, CreationHelper helper) {
534+
private short getFormat(JSONObject visualizationType, CreationHelper helper) throws JSONException {
498535
String format = "0";
499-
if (precision != -1) {
500-
format = getNumberFormatByPrecision(precision, format);
536+
if (visualizationType != null) {
537+
if (visualizationType.has("precision")) {
538+
format = getNumberFormatByPrecision(visualizationType.getInt("precision"), format);
539+
}
501540
return helper.createDataFormat().getFormat(format);
502541
}
503542
return helper.createDataFormat().getFormat(format);
@@ -737,18 +776,18 @@ private CellStyle buildCols(Workbook wb, Sheet sheet, JSONObject settings, JSONA
737776
}
738777
cell.setCellStyle(cellStyle);
739778
}
740-
int precision = getPrecisionByColumn(settings, column);
779+
JSONObject visualizationType = getVisualizationTypeByColumn(settings, column);
741780

742781
if (r < rows.length() - numberOfSummaryRows) {
743-
setFormattedCellValue(wb, precision, type, cell, stringifiedValue);
782+
setFormattedCellValue(wb, visualizationType, type, cell, stringifiedValue);
744783
} else {
745784
if (isSummaryColumnVisible(getDashboardHiddenColumnsList(settings, "hideFromSummary"), column)) {
746785
String label = "";
747786
if (colIndex.equals("column_1")) {
748787
label = summaryRowsLabels.get(r - (rows.length() - numberOfSummaryRows)).concat(" ");
749788
}
750789
cell.setCellStyle(cellStyle);
751-
setFormattedCellValue(wb, precision, type, cell, label.concat(stringifiedValue));
790+
setFormattedCellValue(wb, visualizationType, type, cell, label.concat(stringifiedValue));
752791
}
753792
}
754793
}
@@ -800,41 +839,6 @@ private Row createDashboardHeaderColumnNames(Sheet sheet, Map<String, String> gr
800839
}
801840
}
802841

803-
protected int getPrecisionByColumn(JSONObject settings, JSONObject column) {
804-
try {
805-
JSONObject visualization = getJsonObjectUtils().getVisualizationFromSettings(settings);
806-
807-
if (visualization == null || !visualization.has("visualizationTypes"))
808-
return -1;
809-
810-
JSONObject visualizationTypes = visualization.getJSONObject("visualizationTypes");
811-
812-
JSONArray types = visualizationTypes.getJSONArray("types");
813-
814-
for (int i = 0; i < types.length(); i++) {
815-
JSONObject type = types.getJSONObject(i);
816-
JSONArray target;
817-
try {
818-
target = type.getJSONArray("target");
819-
} catch (JSONException e) {
820-
target = new JSONArray();
821-
target.put(type.getString("target"));
822-
}
823-
824-
if (type.has("precision")) {
825-
if (target.toString().contains(column.getString("id")) || target.toString().contains("all")) {
826-
return type.getInt("precision");
827-
} else {
828-
return -1;
829-
}
830-
}
831-
}
832-
} catch (Exception ignored) {
833-
return -1;
834-
}
835-
return -1;
836-
}
837-
838842
public CellStyle buildPoiCellStyle(Style style, XSSFFont font, Workbook wb) {
839843
CellStyle cellStyle = wb.createCellStyle();
840844

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@ private void buildColumns(JSONObject settings, JSONArray rows, JSONArray columns
564564
}
565565
valueStr = workaroundToRemoveCommonProblematicChars(valueStr);
566566

567+
JSONObject visualizationType = getVisualizationTypeByColumn(settings, column);
568+
valueStr = applyVisualizationTypeToValueStr(visualizationType, valueStr);
569+
567570
Cell<PDPage> cell = row.createCell(columnPercentWidths[c], valueStr,
568571
HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
569572

@@ -603,6 +606,24 @@ private void buildColumns(JSONObject settings, JSONArray rows, JSONArray columns
603606
}
604607
}
605608

609+
private String applyVisualizationTypeToValueStr(JSONObject visualizationType, String valueStr) throws JSONException {
610+
611+
if (visualizationType.has("precision")) {
612+
int precision = visualizationType.getInt("precision");
613+
try {
614+
double numericValue = Double.parseDouble(valueStr);
615+
String formatString = "%." + precision + "f";
616+
valueStr = String.format(formatString, numericValue);
617+
} catch (NumberFormatException e) {
618+
logger.warn("Cannot apply precision to non-numeric value: " + valueStr, e);
619+
}
620+
}
621+
622+
String prefix = visualizationType.optString("prefix", "");
623+
String suffix = visualizationType.optString("suffix", "");
624+
return prefix + valueStr + suffix;
625+
}
626+
606627
private void applyWholeRowStyle(int c, List<Boolean> styleCanBeOverriddenByWholeRowStyle, Row<PDPage> row, Style cellStyle) {
607628
for (int previousCell = c - 1; previousCell >= 0; previousCell--) {
608629
if (styleCanBeOverriddenByWholeRowStyle.get(previousCell).equals(Boolean.TRUE)) {

0 commit comments

Comments
 (0)