Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4603,15 +4603,12 @@ public void deleteRelatedContent(final Contentlet contentlet, final Relationship
cons = permissionAPI
.filterCollection(cons, PermissionAPI.PERMISSION_READ, respectFrontendRoles, user);

for (final Contentlet relatedContent : cons) {
if (hasParent) {
TreeFactory.deleteTreesByParentAndChildAndRelationType(contentlet.getIdentifier(),
relatedContent.getIdentifier(), relationship.getRelationTypeValue());
} else {
TreeFactory.deleteTreesByParentAndChildAndRelationType(
relatedContent.getIdentifier(),
contentlet.getIdentifier(), relationship.getRelationTypeValue());
}
if (hasParent) {
TreeFactory.deleteTreesByParentAndRelationType(contentlet.getIdentifier(),
relationship.getRelationTypeValue());
} else {
TreeFactory.deleteTreesByChildAndRelationType(contentlet.getIdentifier(),
relationship.getRelationTypeValue());
}

final List<String> identifiersToBeRelated = contentletsToBeRelated.stream().map(
Expand Down Expand Up @@ -4977,12 +4974,20 @@ public void relateContent(final Contentlet contentlet,
Tree newTree;
Set<Tree> uniqueRelationshipSet = new HashSet<>();

List<Contentlet> conRels = getRelatedContentFromIndex(contentlet, relationship,
related.isHasParent(), user, respectFrontendRoles);

int treePosition = (conRels != null && conRels.size() != 0) ? conRels.size() : 1;
final String countSQL = related.isHasParent()
? "SELECT COUNT(*) as count FROM tree WHERE parent = ? AND relation_type = ?"
: "SELECT COUNT(*) as count FROM tree WHERE child = ? AND relation_type = ?";
final List<Map<String, Object>> countResult = new DotConnect().setSQL(countSQL)
.addParam(contentlet.getIdentifier())
.addParam(relationship.getRelationTypeValue())
.loadObjectResults();
final int existingCount = countResult.isEmpty() ? 0
: Integer.parseInt(countResult.get(0).get("count").toString());

int treePosition = existingCount > 0 ? existingCount : 1;
int positionInParent = 1;

final List<Tree> treesToInsert = new ArrayList<>();
for (Contentlet c : related.getRecords()) {
if (child) {
for (Tree currentTree : contentParents) {
Expand All @@ -5002,17 +5007,12 @@ public void relateContent(final Contentlet contentlet,
positionInParent = positionInParent + 1;

if (uniqueRelationshipSet.add(newTree)) {
final int newTreePosition = newTree.getTreeOrder();
final Tree treeToUpdate = TreeFactory.getTree(newTree);
treeToUpdate.setTreeOrder(newTreePosition);

TreeFactory.saveTree(treeToUpdate != null && UtilMethods.isSet(
treeToUpdate.getRelationType()) ? treeToUpdate : newTree);

treesToInsert.add(newTree);
treePosition++;
}
invalidateRelatedContentCache(c, relationship, !related.isHasParent());
}
TreeFactory.insertTrees(treesToInsert);

//If relationship field, related content cache must be invalidated
invalidateRelatedContentCache(contentlet, relationship, related.isHasParent());
Expand Down Expand Up @@ -7035,7 +7035,7 @@ private void addRestrictedContentForLimitedUser(final Contentlet fromContentlet,
final Relationship relationship = contentletRelationshipRecords
.getRelationship();
final UserAPI userAPI = APILocator.getUserAPI();
if (relationshipAPI.sameParentAndChild(relationship)) {
/*if (relationshipAPI.sameParentAndChild(relationship)) {
//from parent
addContentLimitedByPermissions(user, contentletRelationshipRecords,
getRelatedContentFromIndex(
Expand All @@ -7056,7 +7056,7 @@ private void addRestrictedContentForLimitedUser(final Contentlet fromContentlet,
relationshipAPI.isParent(relationship, contentType),
userAPI.getSystemUser(),
true));
}
}*/
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.categories.business.CategoryAPI;
import com.dotmarketing.portlets.categories.model.Category;
import com.dotmarketing.common.model.ContentletSearch;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
Expand Down Expand Up @@ -1252,18 +1253,16 @@ public List<String> dependenciesLeftToReindex(final Contentlet contentlet) throw

for(final Relationship relationship : relationships) {

final List<Contentlet> oldDocs;
final List<String> oldRelatedIds = new ArrayList<>();
final List<String> newRelatedIds = new ArrayList<>();

oldDocs = contentletAPI.getRelatedContent(contentlet, relationship,
userAPI.getSystemUser(), false);

if(oldDocs.size() > 0) {
for(Contentlet oldDoc : oldDocs) {
oldRelatedIds.add(oldDoc.getIdentifier());
}
}
final List<ContentletSearch> oldSearchResults = contentletAPI.searchIndex(
"+" + relationship.getRelationTypeValue()
+ ":" + contentlet.getIdentifier(),
-1, 0, null, userAPI.getSystemUser(), false);
for (final ContentletSearch result : oldSearchResults) {
oldRelatedIds.add(result.getIdentifier());
}

relatedContentlets.stream().filter(map -> map.get(ESMappingConstants.RELATION_TYPE)
.equals(relationship.getRelationTypeValue())).forEach(
Expand Down
54 changes: 50 additions & 4 deletions dotCMS/src/main/java/com/dotmarketing/factories/TreeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import com.dotmarketing.beans.Tree;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.common.db.DotConnect;
import com.dotmarketing.db.DbConnectionFactory;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.util.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -349,10 +353,52 @@ public static void insertTree(Tree tree) {
}
}




public static void deleteTreesByParentAndRelationType(final String parentId,
final String relationType) {
try {
new DotConnect()
.setSQL("DELETE FROM tree WHERE parent = ? AND relation_type = ?")
.addParam(parentId)
.addParam(relationType)
.loadResult();
} catch (DotDataException e) {
throw new DotStateException(e);
}
}

public static void deleteTreesByChildAndRelationType(final String childId,
final String relationType) {
try {
new DotConnect()
.setSQL("DELETE FROM tree WHERE child = ? AND relation_type = ?")
.addParam(childId)
.addParam(relationType)
.loadResult();
} catch (DotDataException e) {
throw new DotStateException(e);
}
}


public static void insertTrees(final List<Tree> trees) {
if (trees == null || trees.isEmpty()) {
return;
}
try {
final Connection conn = DbConnectionFactory.getConnection();
try (PreparedStatement ps = conn.prepareStatement(
"INSERT INTO tree (child, parent, relation_type, tree_order) VALUES (?,?,?,?)")) {
for (final Tree tree : trees) {
ps.setString(1, tree.getChild());
ps.setString(2, tree.getParent());
ps.setString(3, tree.getRelationType());
ps.setInt(4, tree.getTreeOrder());
ps.addBatch();
}
ps.executeBatch();
}
} catch (SQLException e) {
throw new DotStateException(e);
}
}
}

Loading