XWiki Standard 9.8.1 Documentation » Programming Overview » Post Processing Groovy Listener

Post Processing Groovy Listener

In the Groovy Notifications tutorial we have presented a way of logging the document create and update events using the XWiki Observation module. However, you might need to make some additional changes on the document after the "Save" action. 

A possible use case would be an XWiki application that stores resource names of any kind and for each application entry, the page title is identical to the page name. Supposing, you are already setting the title in the application sheet, you might also need the title to be automatically updated after renaming the entry. Moreover, you might want to avoid manual title changes from other users or even typos. For this purpose, you could create a Scheduler job named "Update Title Listener" with the following code:

import org.xwiki.observation.*
import org.xwiki.observation.event.*
import org.xwiki.bridge.event.*
import org.xwiki.observation.event.filter.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*

class LoggingEventListener implements EventListener
{

  LoggingEventListener()
 {
 }

  String getName()
 {
     // The unique name of this event listener
     return "loggingTitle"
 }

    List<Event> getEvents()
 {
     // The list of events this listener listens to
    def regEx = new RegexEventFilter("xwiki:RessourcesApplication\\..*")
    return Arrays.asList(new DocumentCreatingEvent(regEx), new DocumentUpdatingEvent(regEx))
 }

 // Called by the Observation Manager when an event matches the list of events returned
 // by getEvents()
void onEvent(Event event, Object source, Object data)
{
    source.setTitle(source.getName())
}
}
// Register against the Observation Manager
def observation = Utils.getComponent(ObservationManager.class)
observation.removeListener("loggingTitle")
def listener = new LoggingEventListener()
observation.addListener(listener)

The events the listener listens to are:

and in order to be able to use them, you must make sure you have imported the dedicated package: import org.xwiki.bridge.event.*

The complete list of XWiki Bridge Events is available on GitHub. Another tutorial explaining how to update the value of the title field of an XWiki document based on the value of an object's property is available on xwiki.org.

  

Related Pages

Search this space

 

Most popular tags

Failed to execute the [groovy] macro
  1. access rights
  2. activity stream
  3. annotation
  4. attachment
  5. comment
  6. Document Tree Macro
  7. export
  8. Extension Manager
  9. Flamingo skin
  10. global user
  11. Groovy event listener
  12. group
  13. nested page
  14. search
  15. skin
  16. syntax
  17. user
  18. user profile
  19. velocity macros
  20. wiki
  21. wysiwyg
  22. XWiki Applications
  23. xwikiattachment_archive table
  24. xwikiattachment table
  25. xwikiattrecyclebin table
  26. xwikiproperties table

[Display all tags from this space]