Class ChildrenAsPropertyResource

  • All Implemented Interfaces:
    org.apache.sling.api.adapter.Adaptable, org.apache.sling.api.resource.Resource

    public class ChildrenAsPropertyResource
    extends org.apache.sling.api.resource.ResourceWrapper
    Class to wrapper a real resource to facilitate the persistence of children resources in a property (serialized as JSON). Can be used as follows... To write data: Resource real = resolve.getResource("/content/real"); ChildrenAsPropertyResource wrapper = new ChildrenAsPropertyResource(real); Resource child = wrapper.create("child-1", "nt:unstructured"); ModifiableValueMap mvm = child.adaptTo(ModifiableValueMap.class); mvm.put("prop-1", "some data"); mvm.put("prop-2", Calendar.getInstance()); wrapper.persist(); resolver.commit(); To read data: Resource real = resolve.getResource("/content/real"); ChildrenAsPropertyResource wrapper = new ChildrenAsPropertyResource(real); for(Resource child : wrapper.getChildren()) { child.getValueMap().get("prop-1", String.class); }
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Comparator<org.apache.sling.api.resource.Resource> RESOURCE_NAME_COMPARATOR  
      • Fields inherited from interface org.apache.sling.api.resource.Resource

        RESOURCE_TYPE_NON_EXISTING
    • Constructor Summary

      Constructors 
      Constructor Description
      ChildrenAsPropertyResource​(org.apache.sling.api.resource.Resource resource)
      ResourceWrapper that allows resource children to be modeled in data stored into a property using the default property name of "children".
      ChildrenAsPropertyResource​(org.apache.sling.api.resource.Resource resource, String propertyName)
      ResourceWrapper that allows resource children to be modeled in data stored into a property.
      ChildrenAsPropertyResource​(org.apache.sling.api.resource.Resource resource, String propertyName, Comparator<org.apache.sling.api.resource.Resource> comparator)
      ResourceWrapper that allows resource children to be modeled in data stored into a property.
    • Field Detail

      • RESOURCE_NAME_COMPARATOR

        public static final Comparator<org.apache.sling.api.resource.Resource> RESOURCE_NAME_COMPARATOR
    • Constructor Detail

      • ChildrenAsPropertyResource

        public ChildrenAsPropertyResource​(org.apache.sling.api.resource.Resource resource)
                                   throws InvalidDataFormatException
        ResourceWrapper that allows resource children to be modeled in data stored into a property using the default property name of "children".
        Parameters:
        resource - the resource to store the children as properties on
        Throws:
        InvalidDataFormatException
      • ChildrenAsPropertyResource

        public ChildrenAsPropertyResource​(org.apache.sling.api.resource.Resource resource,
                                          String propertyName)
                                   throws InvalidDataFormatException
        ResourceWrapper that allows resource children to be modeled in data stored into a property.
        Parameters:
        resource - the resource to store the children as properties on
        propertyName - the property name to store the children as properties in
        Throws:
        InvalidDataFormatException
      • ChildrenAsPropertyResource

        public ChildrenAsPropertyResource​(org.apache.sling.api.resource.Resource resource,
                                          String propertyName,
                                          Comparator<org.apache.sling.api.resource.Resource> comparator)
                                   throws InvalidDataFormatException
        ResourceWrapper that allows resource children to be modeled in data stored into a property.
        Parameters:
        resource - the resource to store the children as properties on
        propertyName - the property name to store the children as properties in
        comparator - the comparator used to order the serialized children
        Throws:
        InvalidDataFormatException
    • Method Detail

      • listChildren

        public final Iterator<org.apache.sling.api.resource.Resource> listChildren()
        Specified by:
        listChildren in interface org.apache.sling.api.resource.Resource
        Overrides:
        listChildren in class org.apache.sling.api.resource.ResourceWrapper
      • getChildren

        public final Iterable<org.apache.sling.api.resource.Resource> getChildren()
        Specified by:
        getChildren in interface org.apache.sling.api.resource.Resource
        Overrides:
        getChildren in class org.apache.sling.api.resource.ResourceWrapper
      • getChild

        public final org.apache.sling.api.resource.Resource getChild​(String name)
        Specified by:
        getChild in interface org.apache.sling.api.resource.Resource
        Overrides:
        getChild in class org.apache.sling.api.resource.ResourceWrapper
      • getParent

        public final org.apache.sling.api.resource.Resource getParent()
        Specified by:
        getParent in interface org.apache.sling.api.resource.Resource
        Overrides:
        getParent in class org.apache.sling.api.resource.ResourceWrapper
      • create

        public final org.apache.sling.api.resource.Resource create​(String name,
                                                                   String primaryType)
                                                            throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • create

        public final org.apache.sling.api.resource.Resource create​(String name,
                                                                   String primaryType,
                                                                   Map<String,​Object> data)
                                                            throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • delete

        public final void delete​(String name)
                          throws javax.jcr.RepositoryException
        Deletes the named child. Requires subsequent call to persist().
        Parameters:
        name - the child node name to delete
        Throws:
        javax.jcr.RepositoryException
      • persist

        public final void persist()
                           throws javax.jcr.RepositoryException
        Persist changes to the underlying valuemap so they are available for persisting to the JCR.
        Throws:
        javax.jcr.RepositoryException
      • deserializeToSyntheticChildResources

        protected final List<SyntheticChildAsPropertyResource> deserializeToSyntheticChildResources​(com.google.gson.JsonObject jsonObject)
        Converts a JSONObject to the list of SyntheticChildAsPropertyResources.
        Parameters:
        jsonObject - the JSONObject to deserialize.
        Returns:
        the list of SyntheticChildAsPropertyResources the jsonObject represents.