AnsweredAssumed Answered

Deleting nodes in BeforeDeleteNode Behaviour

Question asked by krzysztof.nowacki on Dec 4, 2013
Latest reply on Dec 4, 2013 by krzysztof.nowacki
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?

Outcomes