Java Code Examples for org.apache.sling.api.resource.Resource#getChild()
The following examples show how to use
org.apache.sling.api.resource.Resource#getChild() .
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: RatingsServiceImpl.java From sling-samples with Apache License 2.0 | 7 votes |
/** * @see org.apache.sling.sample.slingshot.ratings.RatingsService#getRating(org.apache.sling.api.resource.Resource) */ @Override public double getRating(final Resource resource) { final String fullPath = getRatingsResourcePath(resource); float rating = 0; if ( fullPath != null ) { final Resource ratingsResource = resource.getChild(fullPath); if ( ratingsResource != null ) { int count = 0; for(final Resource r : ratingsResource.getChildren()) { final ValueMap vm = r.getValueMap(); final double current = vm.get(RatingsUtil.PROPERTY_RATING, 0.0); rating += current; count++; } if ( count > 0 ) { rating = rating / count; } } } return rating; }
Example 2
Source File: ChildResourceViaProvider.java From sling-org-apache-sling-models-impl with Apache License 2.0 | 6 votes |
@Override public Object getAdaptable(Object original, String value) { if (StringUtils.isBlank(value)) { return ORIGINAL; } if (original instanceof Resource) { return ((Resource) original).getChild(value); } else if (original instanceof SlingHttpServletRequest) { final SlingHttpServletRequest request = (SlingHttpServletRequest) original; final Resource resource = request.getResource(); if (resource == null) { return null; } Resource child = resource.getChild(value); if (child == null) { log.debug("Could not obtain child {} of resource {}", value, resource.getPath()); return null; } return new ChildResourceRequestWrapper(request, child); } else { log.warn("Received unexpected adaptable of type {}.", original.getClass().getName()); return null; } }
Example 3
Source File: NPEfix19_nineteen_s.java From coming with MIT License | 6 votes |
private boolean isJcrData(Resource resource){ boolean jcrData = false; if (resource!= null) { ValueMap props = resource.adaptTo(ValueMap.class); if (props.containsKey(PROP_JCR_DATA) ) { jcrData = true; } else { Resource jcrContent = resource.getChild(JCR_CONTENT_LEAF); if (jcrContent!= null) { props = jcrContent.adaptTo(ValueMap.class); if (props.containsKey(PROP_JCR_DATA) ) { jcrData = true; } } } } return jcrData; }
Example 4
Source File: NPEfix19_nineteen_t.java From coming with MIT License | 6 votes |
private boolean isJcrData(Resource resource){ boolean jcrData = false; if (resource!= null) { ValueMap props = resource.adaptTo(ValueMap.class); if (props != null && props.containsKey(PROP_JCR_DATA) ) { jcrData = true; } else { Resource jcrContent = resource.getChild(JCR_CONTENT_LEAF); if (jcrContent!= null) { props = jcrContent.adaptTo(ValueMap.class); if (props != null && props.containsKey(PROP_JCR_DATA) ) { jcrData = true; } } } } return jcrData; }
Example 5
Source File: UrlProviderImpl.java From aem-core-cif-components with Apache License 2.0 | 5 votes |
/** * 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 6
Source File: MapOfChildResourcesInjector.java From sling-whiteboard with Apache License 2.0 | 5 votes |
@Override public Object getValue(@NotNull Object adaptable, String name, @NotNull Type declaredType, @NotNull AnnotatedElement element, @NotNull DisposalCallbackRegistry callbackRegistry) { if (adaptable instanceof Resource) { boolean directDescendants = element.getAnnotation(DirectDescendants.class) != null; Resource source = ((Resource) adaptable); if (!directDescendants) { source = source.getChild(name); } return createMap(source != null ? source.getChildren() : Collections.EMPTY_LIST, declaredType); } else if (adaptable instanceof SlingHttpServletRequest) { return getValue(((SlingHttpServletRequest) adaptable).getResource(), name, declaredType, element, callbackRegistry); } return null; }
Example 7
Source File: ProductBindingCreator.java From commerce-cif-connector with Apache License 2.0 | 4 votes |
private void processAddition(ResourceChange change) { resolver.refresh(); String path = change.getPath(); LOG.debug("Process resource addition at path {}", path); Resource changedResource = resolver.getResource(path); if (changedResource == null) { LOG.warn("Cannot retrieve resource at {}. Does the user have the require privileges?", path); return; } Resource contentResource = changedResource.getChild(JcrConstants.JCR_CONTENT); ValueMap properties; if (contentResource != null) { properties = contentResource.getValueMap(); } else { properties = changedResource.getValueMap(); } String storeView = properties.get(Constants.PN_MAGENTO_STORE, ""); if (StringUtils.isEmpty(storeView)) { LOG.warn("The configuration at path {} doesn't have a '{}' property", path, Constants.PN_MAGENTO_STORE); return; } String configRoot = path.substring(0, path.indexOf(Constants.COMMERCE_BUCKET_PATH) - 1); String configName = configRoot.substring(configRoot.lastIndexOf("/") + 1); String bindingName = configName + "-" + storeView; LOG.debug("New binding name: {}", bindingName); Map<String, Object> mappingProperties = new HashMap<>(); mappingProperties.put(JcrConstants.JCR_PRIMARYTYPE, "sling:Folder"); mappingProperties.put(JcrConstants.JCR_TITLE, bindingName); mappingProperties.put(Constants.PN_CONF, configRoot); Resource parent = resolver.getResource(BINDINGS_PARENT_PATH); if (parent == null) { LOG.warn("Binding parent path not found at {}. Nothing to do here...", BINDINGS_PARENT_PATH); return; } String bindingPath = parent.getPath() + "/" + bindingName; LOG.debug("Check if we already have a binding at {}", bindingPath); try { LOG.debug("Creating a new resource at {}", bindingPath); ResourceUtil.getOrCreateResource(resolver, bindingPath, mappingProperties, "", true); if (contentResource != null) { LOG.debug("Adding {} property at {}", Constants.PN_CATALOG_PATH, contentResource.getPath()); ModifiableValueMap map = contentResource.adaptTo(ModifiableValueMap.class); map.put(Constants.PN_CATALOG_PATH, bindingPath); resolver.commit(); } } catch (PersistenceException e) { LOG.error(e.getMessage(), e); } }
Example 8
Source File: ConfigurationColumnViewItem.java From commerce-cif-connector with Apache License 2.0 | 4 votes |
private boolean hasOnlyChild(Resource resource, String child) { return getChildCount(resource) == 1 && resource.getChild(child) != null; }
Example 9
Source File: MultiFieldDropTargetPostProcessor.java From commerce-cif-connector with Apache License 2.0 | 4 votes |
private void processProperty(Resource resource, String target, String propertyValue, String originalKey, String selectionType, boolean multiple) throws Exception { String[] paths = target.split(SLASH); ResourceResolver resourceResolver = resource.getResourceResolver(); // clean-up the dropTarget property or node ModifiableValueMap originalProperties = resource.adaptTo(ModifiableValueMap.class); originalProperties.remove(originalKey); String dropTargetNodeName = DROP_TARGET_PREFIX.replace(SLING_PROPERTY_PREFIX, StringUtils.EMPTY); Resource dropTargetResource = resource.getChild(dropTargetNodeName); if (dropTargetResource != null) { resourceResolver.delete(dropTargetResource); } if (selectionType != null) { if (SKU.equals(selectionType)) { propertyValue = StringUtils.substringAfterLast(propertyValue, "/"); } else if (SLUG.equals(selectionType)) { Resource product = resourceResolver.getResource(propertyValue); if (product != null) { String slug = product.getValueMap().get(SLUG, String.class); if (slug != null) { propertyValue = slug; } } } } // check all paths and create correct resources and properties boolean isArray = true; Resource currentResource = resource; for (String path : paths) { if (path.startsWith(PROPERTY_PREFIX)) { // this is the property String propertyName = path.replace(PROPERTY_PREFIX, StringUtils.EMPTY); ModifiableValueMap properties = currentResource.adaptTo(ModifiableValueMap.class); if (isArray && multiple) { List<String> childPages = new ArrayList<>(Arrays.asList(properties.get(propertyName, new String[0]))); childPages.add(propertyValue); properties.remove(propertyName); properties.put(propertyName, childPages.toArray()); } else { properties.put(propertyName, propertyValue); } // These properties are added by the drag and drop, they do not belong to the component configuration properties.remove(MULTIPLE.replace(PROPERTY_PREFIX, StringUtils.EMPTY)); properties.remove(SELECTION_ID.replace(PROPERTY_PREFIX, StringUtils.EMPTY)); } else if (path.equals(COMPOSITE_VARIABLE)) { // create new subNode int count = Iterators.size(currentResource.getChildren().iterator()); String nodeName = "item" + count; currentResource = resourceResolver.create(currentResource, nodeName, new HashMap<>()); isArray = false; } else if (StringUtils.isNotBlank(path)) { // get or create new node Resource subResource = currentResource.getChild(path); if (subResource == null) { currentResource = resourceResolver.create(currentResource, path, new HashMap<>()); } else { currentResource = subResource; } } } }