Search in sources :

Example 26 with DataSource

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

the class AbstractContentFragmentDataSourceServlet method doGet.

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
    // return empty datasource by default
    DataSource elements = EmptyDataSource.instance();
    // get content fragment
    ContentFragment fragment = getContentFragment(request);
    if (fragment != null) {
        // get datasource items from the implementation
        List<T> items = getItems(fragment, request);
        // transform items to resources
        List<Resource> resources = new LinkedList<>();
        ResourceResolver resolver = request.getResourceResolver();
        for (T item : items) {
            Resource resource = createResource(resolver, getTitle(item), getValue(item));
            resources.add(resource);
        }
        // create datasource
        elements = new SimpleDataSource(resources.iterator());
    }
    // provide datasource
    request.setAttribute(DataSource.class.getName(), elements);
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) 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 27 with DataSource

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

the class ModelDataSourceServlet method doGet.

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
    DataSource dataSource = EmptyDataSource.instance();
    ResourceResolver resourceResolver = request.getResourceResolver();
    QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
    if (queryBuilder != null) {
        Map<String, String> parameterMap = new HashMap<>();
        parameterMap.put("path", "/conf");
        parameterMap.put("type", NT_TEMPLATE);
        parameterMap.put("p.limit", "-1");
        parameterMap.put("1_property", JcrConstants.JCR_CONTENT + "/model/" + ResourceResolver.PROPERTY_RESOURCE_TYPE);
        parameterMap.put("1_property.value", "wcm/scaffolding/components/scaffolding");
        PredicateGroup predicateGroup = PredicateGroup.create(parameterMap);
        Session session = resourceResolver.adaptTo(Session.class);
        Query query = queryBuilder.createQuery(predicateGroup, session);
        SearchResult searchResult = query.getResult();
        // Query builder has a leaking resource resolver, so the following work around is required.
        ResourceResolver leakingResourceResolver = null;
        try {
            // Iterate over the hits if you need special information
            List<Resource> resources = new LinkedList<>();
            for (Iterator<Resource> resourceIterator = searchResult.getResources(); resourceIterator.hasNext(); ) {
                Resource resource = resourceIterator.next();
                if (leakingResourceResolver == null) {
                    // Get a reference to query builder's leaking resource resolver
                    leakingResourceResolver = resource.getResourceResolver();
                }
                ValueMap modelValueMap = resource.getValueMap();
                String modelTitle = modelValueMap.get(JcrConstants.JCR_CONTENT + "/" + JcrConstants.JCR_TITLE, resource.getName());
                String modelPath = resource.getPath();
                Resource syntheticResource = createResource(resourceResolver, modelTitle, modelPath);
                resources.add(syntheticResource);
            }
            dataSource = new SimpleDataSource(resources.iterator());
        } finally {
            if (leakingResourceResolver != null) {
                // Always close the leaking query builder resource resolver.
                leakingResourceResolver.close();
            }
        }
    }
    request.setAttribute(DataSource.class.getName(), dataSource);
}
Also used : Query(com.day.cq.search.Query) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder) PredicateGroup(com.day.cq.search.PredicateGroup) LinkedList(java.util.LinkedList) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) EmptyDataSource(com.adobe.granite.ui.components.ds.EmptyDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Session(javax.jcr.Session)

Example 28 with DataSource

use of com.adobe.granite.ui.components.ds.DataSource 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 29 with DataSource

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

the class OptionsImpl method populateOptionItems.

/* ------------------------ Internal stuff -------------------------------------------- */
private void populateOptionItems() {
    this.optionItems = new ArrayList<>();
    Source source = Source.getSource(sourceString);
    if (source == null) {
        populateOptionItemsFromLocal();
    } else {
        switch(source) {
            case DATASOURCE:
                populateOptionItemsFromDatasource();
                break;
            case LIST:
                populateOptionItemsFromList();
                break;
            default:
                populateOptionItemsFromLocal();
        }
    }
}
Also used : SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource)

Example 30 with DataSource

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

the class AllowedColorSwatchesDataSourceServletTest method testDataSource.

@Test
public void testDataSource() throws Exception {
    AllowedColorSwatchesDataSourceServlet dataSourceServlet = new AllowedColorSwatchesDataSourceServlet();
    String[] expected = new String[] { COLOR_VALUE_1, COLOR_VALUE_2, COLOR_VALUE_3 };
    context.build().resource("/content", ImmutableMap.of("sling:resourceType", "/restype")).commit();
    context.contentPolicyMapping("/restype", ImmutableMap.of(AllowedColorSwatchesDataSourceServlet.PN_ALLOWED_COLOR_SWATCHES, expected));
    context.request().setAttribute(Value.CONTENTPATH_ATTRIBUTE, "/content");
    dataSourceServlet.doGet(context.request(), context.response());
    DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName());
    Assertions.assertNotNull(dataSource);
    dataSource.iterator().forEachRemaining(resource -> {
        ValueMap props = resource.getValueMap();
        String allowedColorSwatch = props.get(AllowedColorSwatchesDataSourceServlet.PN_COLOR_VALUE, String.class);
        Assertions.assertTrue(Arrays.asList(expected).contains(allowedColorSwatch), "Allowed color swatches values are not as expected");
    });
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) DataSource(com.adobe.granite.ui.components.ds.DataSource) Test(org.junit.jupiter.api.Test)

Aggregations

DataSource (com.adobe.granite.ui.components.ds.DataSource)48 Test (org.junit.jupiter.api.Test)37 Resource (org.apache.sling.api.resource.Resource)14 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)10 ValueMap (org.apache.sling.api.resource.ValueMap)6 EmptyDataSource (com.adobe.granite.ui.components.ds.EmptyDataSource)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 LinkedList (java.util.LinkedList)3 ValueMapResource (com.adobe.granite.ui.components.ds.ValueMapResource)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 DataSourceOption (com.adobe.acs.commons.wcm.datasources.DataSourceOption)1 ContentFragment (com.adobe.cq.dam.cfm.ContentFragment)1 CoreComponentTestContext (com.adobe.cq.wcm.core.components.context.CoreComponentTestContext)1 TextValueDataResourceSource (com.adobe.cq.wcm.core.components.internal.servlets.TextValueDataResourceSource)1 EmbeddableDescription (com.adobe.cq.wcm.core.components.internal.servlets.embed.EmbeddablesDataSourceServlet.EmbeddableDescription)1 MockStyle (com.adobe.cq.wcm.core.components.testing.MockStyle)1 Config (com.adobe.granite.ui.components.Config)1 Value (com.adobe.granite.ui.components.Value)1