Commits

Shihab Hamid  committed 4ca1a17

on plugin enable, fire notification to all users

  • Participants
  • Parent commits e408d3d

Comments (0)

Files changed (4)

             <version>1.9.12</version>
             <scope>test</scope>
         </dependency>
+
+        <!-- we need the mywork-api to create notifications -->
+        <dependency>
+            <groupId>com.atlassian.mywork</groupId>
+            <artifactId>mywork-api</artifactId>
+            <version>1.0.2</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 
     <build>

File src/main/java/com/atlascamp/mywork/ril/ExampleMacro.java

-package com.atlascamp.mywork.ril;
-
-import java.util.Map;
-import java.util.List;
-
-import com.atlassian.confluence.content.render.xhtml.ConversionContext;
-import com.atlassian.confluence.macro.Macro;
-import com.atlassian.confluence.macro.MacroExecutionException;
-import com.atlassian.confluence.pages.PageManager;
-import com.atlassian.confluence.pages.Page;
-import com.atlassian.confluence.spaces.SpaceManager;
-import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
-import com.atlassian.user.User;
-import com.opensymphony.util.TextUtils;
-
-/**
- * This very simple macro shows you the very basic use-case of displaying *something* on the Confluence page where it is used.
- * Use this example macro to toy around, and then quickly move on to the next example - this macro doesn't
- * really show you all the fun stuff you can do with Confluence.
- */
-public class ExampleMacro implements Macro
-{
-
-    // We just have to define the variables and the setters, then Spring injects the correct objects for us to use. Simple and efficient.
-    // You just need to know *what* you want to inject and use.
-
-    private final PageManager pageManager;
-    private final SpaceManager spaceManager;
-
-    public ExampleMacro(PageManager pageManager, SpaceManager spaceManager)
-    {
-        this.pageManager = pageManager;
-        this.spaceManager = spaceManager;
-    }
-
-    /**
-     * This method returns XHTML to be displayed on the page that uses this macro
-     * we just do random stuff here, trying to show how you can access the most basic
-     * managers and model objects. No emphasis is put on beauty of code nor on
-     * doing actually useful things :-)
-     */
-    @Override
-    public String execute(Map<String, String> parameters, String body, ConversionContext context) throws MacroExecutionException
-    {
-        // in this most simple example, we build the result in memory, appending HTML code to it at will.
-        // this is something you absolutely don't want to do once you start writing plugins for real. Refer
-        // to the next example for better ways to render content.
-        StringBuffer result = new StringBuffer();
-
-        // get the currently logged in user and display his name
-        User user = AuthenticatedUserThreadLocal.getUser();
-        if (user != null)
-        {
-            String greeting = "<p>Hello <b>" + TextUtils.htmlEncode(user.getFullName()) + "</b>.</p>";
-            result.append(greeting);
-        }
-
-        //get the pages added in the last 55 days to the DS space ("Demo Space"), and display them
-        List<Page> list = pageManager.getRecentlyAddedPages(55, "DS");
-        result.append("<p>");
-        result.append("Some stats for <b>Demonstration Space</b>:");
-        result.append("<table class=\"confluenceTable\">");
-        result.append("<thead><tr><th class=\"confluenceTh\">Page</th><th class=\"confluenceTh\">Number of children</th></tr></thead>");
-        result.append("<tbody>");
-        for (Page page : list)
-        {
-            int numberOfChildren = page.getChildren().size();
-            String pageWithChildren = "<tr><td class=\"confluenceTd\">" + TextUtils.htmlEncode(page.getTitle()) + "</td><td class=\"confluenceTd\" style=\"text-align:right\">" + numberOfChildren + "</td></tr>";
-            result.append(pageWithChildren);
-        }
-        result.append("</tbody>");
-        result.append("</table>");
-        result.append("</p>");
-
-        // and show the number of all spaces in this installation.
-        String spaces = "<p>Altogether, this installation has <b>" + spaceManager.getAllSpaces().size() + "</b> spaces.</p>";
-        result.append(spaces);
-
-        // this concludes our little demo. Now you should understand the basics of code injection use in Confluence, and how
-        // to get a really simple macro running.
-
-        return result.toString();
-    }
-
-    @Override
-    public BodyType getBodyType()
-    {
-        return BodyType.NONE;
-    }
-
-    @Override
-    public OutputType getOutputType()
-    {
-        return OutputType.BLOCK;
-    }
-
-}

