0

I have a widget that creates a POST request that creates a node and a dynamic number of subnodes, like:

./sling:resourceType:app/component
_charset_:utf-8
:status:browser
./data:data
./a/a:one
./a/b:two
./b/a:one
./b/b:two

This works nice the first time. I get a node along with subnodes a and b. The problem is in subsequent requests. I need all subnodes to be removed before creating the new ones. So if previously I created subnodes a,b,c and d, the previous request would result just in subnodes a and b to remain.

I know the suffix @Delete,but I would need to know in advance which subnodes need to be deleted, which I don't.

Can this be achieved OOTB with the Sling Post Servlet?

Greetings.

santiagozky
  • 2,519
  • 22
  • 31

2 Answers2

0

In case you are using CQ 5.6 or 5.6.1 you can use ':applyTo' request parameter to delete multiple items using a single request by passing a trailing star in its value.

For example, to delete all the children of '/content/foo', make a POST request with ':operation' = 'delete', and ':applyTo' = '/content/foo/*'.

$ curl -F":operation=delete" -F":applyTo=/content/foo/*" http://host/content/sample

This was introduced in Sling 2.1.2 and hence is not available in CQ 5.5 and below, as 5.5 runs on 2.1.1.

For 5.5, i suspect you might need to get the list of children and then pass the absolute URL's to the :applyTo as the value to delete them, before adding the new nodes.

rakhi4110
  • 9,253
  • 2
  • 30
  • 49
  • unfortunately that would require one request to delete the existing nodes and a second one to create the new ones. If for some reason the second request is not performed I would end up with nothing at all. – santiagozky Mar 28 '14 at 08:39
0

The solution I'm using is use the @Delete suffix. The problem with it is that you need to add one parameter with the @Delete suffix for every node you want to delete. What I'm doing is query the node in advance to check for all the subnodes of the node I'm updating and adding a @Delete parameter for every one of them.

So, If the JCR originally has

-node
 \node1
 \node2
 \node3

I will first get http://example.com/content/node.json and traverse the json. Finally I will send a request with

./sling:resourceType:app/component
./value:value
./node1@Delete
./node2@Delete
./node3@Delete
./node1/value:value1
./node2/value:value2

that will update node1 and node2 while deleting node3 at the same time.

santiagozky
  • 2,519
  • 22
  • 31