diff --git a/dotCMS/src/main/java/com/dotcms/contenttype/business/StoryBlockAPIImpl.java b/dotCMS/src/main/java/com/dotcms/contenttype/business/StoryBlockAPIImpl.java index 23b16e71f64f..c2392a37b12c 100644 --- a/dotCMS/src/main/java/com/dotcms/contenttype/business/StoryBlockAPIImpl.java +++ b/dotCMS/src/main/java/com/dotcms/contenttype/business/StoryBlockAPIImpl.java @@ -14,6 +14,7 @@ import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; import com.fasterxml.jackson.core.JsonProcessingException; +import com.dotmarketing.exception.DotRuntimeException; import com.google.common.collect.ImmutableList; import com.liferay.util.StringPool; import io.vavr.control.Try; @@ -63,8 +64,11 @@ public StoryBlockReferenceResult refreshStoryBlockValueReferences(final Object s boolean refreshed = false; try { final LinkedHashMap blockEditorMap = this.toMap(storyBlockValue); - final List> contentsMap = (List) blockEditorMap.get(CONTENT_KEY); - for (final Map contentMap : contentsMap) { + final Object contentsMap = blockEditorMap.get(CONTENT_KEY); + if(!UtilMethods.isSet(contentsMap) || !(contentsMap instanceof List)) { + return new StoryBlockReferenceResult(true, storyBlockValue); + } + for (final Map contentMap : (List>) contentsMap) { if (UtilMethods.isSet(contentMap)) { final String type = contentMap.get(TYPE_KEY).toString(); if (allowedTypes.contains(type)) { // if somebody adds a story block to itself, we don't want to refresh it @@ -78,9 +82,8 @@ public StoryBlockReferenceResult refreshStoryBlockValueReferences(final Object s } catch (final Exception e) { final String errorMsg = String.format("An error occurred when refreshing Story Block Contentlet " + "references: %s", e.getMessage()); - Logger.error(StoryBlockAPIImpl.class, errorMsg); - Logger.debug(StoryBlockAPIImpl.class, errorMsg, e); - throw new RuntimeException(errorMsg, e); + Logger.warnAndDebug(StoryBlockAPIImpl.class, errorMsg,e); + throw new DotRuntimeException(errorMsg, e); } // Return the original value in case no data was refreshed return new StoryBlockReferenceResult(false, storyBlockValue); @@ -105,8 +108,8 @@ private boolean refreshStoryBlockMap(final Map contentMap, final if (UtilMethods.isSet(attrsMap)) { final Map dataMap = (Map) attrsMap.get(DATA_KEY); if (UtilMethods.isSet(dataMap)) { - final String identifier = dataMap.get(IDENTIFIER_KEY).toString(); - final String inode = dataMap.get(INODE_KEY).toString(); + final String identifier = (String) dataMap.get(IDENTIFIER_KEY); + final String inode = (String) dataMap.get(INODE_KEY); final long languageId = ConversionUtils.toLong(dataMap.get(LANGUAGE_ID_KEY), ()-> APILocator.getLanguageAPI().getDefaultLanguage().getId()); if (UtilMethods.isSet(identifier) && UtilMethods.isSet(inode)) { if (!identifier.equals(parentContentletIdentifier)) { // if somebody adds a story block to itself, we don't want to refresh it @@ -131,11 +134,9 @@ private boolean refreshStoryBlockMap(final Map contentMap, final @Override public List getDependencies(final Contentlet contentlet) { final ImmutableList.Builder contentletIdList = new ImmutableList.Builder<>(); - if (null != contentlet && null != contentlet.getContentType()) { - contentlet.getContentType().fields(StoryBlockField.class).forEach(field -> { - contentletIdList.addAll(this.getDependencies(contentlet.get(field.variable()))); - }); - } + contentlet.getContentType().fields(StoryBlockField.class).forEach(field -> + contentletIdList.addAll(this.getDependencies(contentlet.get(field.variable()))) + ); return contentletIdList.build(); } @@ -146,11 +147,15 @@ public List getDependencies(final Object storyBlockValue) { try { if (null != storyBlockValue && JsonUtil.isValidJSON(storyBlockValue.toString())) { final Map blockEditorMap = this.toMap(storyBlockValue); - final List> contentsMap = (List) blockEditorMap.get(CONTENT_KEY); - for (final Map contentMapObject : contentsMap) { + Object contentsMap = blockEditorMap.getOrDefault(CONTENT_KEY, List.of()); + if(!(contentsMap instanceof List)) { + return List.of(); + } + + for (final Map contentMapObject : (List>) contentsMap) { if (UtilMethods.isSet(contentMapObject)) { - final String type = contentMapObject.get(TYPE_KEY).toString(); - if (allowedTypes.contains(type)) { + final String type = (String) contentMapObject.get(TYPE_KEY); + if (type !=null && allowedTypes.contains(type)) { addDependencies(contentletIdList, contentMapObject); } } @@ -159,9 +164,7 @@ public List getDependencies(final Object storyBlockValue) { } catch (final Exception e) { final String errorMsg = String.format("An error occurred when retrieving Contentlet references from Story" + " Block field: %s", e.getMessage()); - Logger.error(StoryBlockAPIImpl.class, errorMsg); - Logger.debug(StoryBlockAPIImpl.class, errorMsg, e); - throw new RuntimeException(errorMsg, e); + Logger.warnAndDebug(StoryBlockAPIImpl.class, errorMsg,e); } return contentletIdList.build(); } @@ -175,9 +178,8 @@ public Object addContentlet(final Object storyBlockValue, final Contentlet conte } catch (final JsonProcessingException e) { final String errorMsg = String.format("An error occurred when adding Contentlet '%s' to the Story Block " + "field: %s", contentlet.getIdentifier(), e.getMessage()); - Logger.error(StoryBlockAPIImpl.class, errorMsg); - Logger.debug(StoryBlockAPIImpl.class, errorMsg, e); - throw new RuntimeException(errorMsg, e); + Logger.warnAndDebug(StoryBlockAPIImpl.class, errorMsg, e); + return storyBlockValue; } }