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
FieldsFields inherited from interface org.apache.sling.api.resource.Resource
RESOURCE_TYPE_NON_EXISTING -
Constructor Summary
ConstructorsConstructorDescriptionChildrenAsPropertyResource(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 TypeMethodDescriptionfinal org.apache.sling.api.resource.Resourcefinal org.apache.sling.api.resource.Resourcefinal voidDeletes the named child.final voidDelete all children.protected final List<SyntheticChildAsPropertyResource>deserializeToSyntheticChildResources(com.google.gson.JsonObject jsonObject) Converts a JSONObject to the list of SyntheticChildAsPropertyResources.final org.apache.sling.api.resource.Resourcefinal Iterable<org.apache.sling.api.resource.Resource>final org.apache.sling.api.resource.Resourcefinal Iterator<org.apache.sling.api.resource.Resource>final voidpersist()Persist changes to the underlying valuemap so they are available for persisting to the JCR.protected final com.google.gson.JsonObjectserializeToJSON(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
-
Field Details
-
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 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 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 onpropertyName- the property name to store the children as properties incomparator- the comparator used to order the serialized children- Throws:
InvalidDataFormatException
-
-
Method Details
-
listChildren
- Specified by:
listChildrenin interfaceorg.apache.sling.api.resource.Resource- Overrides:
listChildrenin classorg.apache.sling.api.resource.ResourceWrapper
-
getChildren
- Specified by:
getChildrenin interfaceorg.apache.sling.api.resource.Resource- Overrides:
getChildrenin classorg.apache.sling.api.resource.ResourceWrapper
-
getChild
- 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
Deletes the named child. Requires subsequent call to persist().- Parameters:
name- the child node name to delete- Throws:
javax.jcr.RepositoryException
-
deleteAll
Delete 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, 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:
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.
-