Colin Chauvet avatar Colin Chauvet committed f2014a2

Added list of past notifications on admin page

Comments (0)

Files changed (4)

src/main/java/com/example/plugins/tutorial/confluence/notification/resource/NotificationResource.java

 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.FormParam;
+import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 
 import com.atlassian.mywork.model.Notification;
 import com.atlassian.mywork.model.NotificationBuilder;
-import com.atlassian.mywork.service.NotificationService;
+import com.atlassian.mywork.service.LocalNotificationService;
+
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.node.ObjectNode;
 
 
 /**
 @Produces (MediaType.APPLICATION_JSON)
 public class NotificationResource {
 
-    private final NotificationService notificationService;
+    private final LocalNotificationService notificationService;
 
-    public NotificationResource(final NotificationService notificationService) {
+    public NotificationResource(final LocalNotificationService notificationService) {
         this.notificationService = notificationService;
     }
 
     // TODO handle exception
     public Response createNotification(@FormParam ("title") String title, @FormParam ("message") String message) throws Exception{
 
-//        ObjectNode metadata = new ObjectMapper().createObjectNode();
-//        metadata.put("user", "Some Body");
-//        metadata.put("username", "test");
-//        metadata.put("custom", "My Example");
+        ObjectNode metadata = new ObjectMapper().createObjectNode();
+        metadata.put("user", "Some Body");
+        metadata.put("username", "test");
+        metadata.put("custom", "My Example");
         Notification notification = notificationService.createOrUpdate("admin", new NotificationBuilder()
                 .application("com.example.plugins.tutorial.confluence.notification")
                 .title("Message from your beloved administrator")
                 .description(message)
 //                .entity("foo")
 //                .action("bar")
-//                .globalId("something" )
-//                .url("http://www.google.com/")
-//                .metadata(metadata)
+                .globalId("com.example.plugins.tutorial.confluence.notification")
+                .url("http://www.google.com/")
+                .metadata(metadata)
 //                .applicationLinkId("TODO_CHANGE_ME")
                 .createNotification()).get();
         return Response.ok(notification).build();
+    }
+
+    @GET
+    public Response findAllNotifications() throws Exception{
 
+        final Iterable<Notification> notifications = notificationService.find("admin", "com.example.plugins.tutorial.confluence.notification");
+
+        return Response.ok(notifications).build();
     }
 
+
 }

src/main/resources/atlassian-plugin.xml

     <component-import key="renderer"
                       interface="com.atlassian.templaterenderer.velocity.one.six.VelocityTemplateRenderer"/>
     <component-import key="permissionManager" interface="com.atlassian.confluence.security.PermissionManager"/>
-    <component-import key="notificationService" interface="com.atlassian.mywork.service.NotificationService"/>
+    <component-import key="notificationService" interface="com.atlassian.mywork.service.LocalNotificationService"/>
     <resource type="i18n" name="i18n"
               location="com.example.plugins.tutorial.confluence.tutorial-confluence-notification"/>
     <servlet name="Admin Servlet" i18n-name-key="admin-servlet.name" key="admin-servlet"

src/main/resources/js/admin.js

 
     var baseUrl = AJS.contextPath();
 
+    var renderPastNotifications = function ()
+    {
+        AJS.$.ajax({
+            url:baseUrl + "/rest/notification/latest/",
+            type:"GET",
+            success:function (notifications)
+            {
+                var tbody = AJS.$("#notification-list tbody");
+                tbody.empty();
+                AJS.$("#notification-list-body").css("visibility", notifications.length > 0 ? "visible" : "hidden");
+
+                for (var i = 0; i < notifications.length; i++)
+                {
+                    var notification = notifications[i];
+                    tbody.append(renderNotification(notification));
+                }
+            },
+            error:function (error)
+            {
+                console.log("An error happenned while rendering past notifications. " + error);
+            }
+        });
+    }
+
+    var renderNotification = function (notification)
+    {
+        return AJS.template.load("notification-template").fill({
+            title:notification.item.title,
+            message:notification.description,
+            date:new Date(notification.created)
+        })
+    }
+
     var submitForm = function ()
     {
         console.log("Sending notification...");
             type:"POST",
             contentType:"application/json; charset=utf-8",
             data:JSON.stringify({
-                title: [AJS.$("#notification-title").val()],
+                title:[AJS.$("#notification-title").val()],
                 message:[AJS.$("#notification-message").val()]
             }),
             success:function (data)
             {
                 console.log("Ajax call was successful !")
+                renderPastNotifications();
             },
             error:function (error)
             {
         return false;
     });
 
+    renderPastNotifications();
+
 });
 

src/main/resources/templates/admin.vm

 
 <body>
 
+<script type="text/x-template" title="notification-template">
+    <tr id={id} class="notification">
+        <td headers="notification-list-title">{title}</td>
+        <td headers="notification-list-message">{message}</td>
+        <td headers="notification-list-date">{date}</td>
+    </tr>
+</script>
+
 <form id="notification" class="aui">
     <h2>Confluence Notifier Administration</h2>
 
-
     <div>
         <label for="notification-title">Title</label>
         <input type="text" id="notification-title" name="notification-title">
         <input id="submit" type="submit" value="Notify">
     </div>
 
+    <div id="notification-list-body">
+        <h2>Sent notifications</h2>
+
+        <table class='aui' id='notification-list'>
+            <thead>
+            <tr>
+                <th id="notification-list-title">Title</th>
+                <th id="notification-list-message">Message</th>
+                <th id="notification-list-date">Date</th>
+            </tr>
+            </thead>
+            <tbody>
+            </tbody>
+        </table>
+    </div>
 
 </form>
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.