Skip to content

Commit 5878900

Browse files
committed
[KNOWAGE-9777] add column filters to export logic
1 parent 5190375 commit 5878900

7 files changed

Lines changed: 299 additions & 43 deletions

File tree

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

Lines changed: 246 additions & 19 deletions
Large diffs are not rendered by default.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public byte[] getDashboardBinaryData(JSONObject body, boolean isDashboardSingleW
207207
try (Workbook wb = new SXSSFWorkbook(windowSize)) {
208208

209209
int exportedSheets = 0;
210-
Map<String, Map<String, JSONArray>> selections = getSelections(body);
210+
Map<String, Map<String, Object>> selections = getSelections(body);
211211

212212
JSONArray driversFromBody = getDrivers(body);
213213
JSONObject drivers = transformDriversForDatastore(driversFromBody);
@@ -229,7 +229,7 @@ public byte[] getDashboardBinaryData(JSONObject body, boolean isDashboardSingleW
229229

230230
if (selectionsCfg.isPresent() && selectionsCfg.get().isActive() && Boolean.parseBoolean(selectionsCfg.get().getValueCheck()) && !selections.isEmpty()) {
231231
Sheet selectionsSheet = createUniqueSafeSheetForSelections(wb, "Active Selections");
232-
fillDashboardSelectionsSheetWithData(selections, selectionsSheet);
232+
fillDashboardSelectionsSheetWithData(getSelections(body), selectionsSheet);
233233
exportedSheets++;
234234
}
235235

@@ -272,7 +272,7 @@ public byte[] getPivotBinaryData(JSONObject body) {
272272
throw new SpagoBIRuntimeException("Unable to get template for dashboard");
273273
}
274274
try {
275-
Map<String, Map<String, JSONArray>> selections = getSelections(body);
275+
Map<String, Map<String, Object>> selections = getSelections(body);
276276

277277
JSONArray driversFromBody = getDrivers(body);
278278
JSONObject drivers = transformDriversForDatastore(driversFromBody);
@@ -290,7 +290,7 @@ public byte[] getPivotBinaryData(JSONObject body) {
290290

291291
if (selectionsCfg.isPresent() && selectionsCfg.get().isActive() && Boolean.parseBoolean(selectionsCfg.get().getValueCheck()) && !selections.isEmpty()) {
292292
Sheet selectionsSheet = createUniqueSafeSheetForSelections(wb, "Active Selections");
293-
fillDashboardSelectionsSheetWithData(selections, selectionsSheet);
293+
fillDashboardSelectionsSheetWithData(getSelections(body), selectionsSheet);
294294
}
295295

296296
Optional<Config> driversConfig = configsDao.loadConfigParametersByLabelIfExist(CONFIG_NAME_FOR_DRIVERS_SHEET_EXPORT);
@@ -334,7 +334,7 @@ private String getDocumentName(JSONObject template) {
334334
}
335335
}
336336

337-
private int exportDashboard(JSONArray widgetsArray, Workbook wb, String documentName, Map<String, Map<String, JSONArray>> selections, JSONObject drivers, JSONArray parameters) {
337+
private int exportDashboard(JSONArray widgetsArray, Workbook wb, String documentName, Map<String, Map<String, Object>> selections, JSONObject drivers, JSONArray parameters) {
338338
int exportedSheets = 0;
339339
for (int i = 0; i < widgetsArray.length(); i++) {
340340
try {
@@ -373,7 +373,7 @@ private void setDatasetDriversIfPresent(JSONObject body, JSONObject currWidget,
373373
}
374374
}
375375

376-
public int exportWidget(JSONObject body, Workbook wb, String documentName, Map<String, Map<String, JSONArray>> selections, JSONObject drivers, JSONArray parameters) {
376+
public int exportWidget(JSONObject body, Workbook wb, String documentName, Map<String, Map<String, Object>> selections, JSONObject drivers, JSONArray parameters) {
377377

378378
int exportedSheets;
379379
try {
@@ -392,7 +392,7 @@ public int exportWidget(JSONObject body, Workbook wb, String documentName, Map<S
392392
return exportedSheets;
393393
}
394394

395-
public Workbook exportPivotWidget(JSONObject body, String documentName, Map<String, Map<String, JSONArray>> selections, JSONObject drivers, JSONArray parameters) {
395+
public Workbook exportPivotWidget(JSONObject body, String documentName, Map<String, Map<String, Object>> selections, JSONObject drivers, JSONArray parameters) {
396396
try {
397397
JSONObject parametersToSend = transformParametersForDatastore(body, parameters);
398398
if (excelExportIsNotEnabled(body)) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class DashboardPivotExporter extends GenericDashboardWidgetExporter imple
2929

3030
public static Logger logger = Logger.getLogger(DashboardPivotExporter.class);
3131

32-
public DashboardPivotExporter(DashboardExcelExporter excelExporter, JSONObject widget, String documentName, Map<String, Map<String, JSONArray>> selections, JSONObject drivers, JSONObject parameters, String userUniqueIdentifier, String imageB64) {
32+
public DashboardPivotExporter(DashboardExcelExporter excelExporter, JSONObject widget, String documentName, Map<String, Map<String, Object>> selections, JSONObject drivers, JSONObject parameters, String userUniqueIdentifier, String imageB64) {
3333
super(excelExporter, null, widget, documentName, selections, drivers, parameters, userUniqueIdentifier, imageB64);
3434
}
3535

@@ -46,6 +46,9 @@ public Workbook exportPivot() {
4646
String imageB64 = OrganizationImageManager.getOrganizationB64ImageWide(TenantManager.getTenant().getName());
4747

4848
int totalNumberOfRows = dataStore.getInt("results");
49+
if (totalNumberOfRows == 0) {
50+
return null;
51+
}
4952
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
5053
XSSFSheet xssfSheet = xssfWorkbook.createSheet("Source_sheet");
5154

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class DashboardTableExporter extends GenericDashboardWidgetExporter implements IWidgetExporter {
1616
public static Logger logger = Logger.getLogger(DashboardTableExporter.class);
1717

18-
public DashboardTableExporter(DashboardExcelExporter excelExporter, Workbook wb, JSONObject widget, String documentName, Map<String, Map<String, JSONArray>> selections, JSONObject drivers, JSONObject parameters, String userUniqueIdentifier, String imageB64) {
18+
public DashboardTableExporter(DashboardExcelExporter excelExporter, Workbook wb, JSONObject widget, String documentName, Map<String, Map<String, Object>> selections, JSONObject drivers, JSONObject parameters, String userUniqueIdentifier, String imageB64) {
1919
super(excelExporter, wb, widget, documentName, selections, drivers, parameters, userUniqueIdentifier, imageB64);
2020
}
2121

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static IWidgetExporter getExporter(DashboardExcelExporter exporter,
1717
Workbook wb,
1818
JSONObject widget,
1919
String documentName,
20-
Map<String, Map<String, JSONArray>> selections,
20+
Map<String, Map<String, Object>> selections,
2121
JSONObject drivers,
2222
JSONObject parameters,
2323
String userUniqueIdentifier,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class GenericDashboardWidgetExporter extends DashboardExporter implements IWidge
3535
Workbook wb;
3636
JSONObject widget;
3737
String documentName;
38-
Map<String, Map<String, JSONArray>> selections;
38+
Map<String, Map<String, Object>> selections;
3939
JSONObject drivers;
4040
JSONObject parameters;
4141

4242
public GenericDashboardWidgetExporter(DashboardExcelExporter excelExporter,
4343
Workbook wb,
4444
JSONObject widget,
4545
String documentName,
46-
Map<String, Map<String, JSONArray>> selections,
46+
Map<String, Map<String, Object>> selections,
4747
JSONObject drivers,
4848
JSONObject parameters,
4949
String userUniqueIdentifier,

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

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public byte[] getBinaryData(JSONObject template) throws JSONException {
7272
throw new SpagoBIRuntimeException("Unable to get template for dashboard");
7373
}
7474

75-
Map<String, Map<String, JSONArray>> selections = getSelections(template);
75+
Map<String, Map<String, Object>> selections = getSelections(template);
7676

7777
JSONArray driversFromBody = getDrivers(template);
7878
JSONArray parametersFromBody = getParametersFromBody(template);
@@ -95,7 +95,7 @@ public byte[] getBinaryData(JSONObject template) throws JSONException {
9595
}
9696
}
9797

98-
private void exportTableWidget(PDDocument document, JSONObject widget, String widgetId, String executionUser, Map<String, Map<String, JSONArray>> selections, JSONObject drivers, JSONObject parameters, JSONArray driversFromBody, JSONArray parametersFromBody) {
98+
private void exportTableWidget(PDDocument document, JSONObject widget, String widgetId, String executionUser, Map<String, Map<String, Object>> selections, JSONObject drivers, JSONObject parameters, JSONArray driversFromBody, JSONArray parametersFromBody) {
9999
try {
100100
JSONObject settings = widget.optJSONObject("settings");
101101

@@ -182,7 +182,7 @@ private void exportTableWidget(PDDocument document, JSONObject widget, String wi
182182
table.draw();
183183

184184
if (offset >= totalNumberOfRows) {
185-
createSelectionsRows(table, font, selections);
185+
createSelectionsRows(table, font, getSelections(widget));
186186
createFiltersInformationRow(table, font, driversFromBody);
187187
}
188188

@@ -192,15 +192,15 @@ private void exportTableWidget(PDDocument document, JSONObject widget, String wi
192192
}
193193
}
194194

195-
private static int getReservedSelectionLines(Map<String, Map<String, JSONArray>> selections) {
195+
private static int getReservedSelectionLines(Map<String, Map<String, Object>> selections) {
196196
if (selections == null || selections.isEmpty()) {
197197
return 0;
198198
}
199199

200200
// Count the actual number of selections
201201
int selectionCount = 0;
202-
for (Map.Entry<String, Map<String, JSONArray>> datasetEntry : selections.entrySet()) {
203-
Map<String, JSONArray> selectionContent = datasetEntry.getValue();
202+
for (Map.Entry<String, Map<String, Object>> datasetEntry : selections.entrySet()) {
203+
Map<String, Object> selectionContent = datasetEntry.getValue();
204204
if (selectionContent != null && !selectionContent.isEmpty()) {
205205
selectionCount += selectionContent.size();
206206
}
@@ -601,7 +601,7 @@ private float getFooterStartYFromTable(BaseTable table, float leading) {
601601
return tableStartY - tableHeight - leading;
602602
}
603603

604-
private void createSelectionsRows(BaseTable table, PDFont font, Map<String, Map<String, JSONArray>> selections) {
604+
private void createSelectionsRows(BaseTable table, PDFont font, Map<String, Map<String, Object>> selections) {
605605
try {
606606
footerCurrentPage = table.getCurrentPage();
607607
footerCurrentY = getFooterStartYFromTable(table, 12f);
@@ -634,13 +634,14 @@ final class SelectionLine {
634634

635635
java.util.List<SelectionLine> lines = new java.util.ArrayList<>();
636636

637-
for (Map.Entry<String, Map<String, JSONArray>> datasetEntry : selections.entrySet()) {
638-
Map<String, JSONArray> selectionContent = datasetEntry.getValue();
637+
for (Map.Entry<String, Map<String, Object>> datasetEntry : selections.entrySet()) {
638+
Map<String, Object> selectionContent = datasetEntry.getValue();
639639
if (selectionContent == null || selectionContent.isEmpty()) {
640640
continue;
641641
}
642642

643-
for (Map.Entry<String, JSONArray> columnEntry : selectionContent.entrySet()) {
643+
for (Map.Entry<String, Object> columnEntry : selectionContent.entrySet()) {
644+
644645
String key = columnEntry.getKey();
645646
key = key.replace("('", "").replace("')", "");
646647

@@ -824,8 +825,8 @@ final class SelectionLine {
824825
}
825826
}
826827

827-
private static @NonNull StringBuilder getValues(Map.Entry<String, JSONArray> columnEntry) {
828-
JSONArray valuesArray = columnEntry.getValue();
828+
private static @NonNull StringBuilder getValues(Map.Entry<String, Object> columnEntry) {
829+
JSONArray valuesArray = getFilterValues(columnEntry.getValue());
829830

830831
StringBuilder values = new StringBuilder();
831832
if (valuesArray != null) {
@@ -838,6 +839,30 @@ final class SelectionLine {
838839
return values;
839840
}
840841

842+
private static JSONArray getFilterValues(Object selectionValue) {
843+
if (selectionValue instanceof JSONObject filterObject) {
844+
return filterObject.optJSONArray("filterVals");
845+
}
846+
if (selectionValue instanceof JSONArray valuesArray) {
847+
JSONArray flattenedValues = new JSONArray();
848+
for (int i = 0; i < valuesArray.length(); i++) {
849+
Object item = valuesArray.opt(i);
850+
if (item instanceof JSONObject embeddedFilter) {
851+
JSONArray embeddedFilterValues = embeddedFilter.optJSONArray("filterVals");
852+
if (embeddedFilterValues != null) {
853+
for (int j = 0; j < embeddedFilterValues.length(); j++) {
854+
flattenedValues.put(embeddedFilterValues.opt(j));
855+
}
856+
}
857+
} else {
858+
flattenedValues.put(item);
859+
}
860+
}
861+
return flattenedValues;
862+
}
863+
return null;
864+
}
865+
841866
/**
842867
* Wrap text so that each line fits within maxWidth using the provided font metrics.
843868
* Splits by whitespace; if a single token is wider than maxWidth it will be hard-split.
@@ -1298,4 +1323,5 @@ private String workaroundToRemoveCommonProblematicChars(String valueStr) {
12981323
return valueStr;
12991324
}
13001325

1326+
13011327
}

0 commit comments

Comments
 (0)