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.ResourceWrapperClass 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
-
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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description org.apache.sling.api.resource.Resourcecreate(String name, String primaryType)org.apache.sling.api.resource.Resourcecreate(String name, String primaryType, Map<String,Object> data)voiddelete(String name)Deletes the named child.voiddeleteAll()Delete all children.protected List<SyntheticChildAsPropertyResource>deserializeToSyntheticChildResources(com.google.gson.JsonObject jsonObject)Converts a JSONObject to the list of SyntheticChildAsPropertyResources.org.apache.sling.api.resource.ResourcegetChild(String name)Iterable<org.apache.sling.api.resource.Resource>getChildren()org.apache.sling.api.resource.ResourcegetParent()Iterator<org.apache.sling.api.resource.Resource>listChildren()voidpersist()Persist changes to the underlying valuemap so they are available for persisting to the JCR.protected com.google.gson.JsonObjectserializeToJSON(org.apache.sling.api.resource.Resource resourceToSerialize)Converts a list of SyntheticChildAsPropertyResource to their JSON representation, keeping the provided order.
-
-
-
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 InvalidDataFormatExceptionResourceWrapper 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 InvalidDataFormatExceptionResourceWrapper that allows resource children to be modeled in data stored into a property.- Parameters:
resource- the resource to store the children as properties onpropertyName- 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 InvalidDataFormatExceptionResourceWrapper that allows resource children to be modeled in data stored into a property.- Parameters:
resource- the resource to store the children as properties onpropertyName- the property name to store the children as properties incomparator- 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:
listChildrenin interfaceorg.apache.sling.api.resource.Resource- Overrides:
listChildrenin classorg.apache.sling.api.resource.ResourceWrapper
-
getChildren
public final Iterable<org.apache.sling.api.resource.Resource> getChildren()
- Specified by:
getChildrenin interfaceorg.apache.sling.api.resource.Resource- Overrides:
getChildrenin classorg.apache.sling.api.resource.ResourceWrapper
-
getChild
public final org.apache.sling.api.resource.Resource getChild(String name)
- Specified by:
getChildin interfaceorg.apache.sling.api.resource.Resource- Overrides:
getChildin classorg.apache.sling.api.resource.ResourceWrapper
-
getParent
public final org.apache.sling.api.resource.Resource getParent()
- Specified by:
getParentin interfaceorg.apache.sling.api.resource.Resource- Overrides:
getParentin classorg.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 InvalidDataFormatExceptionDelete all children. Requires subsequent call to persist().- Throws:
InvalidDataFormatException
-
persist
public final void persist() throws javax.jcr.RepositoryExceptionPersist 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, InvocationTargetExceptionConverts 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:
IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetException
-
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.
-
-