Search in sources :

Example 1 with WCMException

use of com.day.cq.wcm.api.WCMException in project acs-aem-commons by Adobe-Consulting-Services.

the class MockPageManager method move.

@Override
public Resource move(Resource resource, String destination, String beforeName, boolean shallow, boolean resolveConflict, String[] adjustRefs, String[] publishRefs) throws WCMException {
    try {
        if (rr == null) {
            throw new RuntimeException("Resource resolver was null");
        }
        if (replicator == null) {
            throw new RuntimeException("Replicator was not changed out -- will not work properly");
        }
        replicator.replicate(null, ReplicationActionType.DEACTIVATE, resource.getPath());
        replicator.replicate(null, ReplicationActionType.ACTIVATE, destination + "/" + resource.getName());
        return resource;
    } catch (ReplicationException ex) {
        throw new WCMException(ex);
    }
}
Also used : ReplicationException(com.day.cq.replication.ReplicationException) WCMException(com.day.cq.wcm.api.WCMException)

Example 2 with WCMException

use of com.day.cq.wcm.api.WCMException 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)

Example 3 with WCMException

use of com.day.cq.wcm.api.WCMException in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class LocalizationUtils method getBlueprintPath.

/**
 * Returns the path of the blueprint of the resource.
 *
 * @param resource the resource
 * @param relationshipManager the live relationship manager service
 * @return the path of the blueprint of the resource if it exists, {@code null} otherwise
 */
@Nullable
public static String getBlueprintPath(@NotNull Resource resource, @NotNull LiveRelationshipManager relationshipManager) {
    try {
        if (relationshipManager.isSource(resource)) {
            // the resource is a blueprint
            RangeIterator liveCopiesIterator = relationshipManager.getLiveRelationships(resource, null, null);
            if (liveCopiesIterator != null) {
                LiveRelationship relationship = (LiveRelationship) liveCopiesIterator.next();
                LiveCopy liveCopy = relationship.getLiveCopy();
                if (liveCopy != null) {
                    return liveCopy.getBlueprintPath();
                }
            }
        }
    } catch (WCMException e) {
        LOGGER.error("Unable to get the blueprint: {}", e.getMessage());
    }
    return null;
}
Also used : LiveCopy(com.day.cq.wcm.msm.api.LiveCopy) RangeIterator(javax.jcr.RangeIterator) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) WCMException(com.day.cq.wcm.api.WCMException) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with WCMException

use of com.day.cq.wcm.api.WCMException in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class LocalizationUtils method getLocalPage.

/**
 * Given the current requested page and a reference page, this method will determine a page belonging to the
 * current site and locale that can be used instead of the reference site.
 *
 * Specifically, if the reference page and the current page are both found under a language root, and that language
 * root is not the same, then the returned page is the page located under the current page's language root at the
 * same relative path as the reference page is located under it's own language root; or empty if that page does not
 * exist.
 *
 * If either the reference page or the current page are not located under a language root, or if they share the
 * same language root, and if the reference page has a live relationship where the target is the current page or
 * an ancestor of the current page, then the target of that live relationship is returned; or empty if that page
 * does not exist.
 *
 * All other conditions return empty.
 *
 * @param referencePage The referenced page.
 * @param currentPage The current page.
 * @param resourceResolver A resource resolver.
 * @param languageManager The language manager service.
 * @param relationshipManager The live relationship manager service.
 * @return A page, that belongs to the same language or live copy as the current page, and can be used as the local
 * alternative to the referenced page, or empty if no such page exists.
 */
public static Optional<Page> getLocalPage(@NotNull final Page referencePage, @NotNull final Page currentPage, @NotNull final ResourceResolver resourceResolver, @NotNull final LanguageManager languageManager, @NotNull final LiveRelationshipManager relationshipManager) {
    Page referencePageLanguageRoot = Optional.ofNullable(referencePage.getPath()).map(resourceResolver::getResource).map(languageManager::getLanguageRoot).orElse(null);
    Page currentPageLanguageRoot = languageManager.getLanguageRoot(currentPage.getContentResource());
    if (referencePageLanguageRoot != null && currentPageLanguageRoot != null && !referencePageLanguageRoot.equals(currentPageLanguageRoot)) {
        // check if there's a language copy of the navigation root
        return Optional.ofNullable(referencePage.getPageManager().getPage(ResourceUtil.normalize(String.join("/", currentPageLanguageRoot.getPath(), getRelativePath(referencePageLanguageRoot, referencePage)))));
    } else {
        try {
            String currentPagePath = currentPage.getPath() + "/";
            return Optional.of(Optional.ofNullable((Iterator<LiveRelationship>) relationshipManager.getLiveRelationships(referencePage.adaptTo(Resource.class), null, null)).map(liveRelationshipIterator -> StreamSupport.stream(((Iterable<LiveRelationship>) () -> liveRelationshipIterator).spliterator(), false)).orElseGet(Stream::empty).map(LiveRelationship::getTargetPath).filter(target -> currentPagePath.startsWith(target + "/")).map(referencePage.getPageManager()::getPage).findFirst().orElse(referencePage));
        } catch (WCMException e) {
        // ignore it
        }
    }
    return Optional.empty();
}
Also used : LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ResourceUtil(org.apache.sling.api.resource.ResourceUtil) Iterator(java.util.Iterator) Resource(org.apache.sling.api.resource.Resource) LanguageManager(com.day.cq.wcm.api.LanguageManager) Page(com.day.cq.wcm.api.Page) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) Nullable(org.jetbrains.annotations.Nullable) WCMException(com.day.cq.wcm.api.WCMException) Stream(java.util.stream.Stream) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) NotNull(org.jetbrains.annotations.NotNull) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) Stream(java.util.stream.Stream) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) WCMException(com.day.cq.wcm.api.WCMException)

Aggregations

WCMException (com.day.cq.wcm.api.WCMException)4 LiveRelationship (com.day.cq.wcm.msm.api.LiveRelationship)3 Resource (org.apache.sling.api.resource.Resource)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 Nullable (org.jetbrains.annotations.Nullable)2 ReplicationException (com.day.cq.replication.ReplicationException)1 LanguageManager (com.day.cq.wcm.api.LanguageManager)1 Page (com.day.cq.wcm.api.Page)1 LiveCopy (com.day.cq.wcm.msm.api.LiveCopy)1 LiveRelationshipManager (com.day.cq.wcm.msm.api.LiveRelationshipManager)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 RangeIterator (javax.jcr.RangeIterator)1 RepositoryException (javax.jcr.RepositoryException)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 ResourceUtil (org.apache.sling.api.resource.ResourceUtil)1 NotNull (org.jetbrains.annotations.NotNull)1