Search in sources :

Example 1 with LanguageManager

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

the class ExperienceFragmentDataImpl method getLocalizedFragmentVariationPath.

/**
 * Returns the localized path of the experience fragment variation if the experience fragment resource is defined
 * in a template.
 *
 * @return Localized experience fragment variation path
 * @see ExperienceFragment#getLocalizedFragmentVariationPath()
 */
@Nullable
public String getLocalizedFragmentVariationPath() {
    if (localizedFragmentVariationPath != null) {
        return localizedFragmentVariationPath;
    }
    // get the configured fragment variation path
    String fragmentVariationPath = resource.getValueMap().get(ExperienceFragment.PN_FRAGMENT_VARIATION_PATH, String.class);
    if (currentPage != null && inTemplate()) {
        final Resource pageResource = Optional.ofNullable(currentPage).map(p -> p.adaptTo(Resource.class)).orElse(null);
        final String currentPageRootPath = pageResource != null ? LocalizationUtils.getLocalizationRoot(pageResource, resourceResolver, languageManager, relationshipManager) : null;
        // we should use getLocalizationRoot instead of getXfLocalizationRoot once the XF UI supports creating Live and Language Copies
        String xfRootPath = getXfLocalizationRoot(fragmentVariationPath, currentPageRootPath);
        if (StringUtils.isNotEmpty(currentPageRootPath) && StringUtils.isNotEmpty(xfRootPath)) {
            String xfRelativePath = StringUtils.substring(fragmentVariationPath, xfRootPath.length());
            String localizedXfRootPath = StringUtils.replace(currentPageRootPath, CONTENT_ROOT, ExperienceFragmentsConstants.CONTENT_PATH, 1);
            localizedFragmentVariationPath = StringUtils.join(localizedXfRootPath, xfRelativePath, PATH_DELIMITER_CHAR, NN_CONTENT);
        }
    }
    String xfContentPath = String.join(Character.toString(PATH_DELIMITER_CHAR), fragmentVariationPath, NN_CONTENT);
    if (!resourceExists(localizedFragmentVariationPath) && resourceExists(xfContentPath)) {
        localizedFragmentVariationPath = xfContentPath;
    }
    if (!isExperienceFragmentVariation(localizedFragmentVariationPath)) {
        localizedFragmentVariationPath = null;
    }
    return localizedFragmentVariationPath;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ExperienceFragmentsConstants(com.adobe.cq.xf.ExperienceFragmentsConstants) Resource(org.apache.sling.api.resource.Resource) ExperienceFragment(com.adobe.cq.wcm.core.components.models.ExperienceFragment) Text(com.day.text.Text) LanguageManager(com.day.cq.wcm.api.LanguageManager) NN_CONTENT(com.day.cq.wcm.api.NameConstants.NN_CONTENT) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) LocalizationUtils(com.adobe.cq.wcm.core.components.util.LocalizationUtils) PageManager(com.day.cq.wcm.api.PageManager) Nullable(org.jetbrains.annotations.Nullable) ScriptVariable(org.apache.sling.models.annotations.injectorspecific.ScriptVariable) InjectionStrategy(org.apache.sling.models.annotations.injectorspecific.InjectionStrategy) Template(com.day.cq.wcm.api.Template) Model(org.apache.sling.models.annotations.Model) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) OSGiService(org.apache.sling.models.annotations.injectorspecific.OSGiService) SlingObject(org.apache.sling.models.annotations.injectorspecific.SlingObject) Resource(org.apache.sling.api.resource.Resource) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with LanguageManager

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

LanguageManager (com.day.cq.wcm.api.LanguageManager)2 Page (com.day.cq.wcm.api.Page)2 LiveRelationshipManager (com.day.cq.wcm.msm.api.LiveRelationshipManager)2 Optional (java.util.Optional)2 Resource (org.apache.sling.api.resource.Resource)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 Nullable (org.jetbrains.annotations.Nullable)2 ExperienceFragment (com.adobe.cq.wcm.core.components.models.ExperienceFragment)1 LocalizationUtils (com.adobe.cq.wcm.core.components.util.LocalizationUtils)1 ExperienceFragmentsConstants (com.adobe.cq.xf.ExperienceFragmentsConstants)1 NN_CONTENT (com.day.cq.wcm.api.NameConstants.NN_CONTENT)1 PageManager (com.day.cq.wcm.api.PageManager)1 Template (com.day.cq.wcm.api.Template)1 WCMException (com.day.cq.wcm.api.WCMException)1 LiveRelationship (com.day.cq.wcm.msm.api.LiveRelationship)1 Text (com.day.text.Text)1 Iterator (java.util.Iterator)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 PostConstruct (javax.annotation.PostConstruct)1