Search in sources :

Example 1 with DAMContentFragment

use of com.adobe.cq.wcm.core.components.models.contentfragment.DAMContentFragment 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

DAMContentFragment (com.adobe.cq.wcm.core.components.models.contentfragment.DAMContentFragment)1 PredicateGroup (com.day.cq.search.PredicateGroup)1 Query (com.day.cq.search.Query)1 QueryBuilder (com.day.cq.search.QueryBuilder)1 SearchResult (com.day.cq.search.result.SearchResult)1 PostConstruct (javax.annotation.PostConstruct)1 Session (javax.jcr.Session)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1