Groovy Notifications

This tutorial explains how to create a Groovy Event Listener that responds to document create and update events. For this purpose the XWiki Observation module provides APIs which listen to internal XWiki events. The complete list of XWiki Bridge Events is available on GitHub.

A possible use case is the need to create a Groovy Listener that sends an email notification to the farm administrator whenever a page is created or updated in the "Code" space of the main wiki. Also, you might want to keep a log of the new/updated documents in a wiki document, for instance "Admin.Logger". The recommended approach is to create a Scheduler job because this way you can schedule when the event listener sends email notifications. Moreover, there is no risk for the code to be executed after a server restart. 

The first step will then be to go to "Scheduler.WebHome" and create a new job, named "Document Updated Event Listener". The Groovy code is:

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 "logging"
  }

   List<Event> getEvents()
  {
      // The list of events this listener listens to
      def regEx = new RegexEventFilter("xwiki:Code\\..*")
      return Arrays.asList(new DocumentCreatedEvent(regEx), new DocumentUpdatedEvent(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)
 {
    def context = (XWikiContext) data
    def sender = context.getWiki().getUser(source.getCreator(), context).getEmail()
    def to = "admin@gmail.com"
    def subject = "Document changes on xwiki:Code"
    def body = "Hello, A page has been created or updated in the space 'Code'. You can view this page at: ${source.fullName}"
    def result = context.getWiki().getPluginApi("mailsender", context).sendTextMessage("${sender}", "${to}", subject, body)
   if (source.fullName != "Admin.Logger") {
            def document = context.getWiki().getDocument("Admin.Logger", context)
            document.setContent("${document.getContent()}\n${source.fullName} has been created or updated.")
            context.getWiki().saveDocument(document, "Logging event", true, context)
   }  
}
}
// Register against the Observation Manager
def observation = Utils.getComponent(ObservationManager.class)
observation.removeListener("logging")
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.*

After triggering the Scheduler job, create a page in the "xwiki:Code/" space then go to "Admin.Logger" where you should see the new page listed.

EventListenerLogger.png

Also, provided you have correctly set up a SMTP server, you should receive an email notification regarding the new/updated document.

EventListenerEmailNotification.png

In case you are interested in more Groovy Event Listener examples, you could check the xwiki.org tutorial.

   

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]