Search in sources :

Example 1 with Query

use of com.day.cq.search.Query in project acs-aem-commons by Adobe-Consulting-Services.

the class OnDeployScriptBase method searchAndUpdateResourceType.

/**
 * Searches for the current sling:resourceType under /content and replaces any nodes it finds
 * with the newResourceType.
 *
 * @param oldResourceType The current sling:resourceType.
 * @param newResourceType The new sling:resourceType to be used.
 */
protected final void searchAndUpdateResourceType(String oldResourceType, String newResourceType) throws RepositoryException {
    Map<String, String> map = new HashMap<>();
    map.put("p.limit", "-1");
    map.put("path", "/content");
    map.put("1_property", SLING_RESOURCE_TYPE);
    map.put("1_property.value", oldResourceType);
    logger.info("Finding all nodes under /content with resource type: {}", oldResourceType);
    Query query = resourceResolver.adaptTo(QueryBuilder.class).createQuery(PredicateGroup.create(map), session);
    SearchResult result = query.getResult();
    Iterator<Node> nodeItr = result.getNodes();
    if (nodeItr.hasNext()) {
        while (nodeItr.hasNext()) {
            Node node = nodeItr.next();
            updateResourceType(node, newResourceType);
        }
    } else {
        logger.info("No nodes found with resource type: {}", oldResourceType);
    }
}
Also used : Query(com.day.cq.search.Query) HashMap(java.util.HashMap) Node(javax.jcr.Node) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder)

Example 2 with Query

use of com.day.cq.search.Query in project acs-aem-commons by Adobe-Consulting-Services.

the class TwitterFeedUpdaterImpl method findTwitterResources.

private List<Resource> findTwitterResources(ResourceResolver resourceResolver) {
    List<Resource> twitterResources = new ArrayList<>();
    Map<String, String> predicateMap = new HashMap<>();
    predicateMap.put("path", "/content");
    predicateMap.put("property", "sling:resourceType");
    int counter = 1;
    for (String path : twitterComponentPaths) {
        predicateMap.put("property." + (counter++) + "_value", path);
    }
    predicateMap.put("p.limit", "-1");
    QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
    Session session = resourceResolver.adaptTo(Session.class);
    Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap), session);
    SearchResult result = query.getResult();
    Iterator<Resource> resources = result.getResources();
    while (resources.hasNext()) {
        twitterResources.add(resources.next());
    }
    return twitterResources;
}
Also used : Query(com.day.cq.search.Query) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder) Session(javax.jcr.Session)

Example 3 with Query

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

the class ModelDataSourceServletTest method verifyDataSource.

@Test
public void verifyDataSource() throws Exception {
    // GIVEN
    context.load().json(getClass().getResourceAsStream("test-content.json"), "/conf/foobar/settings/dam/cfm/models/yetanothercfmodel");
    ResourceResolver resourceResolver = Mockito.spy(context.resourceResolver());
    Resource resource = resourceResolver.getResource("/conf/foobar/settings/dam/cfm/models/yetanothercfmodel");
    Resource leakingResource = Mockito.spy(resource);
    QueryBuilder queryBuilder = Mockito.mock(QueryBuilder.class);
    Query query = Mockito.mock(Query.class);
    SearchResult searchResult = Mockito.mock(SearchResult.class);
    Iterator<Resource> iterator = Mockito.mock(Iterator.class);
    ResourceResolver leakingResourceResolver = Mockito.mock(ResourceResolver.class);
    Mockito.doReturn(leakingResourceResolver).when(leakingResource).getResourceResolver();
    Mockito.doReturn(queryBuilder).when(resourceResolver).adaptTo(Mockito.eq(QueryBuilder.class));
    Mockito.when(queryBuilder.createQuery(Mockito.any(), Mockito.any())).thenReturn(query);
    Mockito.when(query.getResult()).thenReturn(searchResult);
    Mockito.when(searchResult.getResources()).thenReturn(iterator);
    Mockito.when(iterator.hasNext()).thenReturn(true, false);
    Mockito.when(iterator.next()).thenReturn(leakingResource);
    SlingHttpServletRequest request = new MockSlingHttpServletRequest(resourceResolver, context.bundleContext());
    // WHEN
    modelDatasourceServlet.doGet(request, context.response());
    // THEN
    SimpleDataSource simpleDataSource = (SimpleDataSource) request.getAttribute(DataSource.class.getName());
    assertThat(simpleDataSource.iterator().hasNext(), CoreMatchers.is(true));
    ValueMap valueMap = simpleDataSource.iterator().next().getValueMap();
    assertThat(valueMap.get("text"), CoreMatchers.is("YetAnotherCFModel"));
    assertThat(valueMap.get("value"), CoreMatchers.is("/conf/foobar/settings/dam/cfm/models/yetanothercfmodel"));
}
Also used : Query(com.day.cq.search.Query) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) 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) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 4 with Query

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

