Search in sources :

Example 1 with Config

use of com.adobe.granite.ui.components.Config in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ElementNamesRenderCondition method doGet.

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
    // return false by default
    request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(false));
    // get component path
    Config config = new Config(request.getResource());
    String componentPath = getParameter(config, request, PN_COMPONENT_PATH, String.class);
    if (componentPath == null) {
        return;
    }
    // get component resource
    Resource component = request.getResourceResolver().getResource(componentPath);
    if (component == null) {
        return;
    }
    // override fragment path if set
    ValueMap properties = component.getValueMap();
    String displayMode = properties.get(PARAM_AND_PN_DISPLAY_MODE, String.class);
    String displayModeParam = request.getParameter(PARAM_AND_PN_DISPLAY_MODE);
    if (displayModeParam != null) {
        displayMode = displayModeParam;
    }
    boolean shouldShow = displayMode != null && displayMode.equals(getParameter(config, request, PARAM_AND_PN_DISPLAY_MODE, String.class));
    request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(shouldShow));
}
Also used : SimpleRenderCondition(com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition) RenderCondition(com.adobe.granite.ui.components.rendercondition.RenderCondition) SimpleRenderCondition(com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition) Config(com.adobe.granite.ui.components.Config) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource)

Example 2 with Config

use of com.adobe.granite.ui.components.Config in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AbstractContentFragmentDataSourceServlet method getContentFragment.

/**
 * Returns the content fragment to use for this datasource, as configured by the datasource properties. If no
 * content fragment is correctly configured, then {@code null} is returned.
 */
@Nullable
private ContentFragment getContentFragment(@NotNull SlingHttpServletRequest request) {
    Config config = getConfig(request);
    ValueMap map = getComponentValueMap(config, request);
    if (config == null) {
        return null;
    }
    // get fragment path
    String fragmentPath = getParameter(config, PN_FRAGMENT_PATH, request);
    // if not present or empty, get fragment path via component
    if (StringUtils.isEmpty(fragmentPath)) {
        // get fragment path from component resource
        fragmentPath = map != null ? map.get(PN_PATH, String.class) : null;
    }
    // no fragment path available
    if (StringUtils.isEmpty(fragmentPath)) {
        return null;
    }
    // get content fragment resource
    Resource fragmentResource = request.getResourceResolver().getResource(fragmentPath);
    if (fragmentResource == null) {
        return null;
    }
    // return content fragment
    return fragmentResource.adaptTo(ContentFragment.class);
}
Also used : Config(com.adobe.granite.ui.components.Config) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Config

use of com.adobe.granite.ui.components.Config in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ModelElementsDataSourceServlet method doGet.

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) {
    // First try to get the model path from request parameters
    // otherwise determine model path from component resource.
    RequestParameter modelPathRequestParameter = request.getRequestParameter(PARAMETER_AND_PN_MODEL_PATH);
    boolean isOrderBy = request.getResource().isResourceType(RESOURCE_TYPE_ORDER_BY_V1);
    String modelPath;
    if (modelPathRequestParameter != null) {
        modelPath = modelPathRequestParameter.getString();
    } else {
        Config config = getConfig(request);
        ValueMap componentValueMap = getComponentValueMap(config, request);
        // get model path from component resource
        modelPath = componentValueMap != null ? componentValueMap.get(ContentFragmentList.PN_MODEL_PATH, String.class) : null;
    }
    DataSource dataSource = EmptyDataSource.instance();
    if (modelPath != null) {
        ResourceResolver resourceResolver = request.getResourceResolver();
        String pathToCFModelElements = String.format("%s/%s/model/cq:dialog/content/items", modelPath, JcrConstants.JCR_CONTENT);
        Resource cfModelElementRoot = resourceResolver.getResource(pathToCFModelElements);
        if (cfModelElementRoot != null) {
            Iterator<Resource> resourceIterator = cfModelElementRoot.listChildren();
            List<Resource> resourceList = new LinkedList<>();
            if (isOrderBy) {
                resourceList.add(createResource(resourceResolver, "Created", JcrConstants.JCR_CREATED));
                resourceList.add(createResource(resourceResolver, "Last Modified", JcrConstants.JCR_CONTENT + "/" + JcrConstants.JCR_LASTMODIFIED));
            }
            while (resourceIterator.hasNext()) {
                Resource elementResource = resourceIterator.next();
                ValueMap valueMap = elementResource.getValueMap();
                String valueValue = valueMap.get("name", "");
                String textValue = valueMap.get("fieldLabel", valueValue);
                if (isOrderBy && StringUtils.isNotEmpty(valueValue)) {
                    valueValue = "jcr:content/data/master/" + valueValue;
                }
                String metaType = valueMap.get("metaType", StringUtils.EMPTY);
                if (!isOrderBy || StringUtils.startsWith(metaType, "text-")) {
                    Resource syntheticResource = createResource(resourceResolver, textValue, valueValue);
                    resourceList.add(syntheticResource);
                }
            }
            dataSource = new SimpleDataSource(resourceList.iterator());
        }
    }
    request.setAttribute(DataSource.class.getName(), dataSource);
}
Also used : RequestParameter(org.apache.sling.api.request.RequestParameter) Config(com.adobe.granite.ui.components.Config) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) LinkedList(java.util.LinkedList) EmptyDataSource(com.adobe.granite.ui.components.ds.EmptyDataSource) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource)

Example 4 with Config

use of com.adobe.granite.ui.components.Config in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ElementsDataSourceServlet method getItems.

@NotNull
@Override
protected List<ContentElement> getItems(@NotNull ContentFragment fragment, @NotNull SlingHttpServletRequest request) {
    Config config = getConfig(request);
    ValueMap map = getComponentValueMap(config, request);
    String textOnlyParam = request.getParameter(PARAM_AND_PN_DISPLAY_MODE);
    boolean textOnly = map != null && map.containsKey(PARAM_AND_PN_DISPLAY_MODE) && map.get(PARAM_AND_PN_DISPLAY_MODE, "multi").equals(SINGLE_TEXT);
    if (textOnlyParam != null) {
        textOnly = textOnlyParam.equals(SINGLE_TEXT);
    }
    if (textOnly) {
        Iterator<ContentElement> elementIterator = fragment.getElements();
        List<ContentElement> elementList = new ArrayList<ContentElement>();
        while (elementIterator.hasNext()) {
            ContentElement element = elementIterator.next();
            String contentType = element.getValue().getContentType();
            if (contentType == null) {
                contentType = element.getContentType();
            }
            if (contentType != null && contentType.startsWith("text/") && !element.getValue().getDataType().isMultiValue()) {
                elementList.add(element);
            }
        }
        return elementList;
    }
    return IteratorUtils.toList(fragment.getElements());
}
Also used : ContentElement(com.adobe.cq.dam.cfm.ContentElement) Config(com.adobe.granite.ui.components.Config) ValueMap(org.apache.sling.api.resource.ValueMap) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Config (com.adobe.granite.ui.components.Config)4 ValueMap (org.apache.sling.api.resource.ValueMap)4 Resource (org.apache.sling.api.resource.Resource)3 ContentElement (com.adobe.cq.dam.cfm.ContentElement)1 DataSource (com.adobe.granite.ui.components.ds.DataSource)1 EmptyDataSource (com.adobe.granite.ui.components.ds.EmptyDataSource)1 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)1 RenderCondition (com.adobe.granite.ui.components.rendercondition.RenderCondition)1 SimpleRenderCondition (com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 RequestParameter (org.apache.sling.api.request.RequestParameter)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1