File src/main/java/com/atlascamp/mywork/ril/PluginEnabledListener.java

+package com.atlascamp.mywork.ril;
+
+import com.atlassian.crowd.embedded.api.CrowdService;
+import com.atlassian.crowd.search.EntityDescriptor;
+import com.atlassian.crowd.search.builder.QueryBuilder;
+import com.atlassian.crowd.search.query.entity.EntityQuery;
+import com.atlassian.event.api.EventListener;
+import com.atlassian.event.api.EventPublisher;
+import com.atlassian.mywork.model.Notification;
+import com.atlassian.mywork.model.NotificationBuilder;
+import com.atlassian.mywork.service.NotificationService;
+import com.atlassian.plugin.event.events.PluginEnabledEvent;
+import org.springframework.beans.factory.DisposableBean;
+
+/**
+ * When this plugin is enabled, the listener
+ * fires a notification to all users, letting
+ * them know they should try out the features
+ * of the plugin.
+ */
+public class PluginEnabledListener implements DisposableBean
+{
+    private static final String MY_PLUGIN_KEY = "com.atlascamp.mywork.ril.mywork-ril";
+
+    private final EventPublisher eventPublisher;
+    private final NotificationService notificationService;
+    private final CrowdService crowdService;
+
+    public PluginEnabledListener(EventPublisher eventPublisher, NotificationService notificationService, CrowdService crowdService)
+    {
+        this.eventPublisher = eventPublisher;
+        this.notificationService = notificationService;
+        this.crowdService = crowdService;
+        eventPublisher.register(this);
+    }
+
+    @EventListener
+    public void onPluginEnabled(PluginEnabledEvent event)
+    {
+        if (event.getPlugin().getKey().equals(MY_PLUGIN_KEY))
+        {
+            broadcast(new NotificationBuilder()
+                    .itemIconUrl("http://png.findicons.com/files/icons/1933/symbols/16/star.png")
+                    .itemTitle("Try the 'Read It Later' plugin")
+                    .title("Congratulations! Your admin installed the 'Read It Later' plugin.")
+                    .iconUrl("http://findicons.com/files/icons/42/basic/32/tick.png")
+                    .description("You can try it out by clicking the 'Read It Later' link on any page.<br><br><img src='https://www.evernote.com/shard/s124/sh/65fe5ecc-c5db-4e11-a7dd-01995cc854f1/4ba562085ac575b51603b3e56a8654ef/res/8d52fcc4-83c8-40a9-b62c-17e72115c10d/WorkBox_AtlasCamp_2012-20120912-113551.jpg.jpg'/><br><br>A new task will appear in your WorkBox reminding you to read the page at a later time.")
+                    .createNotification());
+        }
+    }
+
+    public void broadcast(Notification notification)
+    {
+        // todo: only send the notification to a list of users that haven't yet been sent the notification
+        // we could store the list of notified users in the database using PluginSettings, Bandana,
+        // ActiveObjects or as a property on the user
+
+        for (String username : getAllUsernames())
+        {
+            notificationService.createOrUpdate(username, notification);
+        }
+    }
+
+    public Iterable<String> getAllUsernames()
+    {
+        return crowdService.search(QueryBuilder.queryFor(String.class, EntityDescriptor.user()).returningAtMost(EntityQuery.ALL_RESULTS));
+    }
+
+    @Override
+    public void destroy() throws Exception
+    {
+        eventPublisher.unregister(this);
+    }
+}

File src/main/resources/atlassian-plugin.xml

         <vendor name="${project.organization.name}" url="${project.organization.url}" />
     </plugin-info>
 
+    <component key="pluginEnabledListener" class="com.atlascamp.mywork.ril.PluginEnabledListener"/>
 
+    <component-import key="eventPublisher" interface="com.atlassian.event.api.EventPublisher"/>
+    <component-import key="crowdService" interface="com.atlassian.crowd.embedded.api.CrowdService"/>
+    <component-import key="notificationService" interface="com.atlassian.mywork.service.NotificationService"/>
 
 </atlassian-plugin>