onDeleteNode and beforeDeleteNode fired when a new version is uploaded

cancel
Showing results for 
Search instead for 
Did you mean: 
akreienbring
Active Member

onDeleteNode and beforeDeleteNode fired when a new version is uploaded

I'm developing some kind of CRUD application that needs to create, update and delete an external repository when content (a special type of document) changes in Alfresco.

Therefor I implemented a behaviour that should react on a version change (update external) and on the deletion (delete external) of a document.

But I realized that the policies beforeDeleteNode and onDeleteNode are both triggered when a new version is uploaded.

For me this makes no sense because the Node is NOT deleted, only a new version is created for an existing Node.

I consider this as a bug, because the Node document  is only replaced with another Node. It keeps the id, the properties etc...

On the other hand I think there's no need to fire these events on a version change. Because the VersionServicePolicies is sufficient to react on it.

This breakes the concept of CRUD.

Does anyone know which event to react on, when a node is indeed deleted (or archived) from the repository?

 

3 Replies
afaust
Master

Re: onDeleteNode and beforeDeleteNode fired when a new version is uploaded

This is not a bug. A new node is created and deleted - the temporary working copy on which all changes are applied before those are committed to the primary node. This is a conscious design of the Share upload web script, and can happen in other contexts as well as working copies (via the CheckOutCheckInService) are a core concept in Alfresco. You absolutely have to check for the presence of a working copy aspect in any behaviours where you only want to act on "real" nodes.

akreienbring
Active Member

Re: onDeleteNode and beforeDeleteNode fired when a new version is uploaded

Thanks for the hint with the working copy aspect. For others who want to do something when a node is finally deleted, here's how I solved it for now:

In the behaviour I've implemented:

	public void beforeDeleteNode(NodeRef nodeRef) {
		if (logger.isDebugEnabled()) logger.debug("Inside beforeDeleteNode. Calling disable-web-flag");
		
		if(!hasWorkingCopyAspect(nodeRef)){
			//if a node has the working copy aspect then this event was triggered even thought
			//the NODE / Document is NOT finally deleted. For example if a new version is uploaded or was 
			//checked out / in
			
			Action action = actionService.createAction("[whatever-must-be-deletet]");
			actionService.executeAction(action, nodeRef);
			
		};
	}

	private boolean hasWorkingCopyAspect(NodeRef nodeRef) {
		// check the node for the presence of the working copy aspect
		if (logger.isDebugEnabled()) logger.debug("Node exists = " + nodeService.exists(nodeRef));
		
		if (nodeService.hasAspect(nodeRef, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"workingcopy"))) {
			if (logger.isDebugEnabled()) logger.debug("Node has the working copy aspect.");
			
			return true;
		} else {
			if (logger.isDebugEnabled()) logger.debug("Node doesn't have the working copy aspect.");
			return false;
		}
	}

Note that this does NOT work in the onDeleteNode event. When onDeleteNode is fired the aspect is not present anymore.

I hope that this works in all cases the beforeDeleteNode is fired.

badawiraphael
Member II

Re: onDeleteNode and beforeDeleteNode fired when a new version is uploaded

EDIT : issue was solved by triggering the event on FIRST_EVENT rather than TRANSACTION_COMMIT. Apparently when beforeDeleteNode is at TRANSACTION_COMMIT stage the node is already deleted.

 

Hello there,

 

In which version of Alfresco did this code run?

 

Currently in Alfresco 6, I have some InvalidNodeRefException (Node does not exist) in a beforeMoveNode event callback.

 

The callback is registered without fancyness :

 

        eventManager.bindClassBehaviour(
            NodeServicePolicies.BeforeDeleteNodePolicy.QNAME,
            ContentModel.TYPE_CONTENT,
            new JavaBehaviour(this, "beforeDeleteDocument",
                NotificationFrequency.TRANSACTION_COMMIT
            ));

 

 

and then we have your implementation (thanks to you) :

 

    private boolean hasWorkingCopyAspect(NodeRef nodeRef) {	
		if (nodeService.hasAspect(nodeRef, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "workingcopy"))) {
			return true;
		}
		return false;
	}

    public void beforeDeleteDocument(NodeRef nodeRef) throws WikiException {
        if (hasWorkingCopyAspect(nodeRef)){
			//if a node has the working copy aspect then this event was triggered even thought
			//the NODE / Document is NOT finally deleted. For example if a new version is uploaded or was 
			//checked out / in
            return;
			
		};
        String nodeName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);

and so on and so on.

 

I have the InvalidNodeRefException in the nodeService.hasAspect call. If I comment it, then I have the InvalidNodeRefException in the nodeService.getProperty call.

In all other implemented behaviours of the policy the nodeService works like a charm.

It's as if the node was already deleted at beforeDeleteNode call time, which doesn't make any sense to me...