Skip to content

Commit

Permalink
Fix: first Step (#30613)
Browse files Browse the repository at this point in the history
### Proposed Changes
* Remove this Stop condition because it was addressing to random
behavior


https://github.com/dotCMS/core/pull/30613/files#diff-a44ca86884cbc5b4cbfefc706c1ba39891814a72c266937f621ef70f231abb34L89

- Decrease the depth when we go down into the levels


https://github.com/dotCMS/core/pull/30613/files#diff-a44ca86884cbc5b4cbfefc706c1ba39891814a72c266937f621ef70f231abb34R359-R365

- Not use the default addRelationship method because it take the Query
parameter depth value over the Request attribute value


https://github.com/dotCMS/core/pull/30613/files#diff-8120c6eddd966c9d6be3ee4dbc5af4a94bea9946b0627eb49991dde286610e49R791-R802

- Set the Depth from the URL parameter to the attribute, we are taking
it from the attribute to set the depth to any Block Editor


https://github.com/dotCMS/core/pull/30613/files#diff-95c4fca870c194a8a966d4063a39779340be2a09ccefce38ab560be274076b2fR599

- Reset the relationship value, If the Contentlet is take from Cache
then this value can be set with a different depth


https://github.com/dotCMS/core/pull/30613/files#diff-95c4fca870c194a8a966d4063a39779340be2a09ccefce38ab560be274076b2fR1303
  • Loading branch information
freddyDOTCMS authored Nov 20, 2024
1 parent a8e031c commit 913c82b
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.dotcms.notifications.bean.NotificationLevel;
import com.dotcms.notifications.bean.NotificationType;
import com.dotcms.notifications.business.NotificationAPI;
import com.dotcms.rendering.velocity.viewtools.content.util.ContentUtils;
import com.dotcms.repackage.net.sf.hibernate.ObjectNotFoundException;
import com.dotcms.rest.api.v1.DotObjectMapperProvider;
import com.dotcms.system.SimpleMapAppContext;
Expand Down Expand Up @@ -58,15 +59,7 @@
import com.dotmarketing.portlets.structure.model.Field;
import com.dotmarketing.portlets.structure.model.Structure;
import com.dotmarketing.portlets.workflows.business.WorkFlowFactory;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.InodeUtils;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.NumberUtil;
import com.dotmarketing.util.PaginatedArrayList;
import com.dotmarketing.util.RegEX;
import com.dotmarketing.util.RegExMatch;
import com.dotmarketing.util.UUIDGenerator;
import com.dotmarketing.util.UtilMethods;
import com.dotmarketing.util.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -931,6 +924,7 @@ private Contentlet processCachedContentlet(final Contentlet cachedContentlet) {
contentletCache.add(refreshedContentlet.getInode(), refreshedContentlet);
return refreshedContentlet;
}

}
return cachedContentlet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@
import org.apache.commons.lang3.mutable.MutableBoolean;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;

/**
* Implementation class for the {@link StoryBlockAPI}.
Expand All @@ -56,6 +51,7 @@ public StoryBlockReferenceResult refreshReferences(final Contentlet contentlet)
final boolean inTransaction = DbConnectionFactory.inTransaction();
if (!inTransaction && null != contentlet && null != contentlet.getContentType() &&
contentlet.getContentType().hasStoryBlockFields()) {

if (ThreadUtils.isMethodCallCountEqualThan(this.getClass().getName(), "refreshReferences", MAX_RECURSION_LEVEL)) {
Logger.debug(this, () -> "This method has been called more than " + MAX_RECURSION_LEVEL +
" times in the same thread. This could be a sign of circular reference in the Story Block field. Data will NOT be refreshed.");
Expand All @@ -66,6 +62,7 @@ public StoryBlockReferenceResult refreshReferences(final Contentlet contentlet)
.forEach(field -> {

final Object storyBlockValue = contentlet.get(field.variable());

if (null != storyBlockValue) {
final StoryBlockReferenceResult result =
this.refreshStoryBlockValueReferences(storyBlockValue, contentlet.getIdentifier());
Expand All @@ -86,12 +83,6 @@ public StoryBlockReferenceResult refreshReferences(final Contentlet contentlet)
public StoryBlockReferenceResult refreshStoryBlockValueReferences(final Object storyBlockValue, final String parentContentletIdentifier) {
boolean refreshed = false;
if (null != storyBlockValue && JsonUtil.isValidJSON(storyBlockValue.toString())) {
if (ThreadUtils.isMethodCallCountEqualThan(this.getClass().getName(),
"refreshStoryBlockValueReferences", MAX_RECURSION_LEVEL)) {
Logger.debug(this, () -> "This method has been called more than " + MAX_RECURSION_LEVEL +
" times in the same thread. This could be a sign of circular reference in the Story Block field. Data will NOT be refreshed.");
return new StoryBlockReferenceResult(false, storyBlockValue);
}
try {
final LinkedHashMap<String, Object> blockEditorMap = this.toMap(storyBlockValue);
final Object contentsMap = blockEditorMap.get(CONTENT_KEY);
Expand Down Expand Up @@ -364,10 +355,58 @@ private void refreshBlockEditorDataMap(final Map<String, Object> dataMap, final
private void addContentletRelationships(final Contentlet contentlet) {
final HttpServletRequest httpRequest = HttpServletRequestThreadLocal.INSTANCE.getRequest();
final PageMode currentPageMode = PageMode.get(httpRequest);
if (null != httpRequest && null == httpRequest.getAttribute(WebKeys.HTMLPAGE_DEPTH)) {
httpRequest.setAttribute(WebKeys.HTMLPAGE_DEPTH, MAX_RELATIONSHIP_DEPTH.get());

int depth = getInitialDepthValue();

if (isInsideAnotherBlockEditorAndRelatedContent()) {
depth = decreaseDepthValue(depth);
}

ContentUtils.addRelationships(contentlet, APILocator.systemUser(), currentPageMode, contentlet.getLanguageId(), depth);
}

/**
* Decrease the DEPTH value:
* If the current value is 2, reduce it to 0.
* If the current value is 3, reduce it to 1.
*
* @param depthValue current depth value
* @return
*/
private int decreaseDepthValue(int depthValue) {
if (depthValue == 2) {
return 0;
}
ContentUtils.addRelationships(contentlet, APILocator.systemUser(), currentPageMode, contentlet.getLanguageId());

if (depthValue == 3) {
return 1;
}

return depthValue;
}

