The event bus classes are important if you want your plugin to act on certain events in Opinio. For example, when a respondent has completed a survey, you want to do something with the data (like sending an email). To know when this respondent has completed, you need to receive an event notification.
To receive this notification, the plugin must register itself on the event bus. There are several bus types and many event types in each bus. See om.objectplanet.survey.event.EventBusManager in the plugin documentation.
Examples of bus types:
BUS_TYPE_SURVEY - Survey response bus: handles the events in the Survey module. Examples of event types are: DisplayQuestionEvent, NewRespondentEvent, RespondentCompleteEvent, ResponseEvent and so on.
BUS_TYPE_SURVEY_MANAGEMENT - Survey design bus: handles administration module events. When a new survey is created, for example, a NewSurveyEvent is fired. Examples of event types are: NewSurveyEvent, SurveyDeletedEvent, QuestionDeletedEvent and so on.
BUS_TYPE_USER - User event bus: handles user administration events. When a new Opinio user is created, for example, a NewUserEvent is fired. Examples of event types are: NewUserEvent, PreUserDeleteEvent, UserDeletedEvent and so on.
There are several addListener(**) methods in the EventBusManager. You can register to listen to:
all events in the system (every single one) - use addListener(IEventListener listener) method
all events on a specific bus type - use addListener(IEventListener listener, int busType)
specific event type sent on the specific bus - use addListener(IEventListener listener, int busType, int eventType)
specific event type sent on the specific bus for a particular resource - use addListener(IEventListener listener, int busType, int eventType, long resourceId)
IMPORTANT: Try to register events on as low level as possible to maximize the performance.
Example 1. Plugin example
Implement com.objectplanet.survey.event.IEventListener interface.
In the start() method of your plugin, you can register your plugin to listen to the administration events:
public void start() { EventBusManager eventMgr = EventBusManager.instance(); eventMgr.addListener(this, EventBusManager.BUS_TYPE_SURVEY_MANAGEMENT, EventBusManager. EVENT_TYPE_NEW_SURVEY); }
Implement the handleEvent() method from IEventListener interface, so that the events received are handled correctly. For example:
public void handleEvent(PluginBusEvent event) { if (event instanceof NewSurveyEvent) { NewSurveyEvent nsEvent = (NewSurveyEvent) event; long surveyId = nsEvent.getSurveyId(); // Do something with the survey; send an email, // call an external system etc. .... } }