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);
}
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();
}
}
}
Aggregations