the class ContentFragmentListImplTest method testListWithOneFragment.

@SuppressWarnings("unchecked")
private void testListWithOneFragment(String listName) {
    Resource resource = context.resourceResolver().getResource("/content/dam/contentfragments-for-list/text-only");
    if (resource != null) {
        Resource DAMFragment = Mockito.spy(resource);
        Query query = Mockito.mock(Query.class);
        SearchResult searchResult = Mockito.mock(SearchResult.class);
        Iterator<Resource> iterator = Mockito.mock(Iterator.class);
        ResourceResolver spyResolver = Mockito.spy(DAMFragment.getResourceResolver());
        when(query.getResult()).thenReturn(searchResult);
        when(searchResult.getResources()).thenReturn(iterator);
        when(iterator.hasNext()).thenReturn(true, false);
        when(iterator.next()).thenReturn(DAMFragment);
        when(DAMFragment.getResourceResolver()).thenReturn(spyResolver);
        Mockito.doNothing().when(spyResolver).close();
        when(queryBuilderMock.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class))).thenReturn(query);
        ContentFragmentList contentFragmentList = getModelInstanceUnderTest(listName);
        assertEquals(ContentFragmentListImpl.RESOURCE_TYPE_V1, contentFragmentList.getExportedType());
        assertEquals(contentFragmentList.getListItems().size(), 1);
        Utils.testJSONExport(contentFragmentList, Utils.getTestExporterJSONPath(TEST_BASE, listName));
        Mockito.doCallRealMethod().when(spyResolver).close();
    }
}
Also used : Query(com.day.cq.search.Query) ContentFragmentList(com.adobe.cq.wcm.core.components.models.contentfragment.ContentFragmentList) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SearchResult(com.day.cq.search.result.SearchResult) PredicateGroup(com.day.cq.search.PredicateGroup) Session(javax.jcr.Session)

Example 5 with Query

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

the class ContentFragmentListImplTest method setUp.

@BeforeEach
@SuppressWarnings("unchecked")
void setUp() throws NoSuchFieldException, IllegalAccessException {
    super.setUp();
    // Load additional content for content list model
    context.load().json(TEST_BASE + "/test-content.json", "/content/tests");
    context.load().json("/contentfragmentlist/test-content-dam-contentfragments.json", "/content/dam/contentfragments-for-list");
    Query query = Mockito.mock(Query.class);
    SearchResult searchResult = Mockito.mock(SearchResult.class);
    Iterator<Resource> iterator = Mockito.mock(Iterator.class);
    Resource resource = Mockito.mock(Resource.class);
    leakingResourceResolverMock = Mockito.mock(ResourceResolver.class);
    when(query.getResult()).thenReturn(searchResult);
    when(searchResult.getResources()).thenReturn(iterator);
    when(iterator.hasNext()).thenReturn(true, false);
    when(iterator.next()).thenReturn(resource);
    when(resource.getResourceResolver()).thenReturn(leakingResourceResolverMock);
    when(queryBuilderMock.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class))).thenReturn(query);
}
Also used : Query(com.day.cq.search.Query) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SearchResult(com.day.cq.search.result.SearchResult) PredicateGroup(com.day.cq.search.PredicateGroup) Session(javax.jcr.Session) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Query (com.day.cq.search.Query)14 SearchResult (com.day.cq.search.result.SearchResult)12 Session (javax.jcr.Session)10 Resource (org.apache.sling.api.resource.Resource)10 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)9 PredicateGroup (com.day.cq.search.PredicateGroup)8 QueryBuilder (com.day.cq.search.QueryBuilder)8 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 ListItem (com.adobe.cq.wcm.core.components.models.ListItem)2 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)2 PathPredicateEvaluator (com.day.cq.search.eval.PathPredicateEvaluator)2 Hit (com.day.cq.search.result.Hit)2 LiveRelationshipManager (com.day.cq.wcm.msm.api.LiveRelationshipManager)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2