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);
}
}
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);
}
}
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;
}
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();
}
Aggregations