Search in sources :

Example 6 with QueryBuilder

use of com.day.cq.search.QueryBuilder 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 7 with QueryBuilder

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

the class ContentFragmentListImpl method initModel.

@PostConstruct
private void initModel() {
    // Default path limits search to DAM
    if (StringUtils.isEmpty(parentPath)) {
        parentPath = DEFAULT_DAM_PARENT_PATH;
    }
    if (StringUtils.isEmpty(modelPath)) {
        LOG.warn("Please provide a model path");
        return;
    }
    Session session = resourceResolver.adaptTo(Session.class);
    if (session == null) {
        LOG.warn("Session was null therefore no query was executed");
        return;
    }
    QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
    if (queryBuilder == null) {
        LOG.warn("Query builder was null therefore no query was executed");
        return;
    }
    Map<String, String> queryParameterMap = new HashMap<>();
    queryParameterMap.put("path", parentPath);
    queryParameterMap.put("type", NT_DAM_ASSET);
    queryParameterMap.put("p.limit", Integer.toString(maxItems));
    queryParameterMap.put("1_property", JcrConstants.JCR_CONTENT + "/data/cq:model");
    queryParameterMap.put("1_property.value", modelPath);
    if (StringUtils.isNotEmpty(orderBy)) {
        queryParameterMap.put("orderby", "@" + orderBy);
        if (StringUtils.isNotEmpty(sortOrder)) {
            queryParameterMap.put("orderby.sort", sortOrder);
        }
    }
    ArrayList<String> allTags = new ArrayList<>();
    if (tagNames != null && tagNames.length > 0) {
        allTags.addAll(Arrays.asList(tagNames));
    }
    if (!allTags.isEmpty()) {
        // Check for the taggable mixin
        queryParameterMap.put("2_property", JcrConstants.JCR_CONTENT + "/metadata/" + JcrConstants.JCR_MIXINTYPES);
        queryParameterMap.put("2_property.value", TagConstants.NT_TAGGABLE);
        // Check for the actual tags (by default, tag are or'ed)
        queryParameterMap.put("tagid.property", JcrConstants.JCR_CONTENT + "/metadata/cq:tags");
        for (int i = 0; i < allTags.size(); i++) {
            queryParameterMap.put(String.format("tagid.%d_value", i + 1), allTags.get(i));
        }
    }
    PredicateGroup predicateGroup = PredicateGroup.create(queryParameterMap);
    Query query = queryBuilder.createQuery(predicateGroup, session);
    SearchResult searchResult = query.getResult();
    LOG.debug("Query statement: '{}'", searchResult.getQueryStatement());
    // 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
        Iterator<Resource> resourceIterator = searchResult.getResources();
        while (resourceIterator.hasNext()) {
            Resource resource = resourceIterator.next();
            if (leakingResourceResolver == null) {
                // Get a reference to QB's leaking resource resolver
                leakingResourceResolver = resource.getResourceResolver();
            }
            DAMContentFragment contentFragmentModel = new DAMContentFragmentImpl(resource, contentTypeConverter, null, elementNames);
            items.add(contentFragmentModel);
        }
    } finally {
        if (leakingResourceResolver != null) {
            // Always close the leaking query builder resource resolver
            leakingResourceResolver.close();
        }
    }
}
Also used : Query(com.day.cq.search.Query) 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) DAMContentFragment(com.adobe.cq.wcm.core.components.models.contentfragment.DAMContentFragment) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Session(javax.jcr.Session) PostConstruct(javax.annotation.PostConstruct)

Aggregations

QueryBuilder (com.day.cq.search.QueryBuilder)7 Query (com.day.cq.search.Query)5 SearchResult (com.day.cq.search.result.SearchResult)4 Session (javax.jcr.Session)4 Resource (org.apache.sling.api.resource.Resource)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)2 PredicateGroup (com.day.cq.search.PredicateGroup)2 HashMap (java.util.HashMap)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 DAMContentFragment (com.adobe.cq.wcm.core.components.models.contentfragment.DAMContentFragment)1 DataSource (com.adobe.granite.ui.components.ds.DataSource)1 EmptyDataSource (com.adobe.granite.ui.components.ds.EmptyDataSource)1 ViewQuery (com.day.cq.wcm.core.contentfinder.ViewQuery)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 PostConstruct (javax.annotation.PostConstruct)1 Node (javax.jcr.Node)1 Bindings (javax.script.Bindings)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1