Search in sources :

Example 81 with Page

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

the class LanguageNavigationImpl method getItems.

private List<NavigationItem> getItems(Page root) {
    List<NavigationItem> pages = new ArrayList<>();
    if (root.getDepth() < structureDepth) {
        Iterator<Page> it = root.listChildren(new PageFilter());
        while (it.hasNext()) {
            Page page = it.next();
            boolean active = currentPage.getPath().equals(page.getPath()) || currentPage.getPath().startsWith(page.getPath() + "/");
            String title = page.getNavigationTitle();
            if (title == null) {
                title = page.getTitle();
            }
            List<NavigationItem> children = getItems(page);
            int level = page.getDepth() - startLevel;
            Page localizedPage = getLocalizedPage(currentPage, page);
            if (localizedPage != null) {
                page = localizedPage;
            }
            boolean current = currentPage.getPath().equals(page.getPath());
            pages.add(newLanguageNavigationItem(page, active, current, linkHandler, level, children, title, getId(), component));
        }
    }
    return pages;
}
Also used : NavigationItem(com.adobe.cq.wcm.core.components.models.NavigationItem) LanguageNavigationItem(com.adobe.cq.wcm.core.components.models.LanguageNavigationItem) ArrayList(java.util.ArrayList) Page(com.day.cq.wcm.api.Page) PageFilter(com.day.cq.wcm.api.PageFilter)

Example 82 with Page

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

the class Utils method getInheritedValue.

/**
 * Get the inherited value of a property from the page content resource. Walk the content tree upwards until an override of that
 * property is specified.
 *
 * @param startPage the page in the content tree to start looking for the requested property.
 * @param propertyName the name of the property which is inherited.
 * @return the inherited value of the property or empty string if the property is not specified in the content tree.
 */
