Changes between Version 18 and Version 19 of PluginDevelopmentGuide


Ignore:
Timestamp:
16.04.2013 01:44:28 (7 years ago)
Author:
jri
Comment:

Listen to DeepaMehta Core events

Legend:

Unmodified
Added
Removed
Modified
  • PluginDevelopmentGuide

    v18 v19  
    670670</project> 
    671671}}} 
     672 
     673=== Listen to DeepaMehta Core events === 
     674 
     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. 
     676 
     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`. 
     678 
     679To listen to a DeepaMehta Core event, in the plugin main class you must: 
     680 
     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. 
     685 
     686Example: 
     687 
     688{{{ 
     689#!java 
     690package org.mydomain.deepamehta4.mycoolplugin; 
     691 
     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; 
     699 
     700import java.util.logging.Logger; 
     701 
     702 
     703 
     704public class MyCoolPlugin extends PluginActivator implements PostCreateTopicListener, PostUpdateTopicListener { 
     705 
     706    private Logger log = Logger.getLogger(getClass().getName()); 
     707 
     708    @Override 
     709    public void postCreateTopic(Topic topic, ClientState clientState, Directives directives) { 
     710        log.info("### Topic created: " + topic); 
     711    } 
     712 
     713    @Override 
     714    public void postUpdateTopic(Topic topic, TopicModel newModel, TopicModel oldModel, ClientState clientState, 
     715                                                                                       Directives directives) { 
     716        log.info("### Topic updated: " + topic + "\nOld topic: " + oldModel); 
     717    } 
     718} 
     719}}} 
     720 
     721This example plugin listens to 2 DeepaMehta Core events: `POST_CREATE_TOPIC` and `POST_UPDATE_TOPIC`. 
     722 
     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. 
     724 
     725The example plugin just logs the created resp. updated topic. In case of "update" the previous topic content is logged as well. 
     726 
     727A [[DeepaMehtaCoreEvents|list of all DeepaMehta Core events]] is available in the reference section.