cancel
Showing results for 
Search instead for 
Did you mean: 

Custom action on deleted Noderef

mangar
Star Contributor
Star Contributor
I have an action that needs to be triggered on a delete action.  I write my action and hook it to a space rule. The action triggers fine.

However, I get an error:

org.alfresco.service.cmr.repository.InvalidNodeRefException: Node does not exist:


at this line of code:

String fileName = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);

which makes sense, as I just deleted the node.

How do I get path/filename etc..  info in an action where I just deleted the node?

How do i get the information before the node is deleted?
5 REPLIES 5

mrogers
Star Contributor
Star Contributor
You need to use the "before delete" event if you want to read any of the node's properties.

mangar
Star Contributor
Star Contributor
After some research, I see that I need to implement a policyComponent and override beforeDeleteNode().

It also looks like that this action would be called for every piece of content.  Is there a way to make this only happen in a certan folder, or for a certain kind of content?

I have gotten pretty good at writing actions, but this is lost on me. Do you have any examples?

lista
Star Contributor
Star Contributor
Another option is to find the node in the archives store, and get the properties from there.

mangar
Star Contributor
Star Contributor
Good idea.  However I can't seem to get the node from the ArchiveService.  in my executeImpl  () method i have this:

         System.out.println("AtionedUponNodeRef Node:"+actionedUponNodeRef.getId()+actionedUponNodeRef.getStoreRef().getIdentifier());
         NodeRef origNodeRef = archiveService.getArchivedNode(actionedUponNodeRef);   
         System.out.println("Original Node:"+origNodeRef.getId()+origNodeRef.getStoreRef().getIdentifier());

with the output being:
AtionedUponNodeRef Node:7adb1a27-6fcf-4321-8807-448bb9edf928SpacesStore
Original Node:7adb1a27-6fcf-4321-8807-448bb9edf928SpacesStore

They are the same node. Smiley Sad

How do I get the archived node?

mangar
Star Contributor
Star Contributor
The solution is a custom policy using the before delete node.

Here is an excellent tutorial by Jeff Potts on behaviours: http://ecmarchitect.com/images/articles/alfresco-behavior/behavior-article.pdf

here is my code snippet:
public class DeleteAsset implements NodeServicePolicies.BeforeDeleteNodePolicy  {
   
   private PolicyComponent policyComponent;
   private Behaviour beforeDeleteNode;
   
   public void init() {
      this.beforeDeleteNode = new JavaBehaviour(this,"beforeDeleteNode",NotificationFrequency.EVERY_EVENT);
      this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI,"beforeDeleteNode"),
            QName.createQName(MyModel.NAMESPACE,MyModel.ASSET_CONTENT_TYPE), this.beforeDeleteNode);
   }
   
   @Override
   public void beforeDeleteNode(NodeRef node) {
      System.out.println("beforeDeleteNode!");
      try {
         your code here         
      } catch(Exception e) {
         e.printStackTrace();
      }
      
   }
}

Just add this just like any other spring bean (See tutorial) and away you go.  One key is the NotificationFrequency must be EVERY_EVENT so you can use your NodeService (or whatever) before the node is deleted!

Hope this helps someone. Smiley Happy