Java Code Examples for org.apache.sling.api.resource.Resource#listChildren()

The following examples show how to use org.apache.sling.api.resource.Resource#listChildren() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProductBindingCreator.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
private void processDeletion(ResourceChange change) {
    String path = change.getPath();
    LOG.debug("Process resource deletion at path {}", path);

    Resource parent = resolver.getResource(BINDINGS_PARENT_PATH);
    Iterator<Resource> resourceIterator = parent.listChildren();

    Stream<Resource> targetStream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(resourceIterator, Spliterator.ORDERED),
        false);

    targetStream.filter(res -> {
        ValueMap properties = res.getValueMap();
        LOG.debug("Checking the binding at {}", res.getPath());
        String cqConf = properties.get(Constants.PN_CONF, "");
        if (StringUtils.isEmpty(cqConf)) {
            return false;
        }
        return path.equals(cqConf + "/" + Constants.COMMERCE_BUCKET_PATH);
    }).findFirst().ifPresent(res -> {
        LOG.debug("Found a binding at {} that uses {}, we'll delete it", res.getPath(), path);
        deleteJcrNode(res);
    });
}
 
Example 2
Source File: UrlProviderImpl.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
/**
 * This method checks if any of the children of the given <code>page</code> resource
 * is a page with a <code>selectorFilter</code> property set with the value
 * of the given <code>selector</code>.
 * 
 * @param page The page resource, from where children pages will be checked.
 * @param selector The searched value for the <code>selectorFilter</code> property.
 * @return If found, a child page resource that contains the given <code>selectorFilter</code> value.
 *         If not found, this method returns null.
 */
public static Resource toSpecificPage(Resource page, String selector) {
    Iterator<Resource> children = page.listChildren();
    while (children.hasNext()) {
        Resource child = children.next();
        if (!NameConstants.NT_PAGE.equals(child.getResourceType())) {
            continue;
        }

        Resource jcrContent = child.getChild(JcrConstants.JCR_CONTENT);
        if (jcrContent == null) {
            continue;
        }

        Object filter = jcrContent.getValueMap().get(SELECTOR_FILTER_PROPERTY);
        if (filter == null) {
            continue;
        }

        // The property is saved as a String when it's a simple selection, or an array when a multi-selection is done
        String[] selectors = filter.getClass().isArray() ? ((String[]) filter) : ArrayUtils.toArray((String) filter);

        if (ArrayUtils.contains(selectors, selector)) {
            LOGGER.debug("Page has a matching sub-page for selector {} at {}", selector, child.getPath());
            return child;
        }
    }
    return null;
}
 
Example 3
Source File: ChildResourceInjector.java    From sling-org-apache-sling-models-impl with Apache License 2.0 5 votes vote down vote up
private Object getResultList(Resource resource, Type declaredType) {
    List<Resource> result = new ArrayList<>();
    Class<?> type = getActualType((ParameterizedType) declaredType);
    if (type != null && resource != null) {
        Iterator<Resource> children = resource.listChildren();
        while (children.hasNext()) {
            result.add(children.next());
        }
    }
    return result;
}
 
Example 4
Source File: CommentServiceImpl.java    From publick-sling-blog with Apache License 2.0 5 votes vote down vote up
/**
 * Get the number of replies for a given comment.
 *
 * @param comment The current comment
 * @return The number of replies for the comment
 */
public int numberOfReplies(final Resource comment) {
    final Iterator<Resource> children = comment.listChildren();
    int size = 0;

    while (children.hasNext()) {
        children.next();
        size++;
    }

    return size;
}
 
Example 5
Source File: CommentsView.java    From publick-sling-blog with Apache License 2.0 5 votes vote down vote up
/**
 * Create the comment list by getting the passed in resource's children and collecting
 * their author, comment, and jcr:created properties. Replies are a recursive call to
 * this methods with the children of the resource passed in resource.
 *
 * @param resource The root comments resource or the first level comment.
 * @param getReplies True to get the next level of comments.
 * @return The collection of comments.
 */
private List<HashMap<String, Object>> getCommentList(Resource resource, boolean getReplies) {
    List<HashMap<String, Object>> comments = new ArrayList<HashMap<String, Object>>();

    if (resource != null && resource.hasChildren()) {
        Iterator<Resource> iterator = resource.listChildren();

        while (iterator.hasNext()) {

            Resource commentResource = iterator.next();
            ValueMap properties = commentResource.adaptTo(ValueMap.class);
            HashMap<String, Object> commentProperties = new HashMap<String, Object>();

            String author = properties.get("author", String.class);
            String comment = properties.get("comment", String.class);
            String date = getDate(properties.get(JcrConstants.JCR_CREATED, Date.class), DISPLAY_DATE_FORMAT);
            boolean display = properties.get("display", false);
            boolean edited = properties.get("edited", false);
            boolean spam = properties.get("spam", false);

            if (StringUtils.isNotBlank(author)
                    && StringUtils.isNotBlank(comment)
                    && StringUtils.isNotBlank(date)) {
                commentProperties.put("author", display && !spam ? author : "--");
                commentProperties.put("comment", display && !spam ? comment : "Comment removed by author.");
                commentProperties.put("date", date);
                commentProperties.put("edited", edited);
                commentProperties.put("path", commentResource.getPath());
                if (getReplies) {
                    commentProperties.put("replies", getCommentList(commentResource, false));
                }
                comments.add(commentProperties);
                count++;
            }
        }
    }

    return comments;
}
 
Example 6
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testPredicateResourceWrapper() {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    Resource product = mockProductResource(false);
    children.add(product);
    children.add(mockFolderResource());
    List<Resource> productChildren = new ArrayList<>();
    Resource someResource = mock(Resource.class);
    when(someResource.getValueMap()).thenReturn(new ValueMapDecorator(new HashMap<>()));
    when(someResource.getName()).thenReturn("something");
    productChildren.add(someResource);
    productChildren.add(mockVariantResource("variant1"));
    Resource variant2 = mockVariantResource("variant2");
    productChildren.add(variant2);
    productChildren.add(mockVariantResource("variant3"));
    productChildren.add(mockFolderResource("folder1"));
    when(product.listChildren()).thenAnswer(invocation -> productChildren.iterator());
    when(product.hasChildren()).thenAnswer(invocation -> !productChildren.isEmpty());
    when(product.getChild("variant2")).thenReturn(variant2);
    when(product.getChild("something")).thenReturn(someResource);

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
    Resource productResource = null;
    for (Iterator<Resource> it = dataSource.iterator(); it.hasNext();) {
        Resource r = it.next();
        if ("commerce/components/product".equals(r.getResourceType())) {
            productResource = r;
            break;
        }
    }
    assertNotNull(productResource);
    assertTrue(productResource.hasChildren());
    assertTrue(productResource.listChildren().hasNext());
    assertEquals("variant1", productResource.listChildren().next().getName());
    assertEquals("variant2", productResource.getChild("variant2").getName());
    assertNull(productResource.getChild("something"));

    int childCount = 0;
    int variantCount = 0;

    for (Iterator it = productResource.listChildren(); it.hasNext();) {
        Resource resource = (Resource) it.next();
        if ("commerce/components/product".equals(resource.getResourceType())) {
            assertEquals("variant", resource.getValueMap().get("cq:commerceType"));
            variantCount++;
        }
        childCount++;
    }
    assertEquals(4, childCount);
    assertEquals(3, variantCount);
}
 
Example 7
Source File: ResourceResolverImpl.java    From jackalope with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<Resource> listChildren(Resource parent) {
    return parent.listChildren();
}