cancel
Showing results for 
Search instead for 
Did you mean: 

Deleting nodes in BeforeDeleteNode Behaviour

krzysztof_nowac
Champ in-the-making
Champ in-the-making
I created following code:


public class DocumentBehavior implements NodeServicePolicies.BeforeDeleteNodePolicy {

private Behaviour beforeDeleteNode;

@Override
public void beforeDeleteNode(NodeRef nodeRef) {
   //In this function I'm searching others nodes and then I'm trying delete them.
   String type = (String)nodeService.getProperty(nodeRef, QName.createQName("kn", "docType"););
   StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
   SearchParameters sp = new SearchParameters();
   sp.addStore(storeRef);
   sp.setLanguage(SearchService.LANGUAGE_CMIS_ALFRESCO);
   sp.setQuery("SELECT d.* FROM kn:document AS d WHERE d.cmis:objectId <> '" + nodeRef.toString() + "' AND d.kn:docType= '" + type + "'");
   ResultSet resultSet = searchService.query(sp);
   List<NodeRef> nodes = resultSet.getNodeRefs();
   for (NodeRef node : nodes) {
      if (nodeService.exists(node)) {
         nodeService.deleteNode(node);
      }
   }
}

public void init() {
   this.beforeDeleteNode = new JavaBehaviour(this, "beforeDeleteNode", NotificationFrequency.EVERY_EVENT);
   this.policyComponent.bindClassBehaviour(QName.createQName(
      NamespaceService.ALFRESCO_URI, "beforeDeleteNode"), QName.createQName("kn", "document"),
      this.beforeDeleteNode);
}

}


When I'm trying delete node in Alfresco Share I have a error: "Could not delete node". There is no exceptions in Alfresco Log.

When I'm trying detele node in Alfresco Explorer, the code executes 40 times (why?) and finally there is an exception (please find attached)

When I'm change NotificationFrequency to TRANSACTION_COMMIT or FIRST_EVENT the main node is deleted, but the nodes that I am searching are not deleted.

Is this a bug in Alfresco or do I implemented it the wrong way?
8 REPLIES 8

yogeshpj
Star Contributor
Star Contributor
Just make sure that you have appropriate rights to delete the content and it is obvious that the function that you have implemented will call multiple time as you are again deleting content in that.

kaynezhang
World-Class Innovator
World-Class Innovator
Your code have logic error:
Your behaviour  is bound to type "kn:document" and before you delete a node of type "kn:document" ,you search node with type "kn:document" and delete it.This will cause beforeDeleteNode is called again.
This may cause an infinite loop.

I run it as administrator.

I think there is no logic error, because I searching nodes that are other than deleting node: d.cmisSmiley SurprisedbjectId <> '" + nodeRef.toString() + "'

Moreover, this query is simplified. The original search query also excludes the "parent" node when beforeDeleteNode is called again on "child" node.

kaynezhang
World-Class Innovator
World-Class Innovator
By

sp.setQuery("SELECT d.* FROM kn:document AS d WHERE d.cmis:objectId <> '" + nodeRef.toString() + "' AND d.kn:docType= '" + type + "'");

You are searching nodes with type of kn:document.
By

NamespaceService.ALFRESCO_URI, "beforeDeleteNode"), QName.createQName("kn", "document"),
      this.beforeDeleteNode);
Your behaviour is bound to type "kn:document.
When you delete node returned by previous (which return node type  "kn:document") ,it will tigger your beforeDeleteNode Behaviour again,and so search and delete will be called again and again.It is a cross recurrence call and maybe called many times 
I'm sorry I  used the word inaccurate(infinite loop)  in my last post.

mrogers
Star Contributor
Star Contributor
What happens if you are trying to delete two documents of type kn:document?   Document A will try to delete B and B will try to delete A ad-infinitum.     

Your <> test probably needs to be expanded to a list of nodes that you have deleted.   You can store your list in the current transaction to stop these sorts of infinite chains.

yogeshpj
Star Contributor
Star Contributor
You have to filter out the node in beforeDeleteNode method.This behavior will call for every node which is going to be delete.
Add logic to filter other nodes.

rfernandes
Confirmed Champ
Confirmed Champ
Following up on Mike last comment. You need to leverage transaction listeners for your policy. Look to the ContentHitsAspect example in the Alfresco SDK. https://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/sdk/samples/CustomAs...

krzysztof_nowac
Champ in-the-making
Champ in-the-making
Rui,
I did as you wrote. It works! Thank you very much for your help.
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.