public int getInitialDepthValue(){
final HttpServletRequest httpRequest = HttpServletRequestThreadLocal.INSTANCE.getRequest();
String value = null;

if (null != httpRequest && null != httpRequest.getAttribute(WebKeys.HTMLPAGE_DEPTH)) {
value = (String) httpRequest.getAttribute(WebKeys.HTMLPAGE_DEPTH);
} else {
value = MAX_RELATIONSHIP_DEPTH.get();
}

int depth = ConversionUtils.toInt(value, 0);
return depth < 0 || depth > 3 ? 0 : depth;
}

private boolean isInsideAnotherBlockEditorAndRelatedContent(){
boolean insideAnotherBlockEditor =
ThreadUtils.isMethodCallCountEqualThan(this.getClass().getName(), "refreshReferences", 2);

boolean insideContentRelated =
ThreadUtils.isMethodCallCountEqualThan(Contentlet.class.getName(), "getRelated", 1);

return insideAnotherBlockEditor && insideContentRelated;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,13 +771,13 @@ public static String addDefaultsToQuery(String query, final boolean editOrPrevie
* @param languageId
*/
public static void addRelationships(final Contentlet contentlet, final User user, final PageMode mode, final long languageId) {

final HttpServletRequest request = HttpServletRequestThreadLocal.INSTANCE.getRequest();
final HttpServletResponse response = HttpServletResponseThreadLocal.INSTANCE.getResponse();
if (addRelationshipsOnPage &&
Objects.nonNull(response) &&
Objects.nonNull(request) &&
Objects.nonNull(user)) {

final String depthParam =
Objects.nonNull(request.getParameter(WebKeys.HTMLPAGE_DEPTH)) ?
request.getParameter(WebKeys.HTMLPAGE_DEPTH) :
Expand All @@ -788,6 +788,19 @@ public static void addRelationships(final Contentlet contentlet, final User user
}
}
}
public static void addRelationships(final Contentlet contentlet, final User user, final PageMode mode,
final long languageId, final int depth) {

final HttpServletRequest request = HttpServletRequestThreadLocal.INSTANCE.getRequest();
final HttpServletResponse response = HttpServletResponseThreadLocal.INSTANCE.getResponse();
if (addRelationshipsOnPage &&
Objects.nonNull(response) &&
Objects.nonNull(request) &&
Objects.nonNull(user)) {

addRelationships(contentlet, user, mode, languageId, depth, request, response);
}
}

/**
* Adds the relationships to the contentlet based on depth argument
Expand Down
4 changes: 4 additions & 0 deletions dotCMS/src/main/java/com/dotcms/rest/ContentResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ public Response getContent(@Context HttpServletRequest request, @Context final H
final String depthParam = paramsMap.get(RESTParams.DEPTH.getValue());
final int depth = toInt(depthParam, () -> -1);

request.setAttribute(RESTParams.DEPTH.toString(), String.valueOf(depth));

if ((depth < 0 || depth > 3) && depthParam != null){
final String errorMsg =
"Error searching content " + id + ". Reason: Invalid depth: " + depthParam;
Expand Down Expand Up @@ -1298,6 +1300,8 @@ private static JSONArray addRelatedContentToJsonArray(HttpServletRequest request
final JSONArray jsonArray = new JSONArray();

for (Contentlet relatedContent : contentlet.getRelated(field.variable(), user, respectFrontendRoles, isParent, language, live)) {
relatedContent.setProperty(field.name(), null);

switch (depth) {
//returns a list of identifiers
case 0:
Expand Down
Loading

0 comments on commit 913c82b

Please sign in to comment.