@NotNull
public static String getInheritedValue(com.day.cq.wcm.api.Page startPage, String propertyName) {
    if (startPage == null) {
        return StringUtils.EMPTY;
    }
    com.day.cq.wcm.api.Page tmp = startPage;
    while (tmp != null && tmp.hasContent() && tmp.getDepth() > 1) {
        ValueMap props = tmp.getProperties();
        if (props != null) {
            boolean override = Boolean.parseBoolean(props.get(propertyName + "_override", String.class));
            if (override) {
                return props.get(propertyName, StringUtils.EMPTY);
            }
            tmp = tmp.getParent();
        }
    }
    return StringUtils.EMPTY;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Page(com.day.cq.wcm.api.Page) NotNull(org.jetbrains.annotations.NotNull)

Example 83 with Page

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

the class Utils method getWrappedImageResourceWithInheritance.

/**
 * Wraps an image resource with the properties and child resources of the inherited featured image of either
 * the linked page or the page containing the resource.
 *
 * @param resource The image resource
 * @param linkHandler The link handler
 * @param currentStyle The style of the image resource
 * @param currentPage The page containing the image resource
 * @return The wrapped image resource augmented with inherited properties and child resource if inheritance is enabled, the plain image resource otherwise.
 */
public static Resource getWrappedImageResourceWithInheritance(Resource resource, LinkHandler linkHandler, Style currentStyle, Page currentPage) {
    if (resource == null) {
        LOGGER.error("The resource is not defined");
        return null;
    }
    if (linkHandler == null) {
        LOGGER.error("The link handler is not defined");
        return null;
    }
    ValueMap properties = resource.getValueMap();
    String fileReference = properties.get(DownloadResource.PN_REFERENCE, String.class);
    Resource fileResource = resource.getChild(DownloadResource.NN_FILE);
    boolean imageFromPageImage = properties.get(PN_IMAGE_FROM_PAGE_IMAGE, StringUtils.isEmpty(fileReference) && fileResource == null);
    boolean altValueFromPageImage = properties.get(PN_ALT_VALUE_FROM_PAGE_IMAGE, imageFromPageImage);
    if (imageFromPageImage) {
        Resource inheritedResource = null;
        String linkURL = properties.get(ImageResource.PN_LINK_URL, String.class);
        boolean actionsEnabled = (currentStyle != null) ? !currentStyle.get(Teaser.PN_ACTIONS_DISABLED, !properties.get(Teaser.PN_ACTIONS_ENABLED, true)) : properties.get(Teaser.PN_ACTIONS_ENABLED, true);
        Resource firstAction = Optional.of(resource).map(res -> res.getChild(Teaser.NN_ACTIONS)).map(actions -> actions.getChildren().iterator().next()).orElse(null);
        if (StringUtils.isNotEmpty(linkURL)) {
            // the inherited resource is the featured image of the linked page
            Optional<Link> link = linkHandler.getLink(resource);
            inheritedResource = link.map(link1 -> (Page) link1.getReference()).map(ComponentUtils::getFeaturedImage).orElse(null);
        } else if (actionsEnabled && firstAction != null) {
            // the inherited resource is the featured image of the first action's page (the resource is assumed to be a teaser)
            inheritedResource = Optional.of(linkHandler.getLink(firstAction, Teaser.PN_ACTION_LINK)).map(link1 -> {
                if (link1.isPresent()) {
                    Page linkedPage = (Page) link1.get().getReference();
                    return Optional.ofNullable(linkedPage).map(ComponentUtils::getFeaturedImage).orElse(null);
                }
                return null;
            }).orElse(null);
        } else {
            // the inherited resource is the featured image of the current page
            inheritedResource = Optional.ofNullable(currentPage).map(page -> {
                Template template = page.getTemplate();
                // make sure the resource is part of the currentPage or of its template
                if (StringUtils.startsWith(resource.getPath(), currentPage.getPath()) || (template != null && StringUtils.startsWith(resource.getPath(), template.getPath()))) {
                    return ComponentUtils.getFeaturedImage(currentPage);
                }
                return null;
            }).orElse(null);
        }
        Map<String, String> overriddenProperties = new HashMap<>();
        Map<String, Resource> overriddenChildren = new HashMap<>();
        String inheritedFileReference = null;
        Resource inheritedFileResource = null;
        String inheritedAlt = null;
        String inheritedAltValueFromDAM = null;
        if (inheritedResource != null) {
            // Define the inherited properties
            ValueMap inheritedProperties = inheritedResource.getValueMap();
            inheritedFileReference = inheritedProperties.get(DownloadResource.PN_REFERENCE, String.class);
            inheritedFileResource = inheritedResource.getChild(DownloadResource.NN_FILE);
            inheritedAlt = inheritedProperties.get(ImageResource.PN_ALT, String.class);
            inheritedAltValueFromDAM = inheritedProperties.get(PN_ALT_VALUE_FROM_DAM, String.class);
        }
        overriddenProperties.put(DownloadResource.PN_REFERENCE, inheritedFileReference);
        overriddenChildren.put(DownloadResource.NN_FILE, inheritedFileResource);
        // don't inherit the image title from the page image
        overriddenProperties.put(PN_TITLE_VALUE_FROM_DAM, "false");
        if (altValueFromPageImage) {
            overriddenProperties.put(ImageResource.PN_ALT, inheritedAlt);
            overriddenProperties.put(PN_ALT_VALUE_FROM_DAM, inheritedAltValueFromDAM);
        } else {
            overriddenProperties.put(PN_ALT_VALUE_FROM_DAM, "false");
        }
        return new CoreResourceWrapper(resource, resource.getResourceType(), null, overriddenProperties, overriddenChildren);
    }
    return resource;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModelFactory(org.apache.sling.models.factory.ModelFactory) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoggerFactory(org.slf4j.LoggerFactory) AllowedComponentList(com.day.cq.wcm.foundation.AllowedComponentList) HashMap(java.util.HashMap) DownloadResource(com.day.cq.commons.DownloadResource) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HashSet(java.util.HashSet) JSONException(org.json.JSONException) Style(com.day.cq.wcm.api.designer.Style) JSONObject(org.json.JSONObject) Image(com.adobe.cq.wcm.core.components.models.Image) Map(java.util.Map) Link(com.adobe.cq.wcm.core.components.commons.link.Link) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Designer(com.day.cq.wcm.api.designer.Designer) Collection(java.util.Collection) Set(java.util.Set) Resource(org.apache.sling.api.resource.Resource) ExperienceFragment(com.adobe.cq.wcm.core.components.models.ExperienceFragment) CoreResourceWrapper(com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper) ComponentUtils(com.adobe.cq.wcm.core.components.util.ComponentUtils) PageManager(com.day.cq.wcm.api.PageManager) Nullable(org.jetbrains.annotations.Nullable) Template(com.day.cq.wcm.api.Template) Optional(java.util.Optional) ImageResource(com.day.cq.commons.ImageResource) NotNull(org.jetbrains.annotations.NotNull) Teaser(com.adobe.cq.wcm.core.components.models.Teaser) Collections(java.util.Collections) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) DownloadResource(com.day.cq.commons.DownloadResource) Resource(org.apache.sling.api.resource.Resource) ImageResource(com.day.cq.commons.ImageResource) CoreResourceWrapper(com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper) Page(com.day.cq.wcm.api.Page) Template(com.day.cq.wcm.api.Template) ComponentUtils(com.adobe.cq.wcm.core.components.util.ComponentUtils) Link(com.adobe.cq.wcm.core.components.commons.link.Link)

Example 84 with Page

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

the class PageImageThumbnail method initModel.

@PostConstruct
protected void initModel() {
    configPath = request.getRequestPathInfo().getResourcePath();
    componentPath = request.getRequestPathInfo().getSuffix();
    if (StringUtils.isBlank(componentPath)) {
        RequestParameter itemParam = request.getRequestParameter("item");
        if (itemParam == null) {
            log.error("Suffix and 'item' param are blank");
            return;
        }
        componentPath = itemParam.getString();
    }
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    if (pageManager == null) {
        log.error("pagemanager is null");
        return;
    }
    Resource component = resourceResolver.getResource(componentPath);
    if (component == null) {
        log.error("the component at {} does not exist", componentPath);
        return;
    }
    Page currentPage = pageManager.getContainingPage(component);
    if (currentPage != null) {
        currentPagePath = currentPage.getPath();
    }
    Page targetPage = null;
    RequestParameter pageLinkParam = request.getRequestParameter("pageLink");
    if (pageLinkParam != null) {
        // retrieve the page link from the request parameter
        String pageLink = pageLinkParam.getString();
        targetPage = pageManager.getPage(pageLink);
    } else {
        // retrieve the page link from the component model
        Teaser teaserModel = modelFactory.getModelFromWrappedRequest(request, component, Teaser.class);
        Link link = null;
        if (teaserModel != null) {
            link = teaserModel.getLink();
        } else {
            Image imageModel = modelFactory.getModelFromWrappedRequest(request, component, Image.class);
            if (imageModel != null) {
                link = imageModel.getImageLink();
            }
        }
        if (link != null) {
            targetPage = (Page) link.getReference();
        } else {
            targetPage = currentPage;
        }
    }
    if (targetPage == null) {
        log.info("A target page cannot be found for the link defined in the request parameter or on the server at {}.", component.getPath());
        return;
    }
    Resource featuredImage = ComponentUtils.getFeaturedImage(targetPage);
    if (featuredImage == null) {
        log.info("No featured image defined for the page at {}", targetPage.getPath());
        return;
    }
    Image imageModel = modelFactory.getModelFromWrappedRequest(request, featuredImage, Image.class);
    if (imageModel == null) {
        log.info("the image model of {} is null", featuredImage.getPath());
        return;
    }
    this.alt = imageModel.getAlt();
    this.src = imageModel.getSrc();
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) Teaser(com.adobe.cq.wcm.core.components.models.Teaser) RequestParameter(org.apache.sling.api.request.RequestParameter) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) Image(com.adobe.cq.wcm.core.components.models.Image) Link(com.adobe.cq.wcm.core.components.commons.link.Link) PostConstruct(javax.annotation.PostConstruct)

