Java Example

This is an example how to listening on "Object Update" event and get detailed information about affected attributes.

package com.example.event.listener;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
 
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.user.ApplicationUser;
import com.riadalabs.jira.plugins.insight.services.events.objects.InsightObjectAsyncEvent;
import com.riadalabs.jira.plugins.insight.services.events.objects.InsightObjectEventListener;
import com.riadalabs.jira.plugins.insight.services.events.objects.event.InsightObjectCreatedEvent;
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean;
 
public class ObjectEventListenerImpl implements InitializingBean, DisposableBean, InsightObjectEventListener {
 
    private static final Logger logger = LoggerFactory.getLogger(ObjectEventListenerImpl.class);
    private final EventPublisher eventPublisher;
 
    /**
     * Constructor.
     *
     * @param eventPublisher
     */
    public ObjectEventListenerImpl(final EventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }
 
    @EventListener
    public void onAsyncEvent(InsightObjectAsyncEvent insightObjectEvent) {
        switch (insightObjectEvent.getEventType()) {
        case OBJECT_UPDATED:
			InsightObjectUpdatedEvent insightObjectUpdatedEvent = (InsightObjectUpdatedEvent) insightObjectEvent;
    		ObjectBean objectBean = insightObjectUpdatedEvent.getObjectBean();
			ApplicationUser user = insightObjectCreatedEvent.getRunAsUser();
			logger.warn("User: " + user + " updated object: " + objectBean);

    		List<NotificationUpdateBean> updatedAttributes = insightObjectUpdatedEvent.getNotificationUpdateBeans();
    		for (NotificationUpdateBean updatedAttribute : updatedAttributes) {
    			String attributeName = updatedAttribute.getAttributeName();
    			List<String> addedValues = updatedAttribute.getAddedValues();
    			List<String> removedValues = updatedAttribute.getRemovedValues();
				logger.warn("Updated attribute " + attributeName + ", added attribute value(s): " + addedValues + ", removed attribute value(s): " + removedValues);
   			}
    		break;
        default:
            break;
        }
    }
 
    /**
     * Called when the plugin has been enabled.
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        eventPublisher.register(this);
    }
 
    /**
     * Called when the plugin is being disabled or removed.
     *
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        eventPublisher.unregister(this);
    }
 
}
  • No labels