| 672 | |
| 673 | === Listen to DeepaMehta Core events === |
| 674 | |
| 675 | In 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 | |
| 677 | Listening 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 | |
| 679 | To 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 | |
| 686 | Example: |
| 687 | |
| 688 | {{{ |
| 689 | #!java |
| 690 | package org.mydomain.deepamehta4.mycoolplugin; |
| 691 | |
| 692 | import de.deepamehta.core.Topic; |
| 693 | import de.deepamehta.core.model.TopicModel; |
| 694 | import de.deepamehta.core.osgi.PluginActivator; |
| 695 | import de.deepamehta.core.service.ClientState; |
| 696 | import de.deepamehta.core.service.Directives; |
| 697 | import de.deepamehta.core.service.event.PostCreateTopicListener; |
| 698 | import de.deepamehta.core.service.event.PostUpdateTopicListener; |
| 699 | |
| 700 | import java.util.logging.Logger; |
| 701 | |
| 702 | |
| 703 | |
| 704 | public 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 | |
| 721 | This example plugin listens to 2 DeepaMehta Core events: `POST_CREATE_TOPIC` and `POST_UPDATE_TOPIC`. |
| 722 | |
| 723 | These 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 | |
| 725 | The example plugin just logs the created resp. updated topic. In case of "update" the previous topic content is logged as well. |
| 726 | |
| 727 | A [[DeepaMehtaCoreEvents|list of all DeepaMehta Core events]] is available in the reference section. |