Skip to content

Commit 1df6816

Browse files
committed
[KNOWAGE-8484]improve image usage check in ImagesService by adding dashboard template parsing logic
1 parent a3dd6ff commit 1df6816

1 file changed

Lines changed: 39 additions & 21 deletions

File tree

knowage-core/src/main/java/it/eng/spagobi/images/ImagesService.java

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ public String addImage(MultiPartBody input, @Context HttpServletRequest req) {
192192
}
193193
}
194194

195-
private String checkIfImageIsInUse(Integer imageId) {
195+
private StringBuilder checkIfImageIsInUse(Integer imageId) {
196196
LOGGER.debug("IN");
197197

198-
String toReturn = null;
198+
StringBuilder toReturn = null;
199199

200200
try {
201201
List<BIObject> biObjects = new ArrayList<>();
@@ -206,24 +206,42 @@ private String checkIfImageIsInUse(Integer imageId) {
206206

207207
biObjects.addAll(biObjectDAO.loadBIObjects(SpagoBIConstants.DASHBOARD_TYPE, null, null));
208208

209-
// check with string, is enough
210-
for (Iterator<BIObject> iterator = biObjects.iterator(); iterator.hasNext();) {
211-
BIObject object = iterator.next();
212-
ObjTemplate template = object.getActiveTemplate();
213-
if (template != null) {
214-
byte[] templateContentBytes = template.getContent();
215-
if (templateContentBytes != null) {
216-
String templateContent = new String(templateContentBytes);
217-
if (templateContent.indexOf("\"imgId\":" + imageId) != -1) {
218-
if (toReturn == null) {
219-
toReturn = object.getName();
220-
} else {
221-
toReturn += ", " + object.getName();
209+
for (BIObject object : biObjects) {
210+
ObjTemplate template = object.getActiveTemplate();
211+
if (template != null) {
212+
byte[] templateContentBytes = template.getContent();
213+
if (templateContentBytes != null) {
214+
String templateContent = new String(templateContentBytes);
215+
// CHECKING THE COCKPIT TEMPLATE (DOCUMENT_COMPOSITE)
216+
if (templateContent.contains("\"imgId\": " + imageId) || templateContent.contains("\"imgId\":" + imageId)) {
217+
if (toReturn == null) {
218+
toReturn = new StringBuilder(object.getName());
219+
} else {
220+
toReturn.append(", ").append(object.getName());
221+
}
222+
// CHECKING THE DASHBOARD TEMPLATE
223+
} else {
224+
JSONObject templateJson = new JSONObject(templateContent);
225+
if (templateJson.has("widgets") && templateJson.getJSONArray("widgets").length() > 0) {
226+
JSONArray widgets = templateJson.getJSONArray("widgets");
227+
for (int i = 0; i < widgets.length(); i++) {
228+
JSONObject widget = widgets.getJSONObject(i);
229+
if (widget.has("settings") && widget.getJSONObject("settings").has("configuration") && widget.getJSONObject("settings").getJSONObject("configuration").has("image")) {
230+
JSONObject image = widget.getJSONObject("settings").getJSONObject("configuration").getJSONObject("image");
231+
if (image.getInt("id") == imageId) {
232+
if (toReturn == null) {
233+
toReturn = new StringBuilder(object.getName());
234+
} else {
235+
toReturn.append(", ").append(object.getName());
236+
}
237+
}
238+
}
239+
}
222240
}
223-
}
224-
}
225-
}
226-
}
241+
}
242+
}
243+
}
244+
}
227245

228246
} catch (Exception e) {
229247
LOGGER.error("Error in checking if image is used in other cockpits template", e);
@@ -244,14 +262,14 @@ public String deleteImage(@Context HttpServletRequest req) {
244262
boolean success = true;
245263
try {
246264
Object idObj = req.getParameter("imageId");
247-
Integer id = null;
265+
Integer id;
248266
if (idObj == null) {
249267
msg = "sbi.cockpit.widgets.image.imageWidgetDesigner.emptyParameter";
250268
success = false;
251269
} else {
252270
id = Integer.valueOf(idObj.toString());
253271

254-
String labelInUSe = checkIfImageIsInUse(id);
272+
StringBuilder labelInUSe = checkIfImageIsInUse(id);
255273
if (labelInUSe != null) {
256274
LOGGER.error("Cannot delete image because it is in use in " + labelInUSe);
257275
msg = "Cannot delete image because it is in use in documents " + labelInUSe;

0 commit comments

Comments
 (0)