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