Canceling an event

I would like to be able to cancel the Save event of a page using a Groovy script.

We are creating forms that when finished the user has to sign. Using a Groovy script, I capture the DocumentUpdateEvent, do a lookup in our in house database, and if valid set various text fields. This all works. I would like to cancel the Save event if an invalid response is entered.

This is the function in which all of the action occurs:

void onEvent(Event event, Object source, Object context) {

}

Can this event be canceled?
If so, how?
If there is any documentation on this where might I find it?

We are on XWiki 9.11.4

Thanks,
Steve

Some events implements org.xwiki.observation.event.CancelableEvent which has a cancel(String reason) method.

In you case the events you can cancel are all the Document*ingEvent events (for example DocumentUpdatingEvent).

I have changed my code from registering DocumentUpdateEvent() to DocumentUpdatingEvent(). In order to test that the onEvent is firing I have the following line to write out a text to the page:

document.setContent("${document.getContent()}\nonEvent Fired")

When I register the DocumentUpdateEvent this works but DocumentUpdatingEvent does not. I have removed the listener and added it back in via:

def observation = Utils.getComponent(ObservationManager.class)
observation.removeListener(“testForm”)
def listener = new LoggingEventListener()
observation.addListener(listener)

I have restarted the XWiki service in between and no changes. Is there something else that I am missing?

–Steve

Is there something else that I am missing?

Hard to say without seeing the code.

For example what exactly do you mean by

document.setContent("${document.getContent()}\nonEvent Fired")

where does this document come from ?

In a DocumentUpdatingEvent listener you are supposed to modify the XWikiDocument instance which is passed as second argument (source in the javadoc) because that’s what is going to be saved after the call to your listener (and never load or save the document yourself).

So the code preceding that line is
def xwiki = context.getWiki()
def document = xwiki.getDocument(source, context)

I thought I was getting a reference to the source document. Instead, I used source directly ( source.setContent("${source.getContent()}\nonEvent Fired") ) and everything worked correctly.

Thanks so much.

Now, what documentation is available that describes these functions? I have gone through the Event Listener tutorial, Local Observation Module samples and they give a brief example but nowhere have I found detailed explanation of properties/methods available. The only direction I have found is posting to the message board.

Once again, thanks for all your help.

–Steve

Now that I have the cancel working, XWiki throws an

Error number 3201 in 3: An Event Listener has cancelled the document save for [xwiki:Forms.TestForm.Test1]. Reason: [Invalid Signature]

Is there a way to capture this and redirect the user?

–Steve