Skip to content

Commit a6a436d

Browse files
committed
[ESMO] [KNOWAGE-9534] implement pdf export requirements from ESMO
1 parent e0978a6 commit a6a436d

5 files changed

Lines changed: 576 additions & 156 deletions

File tree

knowage-core/src/main/java/it/eng/knowage/dashboardexport/DashboardExportResource.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import javax.ws.rs.core.Context;
1919
import javax.ws.rs.core.MediaType;
2020

21+
import java.util.Locale;
22+
2123
import static java.nio.charset.StandardCharsets.UTF_8;
2224

2325
@Path("/1.0/dashboardExport")
@@ -111,11 +113,12 @@ private static String getWidgetName(String widgetName, JSONObject body) throws J
111113
public void downloadPdf(@Context HttpServletRequest req) {
112114
logger.debug("IN");
113115
response.setCharacterEncoding(UTF_8.name());
116+
Locale locale = req.getLocale();
114117
try {
115118
JSONObject body = RestUtilities.readBodyAsJSONObject(req);
116119
String token = request.getHeader(TOKEN_HEADER);
117120
String userId = token.substring(7);
118-
DashboardPdfExporter dashboardPdfExporter = new DashboardPdfExporter(userId, body);
121+
DashboardPdfExporter dashboardPdfExporter = new DashboardPdfExporter(userId, locale);
119122
String mimeType = dashboardPdfExporter.getMimeType();
120123

121124
if (!MimeUtils.isValidMimeType(mimeType))
@@ -126,6 +129,26 @@ public void downloadPdf(@Context HttpServletRequest req) {
126129
data = dashboardPdfExporter.getBinaryData(body);
127130
String widgetName = body.getJSONObject("settings").getJSONObject("style").getJSONObject("title")
128131
.optString("text");
132+
if (widgetName != null && widgetName.startsWith("$P{")) {
133+
// Extract the value between {} - e.g., "$P{country}" -> "country"
134+
int startIndex = widgetName.indexOf('{') + 1;
135+
int endIndex = widgetName.indexOf('}');
136+
String placeholderToReplace = (startIndex > 0 && endIndex > startIndex)
137+
? widgetName.substring(startIndex, endIndex)
138+
: "";
139+
String remainingPart = widgetName.substring(endIndex + 1);
140+
try {
141+
for (int i = 0; i < body.optJSONArray("drivers").length(); i++) {
142+
JSONObject driver = body.optJSONArray("drivers").getJSONObject(i);
143+
if (driver.getString("urlName").equals(placeholderToReplace)) {
144+
widgetName= driver.getString("value") + remainingPart;
145+
break;
146+
}
147+
}
148+
} catch (JSONException e) {
149+
throw new RuntimeException(e);
150+
}
151+
}
129152
response.setHeader("Content-Disposition", "attachment; fileName=" + widgetName + "." + "pdf");
130153
response.setContentType(MediaType.APPLICATION_OCTET_STREAM);
131154
response.setHeader("Content-length", Integer.toString(data.length));

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public class DashboardExporter {
3535

3636
private final JSONObjectUtils jsonObjectUtils;
37-
private final String userUniqueIdentifier;
37+
protected final String userUniqueIdentifier;
3838
private String imageB64;
3939
protected Map<String, String> i18nMessages;
4040
protected Locale locale;
@@ -196,13 +196,18 @@ protected Map<String, Map<String, JSONArray>> getSelections(JSONObject body) {
196196
}
197197
return selectionsToReturn;
198198
} catch (JSONException e) {
199-
return Collections.emptyMap();
199+
throw new SpagoBIRuntimeException("Cannot get selections from body", e);
200200
}
201201
}
202202

203203
private void loopOverSelectionValues(JSONObject selection, Map<String, Map<String, JSONArray>> selectionsToReturn) throws JSONException {
204204
for (int j = 0; j < selection.getJSONArray("value").length(); j++) {
205-
String valueToInsert = "('" + selection.getJSONArray("value").getString(j) + "')";
205+
String valueToInsert;
206+
try {
207+
valueToInsert = "('" + selection.getJSONArray("value").getString(j) + "')";
208+
} catch (JSONException e) {
209+
valueToInsert = "('" + selection.getJSONArray("value").getInt(j) + "')";
210+
}
206211
selectionsToReturn.get(selection.getString("datasetLabel")).get(selection.getString("columnName")).put(valueToInsert);
207212
}
208213
}
@@ -286,7 +291,20 @@ public JSONObject transformParametersForDatastore(JSONObject body, JSONArray par
286291
for (int i = 0; i < parameters.length(); i++) {
287292
JSONObject parameter = parameters.getJSONObject(i);
288293
if (parameter.getInt("dataset") == body.getInt("dataset")) {
289-
parametersToSend.put(parameter.getString("name"), parameter.get("value"));
294+
if (parameter.get("value") != null && !parameter.getString("value").equals("null")) {
295+
parametersToSend.put(parameter.getString("name"), parameter.get("value"));
296+
} else {
297+
JSONArray drivers = body.optJSONArray("drivers") != null ? body.getJSONArray("drivers") : null;
298+
if (drivers != null) {
299+
for (int j = 0; j < drivers.length(); j++) {
300+
JSONObject driver = drivers.getJSONObject(j);
301+
if (driver.getString("urlName").equals(parameter.getString("name"))) {
302+
parametersToSend.put(parameter.getString("name"), driver.get("value"));
303+
break;
304+
}
305+
}
306+
}
307+
}
290308
}
291309
}
292310
}
@@ -815,10 +833,6 @@ public JSONObject getDataStoreForDashboardWidget(JSONObject widget, int offset,
815833
buildLikeSelections(dashboardSelections, widget);
816834
}
817835

818-
if (widget.getString("type").equalsIgnoreCase("static-pivot-table")) {
819-
LOGGER.error(dashboardSelections.toString());
820-
}
821-
822836
datastore = getDatastore(datasetLabel, map, dashboardSelections.toString(), offset, fetchSize);
823837
datastore.put("widgetData", widget);
824838

0 commit comments

Comments
 (0)