Class ChildrenAsPropertyResource

java.lang.Object
org.apache.sling.api.resource.ResourceWrapper
com.adobe.acs.commons.synth.children.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 final Comparator<org.apache.sling.api.resource.Resource>
     

    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    final org.apache.sling.api.resource.Resource
    create(String name, String primaryType)
     
    final org.apache.sling.api.resource.Resource
    create(String name, String primaryType, Map<String,Object> data)
     
    final void
    delete(String name)
    Deletes the named child.
    final void
    Delete all children.
    deserializeToSyntheticChildResources(com.google.gson.JsonObject jsonObject)
    Converts a JSONObject to the list of SyntheticChildAsPropertyResources.
    final org.apache.sling.api.resource.Resource
    final Iterable<org.apache.sling.api.resource.Resource>
    final org.apache.sling.api.resource.Resource
    final Iterator<org.apache.sling.api.resource.Resource>
    final void
    Persist changes to the underlying valuemap so they are available for persisting to the JCR.
    protected final com.google.gson.JsonObject
    serializeToJSON(org.apache.sling.api.resource.Resource resourceToSerialize)
    Converts a list of SyntheticChildAsPropertyResource to their JSON representation, keeping the provided order.

    Methods inherited from class org.apache.sling.api.resource.ResourceWrapper

    adaptTo, getName, getPath, getResource, getResourceMetadata, getResourceResolver, getResourceSuperType, getResourceType, getValueMap, hasChildren, isResourceType, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • RESOURCE_NAME_COMPARATOR

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

    • 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 Details

    • 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
    • deleteAll

      public final void deleteAll() throws InvalidDataFormatException
      Delete all children. Requires subsequent call to persist().
      Throws:
      InvalidDataFormatException
    • 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
    • serializeToJSON

      protected final com.google.gson.JsonObject serializeToJSON(org.apache.sling.api.resource.Resource resourceToSerialize) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
      Converts a list of SyntheticChildAsPropertyResource to their JSON representation, keeping the provided order.
      Parameters:
      resourceToSerialize - the resource to serialize to JSON.
      Returns:
      the JSONObject representing the resources.
      Throws:
      IllegalAccessException
      NoSuchMethodException
      InvocationTargetException
    • 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.