Skip to content

Commit c8e2c26

Browse files
committed
[KNOWAGE-9305] add support for cockpit selections and document metadata in PDF exports
1 parent 4bbbdc9 commit c8e2c26

2 files changed

Lines changed: 94 additions & 29 deletions

File tree

knowagecockpitengine/src/main/java/it/eng/knowage/engine/cockpit/api/export/CockpitExportResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public void exportPdf(@Context HttpServletRequest req) {
113113
Integer documentId = body.optInt(DOCUMENT_ID);
114114
String template = getIOManager().getTemplateAsString();
115115
body.put("template", template);
116-
byte[] data = pdfExporter.getBinaryData(documentId, documentLabel, template);
116+
JSONObject selections = body.optJSONObject("COCKPIT_SELECTIONS").optJSONObject("selections");
117+
byte[] data = pdfExporter.getBinaryData(documentId, documentLabel, template, selections);
117118

118119
response.setContentType(MediaType.APPLICATION_OCTET_STREAM);
119120
response.setHeader("Content-length", Integer.toString(data.length));

knowagecockpitengine/src/main/java/it/eng/knowage/engine/cockpit/api/export/pdf/PdfExporter.java

Lines changed: 92 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import java.net.URL;
4949
import java.text.DateFormat;
5050
import java.text.SimpleDateFormat;
51+
import java.time.LocalDateTime;
52+
import java.time.format.DateTimeFormatter;
5153
import java.util.*;
5254
import java.util.List;
5355

@@ -66,30 +68,33 @@ public PdfExporter(String userUniqueIdentifier, JSONObject body) {
6668
super(userUniqueIdentifier, body);
6769
}
6870

69-
public byte[] getBinaryData(Integer documentId, String documentLabel, String templateString) throws JSONException {
70-
if (templateString == null) {
71+
public byte[] getBinaryData(Integer documentId, String documentLabel, String templateString, JSONObject selections) throws JSONException {
72+
7173
ObjTemplate template = null;
7274
String message = "Unable to get template for document with id [" + documentId + "] and label ["
7375
+ documentLabel + "]";
76+
String creationUser;
7477
try {
75-
if (documentId != null && documentId.intValue() != 0)
78+
if (documentId != null && documentId != 0)
7679
template = DAOFactory.getObjTemplateDAO().getBIObjectActiveTemplate(documentId);
7780
else if (documentLabel != null && !documentLabel.isEmpty())
7881
template = DAOFactory.getObjTemplateDAO().getBIObjectActiveTemplateByLabel(documentLabel);
7982

8083
if (template == null)
8184
throw new SpagoBIRuntimeException(message);
8285

83-
templateString = new String(template.getContent());
86+
if (templateString == null) {
87+
templateString = new String(template.getContent());
88+
}
89+
creationUser = template.getCreationUser();
8490
} catch (EMFAbstractError e) {
8591
throw new SpagoBIRuntimeException(message, e);
8692
}
87-
}
8893

8994
try (PDDocument document = new PDDocument(MemoryUsageSetting.setupTempFileOnly())) {
9095
long widgetId = body.getLong("widget");
9196

92-
exportTableWidget(document, templateString, widgetId);
97+
exportTableWidget(document, templateString, widgetId, creationUser, selections);
9398

9499
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
95100
document.save(byteArrayOutputStream);
@@ -102,14 +107,13 @@ else if (documentLabel != null && !documentLabel.isEmpty())
102107

103108
}
104109

105-
private void exportTableWidget(PDDocument document, String templateString, long widgetId) {
110+
private void exportTableWidget(PDDocument document, String templateString, long widgetId, String creationUser, JSONObject selections) {
106111
try {
107112
JSONObject template = new JSONObject(templateString);
108113
JSONObject widget = getWidgetById(template, widgetId);
109114
JSONObject settings = widget.optJSONObject("settings");
110115
JSONObject style = widget.optJSONObject("style");
111116

112-
113117
JSONObject dataStore;
114118
int totalNumberOfRows = 0;
115119
int offset = 0;
@@ -163,7 +167,7 @@ private void exportTableWidget(PDDocument document, String templateString, long
163167
PDFont font = PDType0Font.load(table.document, pdfFontFile);
164168

165169
addDataToTable(table, settings, columnsOrdered, pdfHiddenColumns, columnDateFormats, columnStyles,
166-
rows, font, style, widgetData, widgetContent);
170+
rows, font, style, widgetData, widgetContent, creationUser, selections);
167171

168172
offset += fetchSize;
169173

@@ -179,10 +183,10 @@ private void exportTableWidget(PDDocument document, String templateString, long
179183
}
180184

181185
private void addDataToTable(BaseTable table, JSONObject settings, JSONArray columnsOrdered,
182-
List<Integer> pdfHiddenColumns, String[] columnDateFormats, JSONObject[] columnStyles, JSONArray rows, PDFont font, JSONObject style, JSONObject widgetData, JSONObject widgetContent)
186+
List<Integer> pdfHiddenColumns, String[] columnDateFormats, JSONObject[] columnStyles, JSONArray rows, PDFont font, JSONObject style, JSONObject widgetData, JSONObject widgetContent, String creationUser, JSONObject selections)
183187
throws JSONException, IOException, URISyntaxException {
184188

185-
addHeaderToTable(table, style, widgetData, widgetContent, columnsOrdered, pdfHiddenColumns, font);
189+
addHeaderToTable(table, style, widgetData, widgetContent, columnsOrdered, pdfHiddenColumns, font, creationUser, selections);
186190

187191
// Check if summary row is enabled
188192
boolean summaryRowEnabled = false;
@@ -292,23 +296,15 @@ private void addDataToTable(BaseTable table, JSONObject settings, JSONArray colu
292296
}
293297

294298
private void addHeaderToTable(BaseTable table, JSONObject style, JSONObject widgetData, JSONObject widgetContent,
295-
JSONArray columnsOrdered, List<Integer> pdfHiddenColumns, PDFont font) throws JSONException, IOException, URISyntaxException {
296-
// HashMap<String, String> arrayHeader = new HashMap<String, String>();
297-
// for (int i = 0; i < widgetContent.getJSONArray("columnSelectedOfDataset").length(); i++) {
298-
// JSONObject column = widgetContent.getJSONArray("columnSelectedOfDataset").getJSONObject(i);
299-
// String key;
300-
// if (column.optBoolean("isCalculated") && !column.has("name")) {
301-
// key = column.getString("alias");
302-
// } else {
303-
// key = column.getString("name");
304-
// }
305-
// arrayHeader.put(key, column.getString("aliasToShow"));
306-
// }
307-
308-
JSONArray groupsFromWidgetContent = getGroupsFromWidgetContent(widgetData);
299+
JSONArray columnsOrdered, List<Integer> pdfHiddenColumns, PDFont font, String creationUser, JSONObject selections) throws JSONException {
300+
JSONArray groupsFromWidgetContent = getGroupsFromWidgetContent(widgetData);
309301
Map<String, String> groupsAndColumnsMap = getGroupAndColumnsMap(widgetContent, groupsFromWidgetContent);
310302

311-
if (!groupsAndColumnsMap.isEmpty()) {
303+
createDocumentInformationRow(table, font, creationUser);
304+
305+
createSelectionsRows(table, font, selections);
306+
307+
if (!groupsAndColumnsMap.isEmpty()) {
312308
Row<PDPage> groupHeaderRow = table.createRow(15f);
313309
for (int i = 0; i < columnsOrdered.length(); i++) {
314310
JSONObject column = columnsOrdered.getJSONObject(i);
@@ -372,8 +368,76 @@ private void addHeaderToTable(BaseTable table, JSONObject style, JSONObject widg
372368

373369
table.addHeaderRow(headerRow);
374370
}
375-
376-
private int getAdjacentEqualNamesAmount(Map<String, String> groupsAndColumnsMap, JSONArray columnsOrdered, int matchStartIndex, String groupNameToMatch) {
371+
372+
private static void createSelectionsRows(BaseTable table, PDFont font, JSONObject selections) {
373+
try {
374+
if (selections != null && selections.length() > 0) {
375+
selections.keys().forEachRemaining(key -> {
376+
try {
377+
JSONObject selectionContent = selections.getJSONObject(key);
378+
selectionContent.keys().forEachRemaining(subKey -> {
379+
Row<PDPage> selectionRow = table.createRow(12f);
380+
Cell<PDPage> selectionDatasetNameCell = selectionRow.createCell(50, "Selections Dataset: " + key,
381+
HorizontalAlignment.get("right"), VerticalAlignment.get("middle"));
382+
selectionDatasetNameCell.setFont(font);
383+
selectionDatasetNameCell.setFillColor(Color.LIGHT_GRAY);
384+
selectionDatasetNameCell.setTextColor(Color.BLACK);
385+
selectionDatasetNameCell.setFontSize(10);
386+
StringBuilder stringBuilder = new StringBuilder();
387+
try {
388+
for (int i = 0; i < selectionContent.getJSONArray(subKey).length(); i++) {
389+
if (i > 0) {
390+
stringBuilder.append(", ");
391+
}
392+
try {
393+
String selectionValue = selectionContent.getJSONArray(subKey).toString().replace("('", "").replace("')", "");
394+
stringBuilder.append(selectionValue);
395+
} catch (JSONException e) {
396+
throw new RuntimeException(e);
397+
}
398+
}
399+
} catch (JSONException e) {
400+
try {
401+
String selectionValue = selectionContent.getJSONObject(subKey).getJSONArray("filterVals").toString().replace("('", "").replace("')", "");
402+
stringBuilder.append(selectionValue);
403+
} catch (JSONException ex) {
404+
throw new RuntimeException(ex);
405+
}
406+
}
407+
Cell<PDPage> subSelectionValueCell = selectionRow.createCell(50, "COLUMN ".concat(subKey).concat(": ").concat(stringBuilder.toString()), HorizontalAlignment.get("left"), VerticalAlignment.get("middle"));
408+
subSelectionValueCell.setFont(font);
409+
subSelectionValueCell.setFillColor(Color.LIGHT_GRAY);
410+
subSelectionValueCell.setTextColor(Color.BLACK);
411+
subSelectionValueCell.setFontSize(10);
412+
});
413+
} catch (JSONException e) {
414+
throw new RuntimeException(e);
415+
}
416+
});
417+
}
418+
} catch (Exception e) {
419+
throw new SpagoBIRuntimeException("Couldn't add selection information to the table", e);
420+
}
421+
}
422+
423+
private static void createDocumentInformationRow(BaseTable table, PDFont font, String creationUser) {
424+
Row<PDPage> documentInformationRow = table.createRow(12f);
425+
String executionDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
426+
Cell<PDPage> executionDateCell = documentInformationRow.createCell(50, "Execution Date: " + executionDate,
427+
HorizontalAlignment.get("center"), VerticalAlignment.get("middle"));
428+
executionDateCell.setFont(font);
429+
executionDateCell.setFillColor(Color.LIGHT_GRAY);
430+
executionDateCell.setTextColor(Color.BLACK);
431+
executionDateCell.setFontSize(10);
432+
Cell<PDPage> creationUserCell = documentInformationRow.createCell(50, "Creation User: " + creationUser,
433+
HorizontalAlignment.get("center"), VerticalAlignment.get("middle"));
434+
creationUserCell.setFont(font);
435+
creationUserCell.setFillColor(Color.LIGHT_GRAY);
436+
creationUserCell.setTextColor(Color.BLACK);
437+
creationUserCell.setFontSize(10);
438+
}
439+
440+
private int getAdjacentEqualNamesAmount(Map<String, String> groupsAndColumnsMap, JSONArray columnsOrdered, int matchStartIndex, String groupNameToMatch) {
377441
try {
378442
int adjacents = 0;
379443
for (int i = matchStartIndex; i < columnsOrdered.length(); i++) {

0 commit comments

Comments
 (0)