Changes between Version 18 and Version 19 of PluginDevelopmentGuide

16.04.2013 01:44:28 (11 years ago)

Listen to DeepaMehta Core events


  • PluginDevelopmentGuide

    v18 v19  
     673=== Listen to DeepaMehta Core events === 
     675In particular situations the DeepaMehta Core fires events, e.g. before and after it creates a new topic in the database. Your plugin can listen to these events and react in its own way. 
     677Listening to a DeepaMehta Core event means implementing the corresponding listener interface. A listener interface consist of just one method: the //listener method//. That method is called by the DeepaMehta Core when the event is fired. The listener interfaces are located in package `de.deepamehta.core.service.event`. 
     679To listen to a DeepaMehta Core event, in the plugin main class you must: 
     681    * Import the listener interface. 
     682    * Declare the plugin main class implements that interface. 
     683    * Implement the listener method. Use the `@Override` annotation. 
     684    * Import the classes appearing in the listener method arguments. 
     690package org.mydomain.deepamehta4.mycoolplugin; 
     692import de.deepamehta.core.Topic; 
     693import de.deepamehta.core.model.TopicModel; 
     694import de.deepamehta.core.osgi.PluginActivator; 
     695import de.deepamehta.core.service.ClientState; 
     696import de.deepamehta.core.service.Directives; 
     697import de.deepamehta.core.service.event.PostCreateTopicListener; 
     698import de.deepamehta.core.service.event.PostUpdateTopicListener; 
     700import java.util.logging.Logger; 
     704public class MyCoolPlugin extends PluginActivator implements PostCreateTopicListener, PostUpdateTopicListener { 
     706    private Logger log = Logger.getLogger(getClass().getName()); 
     708    @Override 
     709    public void postCreateTopic(Topic topic, ClientState clientState, Directives directives) { 
     710"### Topic created: " + topic); 
     711    } 
     713    @Override 
     714    public void postUpdateTopic(Topic topic, TopicModel newModel, TopicModel oldModel, ClientState clientState, 
     715                                                                                       Directives directives) { 
     716"### Topic updated: " + topic + "\nOld topic: " + oldModel); 
     717    } 
     721This example plugin listens to 2 DeepaMehta Core events: `POST_CREATE_TOPIC` and `POST_UPDATE_TOPIC`. 
     723These particular events are fired //after// the DeepaMehta Core has created resp. updated a topic. The DeepaMehta Core passes the created/updated topic to the respective listener method. In case of "update" the previous topic content (`oldModel`) is also passed to enable the plugin to investigate what exactly has changed. 
     725The example plugin just logs the created resp. updated topic. In case of "update" the previous topic content is logged as well. 
     727A [[DeepaMehtaCoreEvents|list of all DeepaMehta Core events]] is available in the reference section.