Skip to content

Commit

Permalink
re Adobe-Consulting-Services#2760 remove CloseableQuery from API
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcin committed Jan 14, 2022
1 parent 2f0047c commit 0457ac2
Show file tree
Hide file tree
Showing 29 changed files with 328 additions and 1,106 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
- #2261 - Update to latest mocking libraries
- #2753 - Update to AEM 6.4 dependencies
- #2754 - Support building with Java 17
- #2760 - Remove CloseableQuery and CloseableQueryBuilder from API

## 5.1.0 - 2021-12-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -19,12 +19,12 @@
*/
package com.adobe.acs.commons.contentfinder.querybuilder.impl.viewhandler;

import com.adobe.acs.commons.search.CloseableQuery;
import com.adobe.acs.commons.search.CloseableQueryBuilder;
import com.adobe.acs.commons.cqsearch.QueryUtil;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.wcm.core.contentfinder.ViewHandler;
import com.day.cq.wcm.core.contentfinder.ViewQuery;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
Expand All @@ -37,7 +37,6 @@
import org.slf4j.LoggerFactory;

import javax.jcr.Session;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -60,11 +59,11 @@ public final class QueryBuilderViewHandler extends ViewHandler {
private static final Logger log = LoggerFactory.getLogger(QueryBuilderViewHandler.class);

@Reference
private transient CloseableQueryBuilder queryBuilder;
private transient QueryBuilder queryBuilder;

@Override
protected ViewQuery createQuery(SlingHttpServletRequest slingRequest, Session session,
String queryString) throws Exception {
String queryString) throws Exception {
Map<String, String> map;

if (GQLToQueryBuilderConverter.convertToQueryBuilder(slingRequest)) {
Expand All @@ -75,11 +74,11 @@ protected ViewQuery createQuery(SlingHttpServletRequest slingRequest, Session se
log.debug("Converted QueryBuilder Parameter Map: {}", map);
}

final CloseableQuery query = queryBuilder.createQuery(PredicateGroup.create(map), session);
final Query query = queryBuilder.createQuery(PredicateGroup.create(map), session);
QueryUtil.setResourceResolverOn(slingRequest.getResourceResolver(), query);
return new QueryBuilderViewQuery(query);
}


/**
* Assume query should be treated as a QueryBuilder query, rather than a GQL query
* <p>
Expand Down Expand Up @@ -113,7 +112,7 @@ private Map<String, String> getQueryBuilderParams(final SlingHttpServletRequest


private Map<String, String> convertToQueryBuilderParams(final SlingHttpServletRequest request,
final String queryString) {
final String queryString) {
Map<String, String> map = new LinkedHashMap<String, String>();

int userDefinedPropertyCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,7 +20,7 @@
package com.adobe.acs.commons.contentfinder.querybuilder.impl.viewhandler;

import com.adobe.acs.commons.contentfinder.querybuilder.impl.ContentFinderHitBuilder;
import com.adobe.acs.commons.search.CloseableQuery;
import com.day.cq.search.Query;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.wcm.core.contentfinder.ViewQuery;
Expand All @@ -36,10 +36,10 @@
public final class QueryBuilderViewQuery implements ViewQuery {
private static final Logger log = LoggerFactory.getLogger(QueryBuilderViewQuery.class);

private final CloseableQuery query;
private final Query query;
private List<com.day.cq.wcm.core.contentfinder.Hit> hits = null;

public QueryBuilderViewQuery(final CloseableQuery query) {
public QueryBuilderViewQuery(final Query query) {
this.query = query;
}

Expand All @@ -52,21 +52,17 @@ public Collection<com.day.cq.wcm.core.contentfinder.Hit> execute() {
return hits;
}

try {
final SearchResult result = this.query.getResult();
final SearchResult result = this.query.getResult();

// iterating over the results
for (Hit hit : result.getHits()) {
try {
hits.add(createHit(hit));
} catch (RepositoryException e) {
log.error("Could not return required information for Content Finder result: {}", hit);
}
// iterating over the results
for (Hit hit : result.getHits()) {
try {
hits.add(createHit(hit));
} catch (RepositoryException e) {
log.error("Could not return required information for Content Finder result: {}", hit);
}

} finally {
query.close();
}

}
return hits;
}
Expand Down
30 changes: 30 additions & 0 deletions bundle/src/main/java/com/adobe/acs/commons/cqsearch/QueryUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.adobe.acs.commons.cqsearch;

import com.day.cq.search.Query;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

/**
* Simple utility to use as an alternative to deprecated CloseableQuery in older versions of AEM.
*/
public class QueryUtil {
private static final Logger log = LoggerFactory.getLogger(QueryUtil.class);

/**
* Uses reflection to forcibly set the Query object's ResourceResolver to the provided.
*/
public static void setResourceResolverOn(ResourceResolver resolver, Query query) {
Class<? extends Query> clazz = query.getClass();
try {
Field resourceResolverField = clazz.getDeclaredField("resourceResolver");
resourceResolverField.setAccessible(true);
resourceResolverField.set(query, resolver);
} catch (NoSuchFieldException | IllegalAccessException e) {
log.debug("Could not set ResourceResolver on provided Query: {} => {}",
e.getClass().getName(), e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* #L%
*/
package com.adobe.acs.commons.search.impl;
package com.adobe.acs.commons.cqsearch.impl;

import com.day.cq.search.Predicate;
import com.day.cq.search.eval.AbstractPredicateEvaluator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
* This package provides Search relation functionality
*/
@Version("1.0.0")
package com.adobe.acs.commons.search;
package com.adobe.acs.commons.cqsearch;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
package com.adobe.acs.commons.dam.impl;


import com.adobe.acs.commons.search.CloseableQuery;
import com.adobe.acs.commons.search.CloseableQueryBuilder;
import com.adobe.acs.commons.cqsearch.QueryUtil;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetManager;
import com.adobe.granite.asset.api.AssetVersionManager;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.dam.api.DamConstants;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
Expand Down Expand Up @@ -127,7 +128,7 @@ public class ReviewTaskAssetMoverHandler implements EventHandler {
private Scheduler scheduler;

@Reference
private CloseableQueryBuilder queryBuilder;
private QueryBuilder queryBuilder;

private static final String DEFAULT_DEFAULT_CONFLICT_RESOLUTION = CONFLICT_RESOLUTION_NEW_VERSION;
private String defaultConflictResolution = DEFAULT_DEFAULT_CONFLICT_RESOLUTION;
Expand Down Expand Up @@ -159,7 +160,7 @@ protected void activate(Map<String, Object> config) {
@Override
public void handleEvent(Event event) {

try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO)){
try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO)) {
final String path = (String) event.getProperty("TaskId");
final Resource taskResource = resourceResolver.getResource(path);

Expand Down Expand Up @@ -195,7 +196,7 @@ public ImmediateJob(String path) {

@Override
public void run() {
try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO)){
try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO)) {

// Access data passed into the Job from the Event
Resource resource = resourceResolver.getResource(path);
Expand All @@ -206,18 +207,17 @@ public void run() {
String contentPath = taskProperties.get(PN_CONTENT_PATH, String.class);

if (StringUtils.startsWith(contentPath, PATH_CONTENT_DAM)) {
try (CloseableQuery query = findAssets(resourceResolver, contentPath)) {
log.debug("Found [ {} ] assets under [ {} ] that were reviewed and require processing.",
query.getResult().getHits().size(),
contentPath);
Query query = findAssets(resourceResolver, contentPath);
log.debug("Found [ {} ] assets under [ {} ] that were reviewed and require processing.",
query.getResult().getHits().size(),
contentPath);

final Iterator<Resource> assets = query.getResult().getResources();
resourceResolver.adaptTo(Session.class).getWorkspace().getObservationManager().setUserData(USER_EVENT_TYPE);
final Iterator<Resource> assets = query.getResult().getResources();
resourceResolver.adaptTo(Session.class).getWorkspace().getObservationManager().setUserData(USER_EVENT_TYPE);

while (assets.hasNext()) {
final Asset asset = assetManager.getAsset(assets.next().getPath());
moveAsset(resourceResolver, assetManager, asset, taskProperties);
}
while (assets.hasNext()) {
final Asset asset = assetManager.getAsset(assets.next().getPath());
moveAsset(resourceResolver, assetManager, asset, taskProperties);
}
}
}
Expand All @@ -233,7 +233,7 @@ public void run() {
* @param contentPath the DAM contentPath which the task covers.
* @return the CloseableQuery whose result represents dam:Assets for which dam:status is set to approved or rejected
*/
private CloseableQuery findAssets(ResourceResolver resourceResolver, String contentPath) {
private Query findAssets(ResourceResolver resourceResolver, String contentPath) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", DamConstants.NT_DAM_ASSET);
params.put("path", contentPath);
Expand All @@ -243,7 +243,9 @@ private CloseableQuery findAssets(ResourceResolver resourceResolver, String cont
params.put("p.offset", "0");
params.put("p.limit", "-1");

return queryBuilder.createQuery(PredicateGroup.create(params), resourceResolver);
Query query = queryBuilder.createQuery(PredicateGroup.create(params), resourceResolver.adaptTo(Session.class));
QueryUtil.setResourceResolverOn(resourceResolver, query);
return query;
}


Expand Down Expand Up @@ -283,7 +285,7 @@ private String createUniqueAssetPath(AssetManager assetManager, String destPath,
* Creates a new revision of an asset and replaces its renditions (including original), and metadata node.
*
* @param resourceResolver the ResourceResolver object
* @param assetManager the AssetManager object
* @param assetManager the AssetManager object
* @param originalAsset the asset to create a new version for
* @param reviewedAsset the asset to that will represent the new version
* @throws PersistenceException
Expand Down
Loading

0 comments on commit 0457ac2

Please sign in to comment.