|
1 | 1 | package it.eng.knowage.engine.api.export.dashboard.excel.exporters; |
2 | 2 |
|
| 3 | +import it.eng.knowage.commons.multitenant.OrganizationImageManager; |
3 | 4 | import it.eng.knowage.engine.api.export.IWidgetExporter; |
4 | 5 | import it.eng.knowage.engine.api.export.dashboard.excel.DashboardExcelExporter; |
5 | 6 | import it.eng.spagobi.commons.SingletonConfig; |
| 7 | +import it.eng.spagobi.tenant.TenantManager; |
6 | 8 | import it.eng.spagobi.utilities.exceptions.SpagoBIRuntimeException; |
7 | 9 | import org.apache.log4j.Logger; |
8 | | -import org.apache.poi.ss.usermodel.Sheet; |
| 10 | +import org.apache.poi.ss.SpreadsheetVersion; |
| 11 | +import org.apache.poi.ss.usermodel.DataConsolidateFunction; |
9 | 12 | import org.apache.poi.ss.usermodel.Workbook; |
| 13 | +import org.apache.poi.ss.util.AreaReference; |
| 14 | +import org.apache.poi.ss.util.CellReference; |
| 15 | +import org.apache.poi.xssf.streaming.SXSSFSheet; |
| 16 | +import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
| 17 | +import org.apache.poi.xssf.usermodel.XSSFPivotTable; |
| 18 | +import org.apache.poi.xssf.usermodel.XSSFSheet; |
| 19 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
10 | 20 | import org.json.JSONArray; |
| 21 | +import org.json.JSONException; |
11 | 22 | import org.json.JSONObject; |
12 | 23 |
|
13 | 24 | import java.util.Map; |
14 | 25 |
|
| 26 | +import static org.apache.poi.ss.usermodel.DataConsolidateFunction.*; |
| 27 | + |
15 | 28 | public class DashboardPivotExporter extends GenericDashboardWidgetExporter implements IWidgetExporter { |
16 | 29 |
|
17 | 30 | public static Logger logger = Logger.getLogger(DashboardPivotExporter.class); |
18 | 31 |
|
19 | | - public DashboardPivotExporter(DashboardExcelExporter excelExporter, Workbook wb, JSONObject widget, String documentName, Map<String, Map<String, JSONArray>> selections, JSONObject drivers, JSONObject parameters, String userUniqueIdentifier) { |
20 | | - super(excelExporter, wb, widget, documentName, selections, drivers, parameters, userUniqueIdentifier); |
| 32 | + public DashboardPivotExporter(DashboardExcelExporter excelExporter, JSONObject widget, String documentName, Map<String, Map<String, JSONArray>> selections, JSONObject drivers, JSONObject parameters, String userUniqueIdentifier, String imageB64) { |
| 33 | + super(excelExporter, null, widget, documentName, selections, drivers, parameters, userUniqueIdentifier, imageB64); |
21 | 34 | } |
22 | 35 |
|
23 | | - @Override |
24 | | - public int export() { |
| 36 | + public Workbook exportPivot() { |
25 | 37 | String widgetId = widget.optString("id"); |
26 | 38 | try { |
27 | 39 | JSONObject settings = widget.getJSONObject("settings"); |
28 | | - String dashboardSheetName = documentName != null ? documentName : "Dashboard"; |
29 | 40 | String widgetName = getJsonObjectUtils().replacePlaceholderIfPresent(getJsonObjectUtils().getDashboardWidgetName(widget), drivers); |
30 | | - Sheet sheet = excelExporter.createUniqueSafeSheet(wb, widgetName, dashboardSheetName); |
31 | 41 |
|
32 | 42 | int offset = 0; |
33 | 43 | int fetchSize = Integer.parseInt(SingletonConfig.getInstance().getConfigValue("SPAGOBI.API.DATASET.MAX_ROWS_NUMBER")); |
34 | 44 | JSONObject dataStore = getDataStoreForDashboardWidget(widget, offset, fetchSize, selections, drivers, parameters); |
35 | 45 | if (dataStore != null) { |
| 46 | + String imageB64 = OrganizationImageManager.getOrganizationB64ImageWide(TenantManager.getTenant().getName()); |
| 47 | + |
36 | 48 | int totalNumberOfRows = dataStore.getInt("results"); |
| 49 | + XSSFWorkbook xssfWorkbook = new XSSFWorkbook(); |
| 50 | + XSSFSheet xssfSheet = xssfWorkbook.createSheet("Source_sheet"); |
| 51 | + |
| 52 | + excelExporter.fillTableSheetWithData(dataStore, xssfWorkbook, xssfSheet, widgetName, offset, settings); |
| 53 | + |
| 54 | + XSSFSheet pivotSheet = xssfWorkbook.createSheet("Pivot_sheet"); |
| 55 | + |
| 56 | + int startRow = 0; |
| 57 | + float rowHeight = 35; // in points |
| 58 | + int rowspan = 2; |
| 59 | + int startCol = 0; |
| 60 | + int colWidth = 25; |
| 61 | + int colspan = 2; |
| 62 | + int namespan = 10; |
| 63 | + int dataspan = 10; |
| 64 | + |
| 65 | + createBrandedHeaderSheet( |
| 66 | + pivotSheet, |
| 67 | + imageB64, |
| 68 | + startRow, |
| 69 | + rowHeight, |
| 70 | + rowspan, |
| 71 | + startCol, |
| 72 | + colWidth, |
| 73 | + colspan, |
| 74 | + namespan, |
| 75 | + dataspan, |
| 76 | + "Dashboard", |
| 77 | + widgetName); |
| 78 | + |
| 79 | + |
| 80 | + int xssfSheetLastRowNum = xssfSheet.getLastRowNum(); |
| 81 | + int sourceSheetLastColumn = xssfSheet.getRow(xssfSheetLastRowNum).getLastCellNum(); |
| 82 | + String lastColLetter = CellReference.convertNumToColString(sourceSheetLastColumn - 1); |
| 83 | + |
| 84 | + boolean isImagePresent = imageB64 != null; |
| 85 | + XSSFPivotTable pivotTable = pivotSheet.createPivotTable( |
| 86 | + new AreaReference(isImagePresent? new CellReference("Source_sheet!A4") : new CellReference("Source_sheet!A2"), |
| 87 | + new CellReference("Source_sheet!" + lastColLetter + (totalNumberOfRows + (isImagePresent ? 3 : 1))), //make the reference big enough for later data |
| 88 | + SpreadsheetVersion.EXCEL2007), |
| 89 | + new CellReference("A5")); |
| 90 | + |
| 91 | + formatPivot(pivotTable); |
| 92 | + |
| 93 | + SXSSFWorkbook swb = new SXSSFWorkbook(xssfWorkbook); |
| 94 | + SXSSFSheet ssheet = swb.getSheet("Source_sheet"); |
| 95 | + |
37 | 96 | while (offset < totalNumberOfRows) { |
38 | | - excelExporter.fillTableSheetWithData(dataStore, wb, sheet, widgetName, offset, settings); |
39 | 97 | offset += fetchSize; |
40 | 98 | dataStore = getDataStoreForDashboardWidget(widget, offset, fetchSize, selections, drivers, parameters); |
| 99 | + excelExporter.fillTableSheetWithData(dataStore, swb, ssheet, widgetName, offset, settings); |
41 | 100 | } |
42 | | - excelExporter.createPivotTable(wb, sheet, widget, widgetName); |
43 | | - return 1; |
| 101 | + |
| 102 | + return swb; |
44 | 103 | } |
45 | 104 | } catch (Exception e) { |
46 | 105 | throw new SpagoBIRuntimeException("Unable to export table widget: " + widgetId, e); |
47 | 106 | } |
48 | | - return 0; |
| 107 | + return null; |
49 | 108 | } |
50 | 109 |
|
| 110 | + private void formatPivot(XSSFPivotTable pivotTable) { |
| 111 | + try { |
| 112 | + JSONObject fields = widget.getJSONObject("fields"); |
| 113 | + JSONArray columns = fields.getJSONArray("columns"); |
| 114 | + JSONArray rows = fields.getJSONArray("rows"); |
| 115 | + JSONArray data = fields.getJSONArray("data"); |
| 116 | + |
| 117 | + int counter = 0; |
51 | 118 |
|
52 | | -} |
| 119 | + for (int i = 0; i < columns.length(); ++i) { |
| 120 | + pivotTable.addRowLabel(counter); |
| 121 | + counter++; |
| 122 | + } |
| 123 | + |
| 124 | + for (int i = 0; i < rows.length(); ++i) { |
| 125 | + pivotTable.addRowLabel(counter); |
| 126 | + counter++; |
| 127 | + } |
| 128 | + |
| 129 | + for (int i = 0; i < data.length(); ++i) { |
| 130 | + JSONObject datum = data.getJSONObject(i); |
| 131 | + DataConsolidateFunction function = getAggregationFunction(datum.getString("aggregation").toUpperCase()); |
| 132 | + pivotTable.addColumnLabel(function, counter, datum.getString("alias")); |
| 133 | + counter++; |
| 134 | + } |
| 135 | + |
| 136 | + } catch (JSONException e) { |
| 137 | + logger.error("Error while creating pivot table", e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private DataConsolidateFunction getAggregationFunction(String aggregation) { |
| 142 | + |
| 143 | + return switch (aggregation) { |
| 144 | + case "SUM" -> SUM; |
| 145 | + case "COUNT" -> COUNT; |
| 146 | + case "AVG" -> AVERAGE; |
| 147 | + case "MAX" -> MAX; |
| 148 | + case "MIN" -> MIN; |
| 149 | + default -> null; |
| 150 | + }; |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + } |
0 commit comments