Commits

Colin Chauvet [Atlassian] Chauvet  committed 3e7740b

First end 2 end working example

  • Participants
  • Parent commits 4707d17

Comments (0)

Files changed (3)

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

 package com.example.plugins.tutorial.confluence.notification.resource;
 
 import javax.ws.rs.Consumes;
+import javax.ws.rs.FormParam;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import com.atlassian.mywork.model.NotificationBuilder;
 import com.atlassian.mywork.service.NotificationService;
 
-import org.codehaus.jackson.node.ObjectNode;
-
-import org.codehaus.jackson.map.ObjectMapper;
-
 
 /**
  * A resource of message.
 
     @POST
     // TODO handle exception
-    public Response createNotification() throws 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.atlassian.mywork.example")
-                .title("Some title")
-                .itemTitle("Top level title")
-                .description("This has some example text")
-                .entity("foo")
-                .action("bar")
-                .globalId("something" )
-                .url("http://www.google.com/")
-                .metadata(metadata)
-                .applicationLinkId("TODO_CHANGE_ME")
+                .application("com.example.plugins.tutorial.confluence.notification")
+                .title("Message from your beloved administrator")
+                .itemTitle(title)
+                .description(message)
+//                .entity("foo")
+//                .action("bar")
+//                .globalId("something" )
+//                .url("http://www.google.com/")
+//                .metadata(metadata)
+//                .applicationLinkId("TODO_CHANGE_ME")
                 .createNotification()).get();
         return Response.ok(notification).build();
 
-//        return Response.ok("Hello World").build();
     }
 
 }

File src/main/resources/js/admin.js

-AJS.toInit(function()
+AJS.toInit(function ()
 {
 
     console.log("Hello there");
 
+    var baseUrl = AJS.contextPath();
+
+    var submitForm = function ()
+    {
+        console.log("Sending notification...");
+        AJS.$.ajax({
+            url:baseUrl + "/rest/notification/latest/",
+            type:"POST",
+            contentType:"application/json; charset=utf-8",
+            data:JSON.stringify({
+                title: [AJS.$("#notification-title").val()],
+                message:[AJS.$("#notification-message").val()]
+            }),
+            success:function (data)
+            {
+                console.log("Ajax call was successful !")
+            },
+            error:function (error)
+            {
+                console.log("Something went wrong...")
+            }
+        });
+    }
+
+    AJS.$("#submit").live('click', function (event)
+    {
+        event.stopPropagation();
+        submitForm();
+        return false;
+    });
+
 });
 

File src/main/resources/templates/admin.vm

 
 
     <div>
+        <label for="notification-title">Title</label>
+        <input type="text" id="notification-title" name="notification-title">
+    </div>
+    <div>
         <label for="notification-message">Message</label>
-        <input type="text" id="notification-message" name="message">
+        <input type="text" id="notification-message" name="notification-message">
     </div>