Search in sources :

Example 46 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 47 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 48 with DataSource

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

the class WorkflowModelDataSourceServlet method doGet.

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
    try {
        WorkflowSession workflowSession = request.getResourceResolver().adaptTo(WorkflowSession.class);
        ArrayList<Resource> resources = new ArrayList<>();
        if (workflowSession != null) {
            WorkflowModel[] models = workflowSession.getModels();
            for (WorkflowModel model : models) {
                resources.add(new WorkflowModelResource(model, request.getResourceResolver()));
            }
        }
        SimpleDataSource dataSource = new SimpleDataSource(resources.iterator());
        request.setAttribute(DataSource.class.getName(), dataSource);
    } catch (WorkflowException e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) WorkflowSession(com.adobe.granite.workflow.WorkflowSession) WorkflowException(com.adobe.granite.workflow.WorkflowException) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) WorkflowModel(com.adobe.granite.workflow.model.WorkflowModel) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource)

Example 49 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 50 with DataSource

use of com.adobe.granite.ui.components.ds.DataSource in project APM by Cognifide.

the class ChildrenDatasourceServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    Iterator<Resource> iterator = Optional.ofNullable(request.getResource()).flatMap(parent -> Optional.ofNullable(parent.getChild("items"))).map(items -> items.listChildren()).orElse(Collections.emptyIterator());
    DataSource dataSource = new SimpleDataSource(iterator);
    request.setAttribute(DataSource.class.getName(), dataSource);
}
Also used : Component(org.osgi.service.component.annotations.Component) DataSource(com.adobe.granite.ui.components.ds.DataSource) Iterator(java.util.Iterator) Property(com.cognifide.apm.core.Property) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) Servlet(javax.servlet.Servlet) Optional(java.util.Optional) Resource(org.apache.sling.api.resource.Resource) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Collections(java.util.Collections) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) Resource(org.apache.sling.api.resource.Resource) DataSource(com.adobe.granite.ui.components.ds.DataSource) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource)

Aggregations

DataSource (com.adobe.granite.ui.components.ds.DataSource)51 Test (org.junit.jupiter.api.Test)37 Resource (org.apache.sling.api.resource.Resource)17 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)13 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 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 ValueMapResource (com.adobe.granite.ui.components.ds.ValueMapResource)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)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