Example 85 with Page

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

the class BreadcrumbImpl method createItems.

private List<NavigationItem> createItems() {
    List<NavigationItem> items = new ArrayList<>();
    int currentLevel = currentPage.getDepth();
    while (startLevel < currentLevel) {
        Page page = currentPage.getAbsoluteParent(startLevel);
        if (page != null && page.getContentResource() != null) {
            boolean isActivePage = page.equals(currentPage);
            if (isActivePage && hideCurrent) {
                break;
            }
            if (checkIfNotHidden(page)) {
                NavigationItem navigationItem = newBreadcrumbItem(page, isActivePage, linkHandler, currentLevel, Collections.emptyList(), getId(), component);
                items.add(navigationItem);
            }
        }
        startLevel++;
    }
    return items;
}
Also used : NavigationItem(com.adobe.cq.wcm.core.components.models.NavigationItem) ArrayList(java.util.ArrayList) Page(com.day.cq.wcm.api.Page)

Aggregations

Page (com.day.cq.wcm.api.Page)100 Resource (org.apache.sling.api.resource.Resource)45 PageManager (com.day.cq.wcm.api.PageManager)34 Test (org.junit.jupiter.api.Test)22 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)18 ValueMap (org.apache.sling.api.resource.ValueMap)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)12 SlingBindings (org.apache.sling.api.scripting.SlingBindings)12 NotNull (org.jetbrains.annotations.NotNull)10 Test (org.junit.Test)9 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Optional (java.util.Optional)6 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)6 Nullable (org.jetbrains.annotations.Nullable)6 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)5 Template (com.day.cq.wcm.api.Template)5 StringUtils (org.apache.commons.lang3.StringUtils)5 Before (org.junit.Before)5