use of com.day.cq.wcm.msm.api.LiveRelationship in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ContainerServlet method doPost.
@Override
protected void doPost(@NotNull SlingHttpServletRequest request, @NotNull final SlingHttpServletResponse response) throws IOException {
ResourceResolver resolver = request.getResourceResolver();
Resource container = request.getResource();
// Delete the child items
try {
String[] deletedChildrenNames = request.getParameterValues(PARAM_DELETED_CHILDREN);
if (deletedChildrenNames != null && deletedChildrenNames.length > 0) {
for (String childName : deletedChildrenNames) {
Resource child = container.getChild(childName);
if (child != null) {
// For deleted items that have a live relationship, ensure a ghost is created
LiveRelationship liveRelationship = liveRelationshipManager.getLiveRelationship(child, false);
if (liveRelationship != null && liveRelationship.getStatus().isSourceExisting()) {
liveRelationshipManager.cancelRelationship(resolver, liveRelationship, true, false);
Resource parent = child.getParent();
String name = child.getName();
resolver.delete(child);
if (parent != null) {
createGhost(parent, name, resolver);
}
} else {
resolver.delete(child);
}
}
}
}
resolver.commit();
} catch (PersistenceException | RepositoryException | WCMException e) {
LOGGER.error("Could not delete items of the container at {}", container.getPath(), e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
// Order the child items
try {
String[] orderedChildrenNames = request.getParameterValues(PARAM_ORDERED_CHILDREN);
if (orderedChildrenNames != null && orderedChildrenNames.length > 0) {
final Node containerNode = container.adaptTo(Node.class);
if (containerNode != null) {
// Create the items if they don't exist
for (int i = 0; i < orderedChildrenNames.length; i++) {
if (!containerNode.hasNode(orderedChildrenNames[i])) {
containerNode.addNode(orderedChildrenNames[i]);
}
}
// Order the items
for (int i = orderedChildrenNames.length - 1; i >= 0; i--) {
// Put the last item at the end
if (i == orderedChildrenNames.length - 1) {
containerNode.orderBefore(orderedChildrenNames[i], null);
} else {
containerNode.orderBefore(orderedChildrenNames[i], orderedChildrenNames[i + 1]);
}
}
// Persist the changes
resolver.commit();
}
}
} catch (RepositoryException | PersistenceException e) {
LOGGER.error("Could not order items of the container at {}", container.getPath(), e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
Aggregations