Search in sources :

Example 6 with LiveRelationship

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);
    }
}
Also used : Node(javax.jcr.Node) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) WCMException(com.day.cq.wcm.api.WCMException)

Aggregations

LiveRelationship (com.day.cq.wcm.msm.api.LiveRelationship)6 Resource (org.apache.sling.api.resource.Resource)5 LiveRelationshipManager (com.day.cq.wcm.msm.api.LiveRelationshipManager)4 WCMException (com.day.cq.wcm.api.WCMException)3 RangeIterator (javax.jcr.RangeIterator)3 LiveCopy (com.day.cq.wcm.msm.api.LiveCopy)2 ArrayList (java.util.ArrayList)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 Nullable (org.jetbrains.annotations.Nullable)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 LanguageManager (com.day.cq.wcm.api.LanguageManager)1 Page (com.day.cq.wcm.api.Page)1 LiveStatus (com.day.cq.wcm.msm.api.LiveStatus)1 MockLanguageManager (io.wcm.testing.mock.aem.MockLanguageManager)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1