Author: [email protected]
Date: Mon Aug 15 14:33:41 2011
New Revision: 1318

Log:
[AMDATUOPENSOCIAL-88] Implemented the "Add" button. Next todo is implementing 
the remove button.

Modified:
   
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/dashboard.js.jsp
   
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
   
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/static/html/templates.html
   
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementRESTServiceImpl.java

Modified: 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/dashboard.js.jsp
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/dashboard.js.jsp
        (original)
+++ 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/dashboard.js.jsp
        Mon Aug 15 14:33:41 2011
@@ -177,6 +177,27 @@
       }
     });
 
+    $('.newgadget').live('click', function() {
+      var uri = document.getElementById('newgadgeturi').value;
+      var catId = $('.selectcategory.selected').attr("id");
+      var addToStore = document.getElementById('addtostore').checked;
+
+      var widget;
+      if (addToStore) {
+        widget = addGadgetToRepository(uri, catId);
+      } else {
+        widget = getGadget(uri);
+      }
+
+      if (widget != 'undefined') {
+        addWidgetToAppData(widget, startId++, dashboard);
+      }
+
+      dashboard.log('dashboardCloseWidgetDialog event thrown',2);
+      dashboard.element.trigger('dashboardCloseWidgetDialog');
+      return false;
+    });
+
 
     dashboard.element.live('dashboardAddWidget',function(e, obj){
       addWidgetToAppData(obj.widget, startId++, dashboard);

Modified: 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
  (original)
+++ 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
  Mon Aug 15 14:33:41 2011
@@ -45,6 +45,7 @@
 addWidgetToAppData = function(obj, startId, dashboard) {
   var currentGadgets = 
retrieveCurrentWidgetsInAppData(obj.metadata.securetoken);
   var gadgetUrl = obj.metadata.gadgeturl + "";
+  var gadgetId = encodeURIComponent(startId + '-' + gadgetUrl);
 
   // use appData opensocial call to add this gadget to the users appData
   var postdata = '{"registeredgadgets":"';
@@ -101,3 +102,41 @@
     }
   });
 }
+
+// Adds a gadget to the gadget repository
+addGadgetToRepository = function(uri, catId) {
+  var newGadget;
+  var url = "${contextPath}/rest/gadgetstore/gadgets?uri="+ 
encodeURIComponent(uri) + "&category=" + encodeURIComponent(catId);
+  jQuery.ajax({
+    url: url,
+    type: "POST",
+    contentType: "application/json",
+    async:false,
+    success: function(response) {
+      newGadget = response;
+    },
+    error: function(request, textStatus, errorThrown) {
+      alert('Could not add the gadget to the store. Error: ' + errorThrown);
+    }
+  });
+  return newGadget;
+}
+
+getGadget = function(uri) {
+  var gadget;
+  var url = "${contextPath}/rest/gadgetstore/gadgets?uri="+ 
encodeURIComponent(uri);
+  jQuery.ajax({
+    url: url,
+    type: "GET",
+    contentType: "application/json",
+    async:false,
+    success: function(response) {
+      gadget = response;
+    },
+    error: function(request, textStatus, errorThrown) {
+      alert('Could not retrieve the gadget. Error: ' + errorThrown);
+    }
+  });
+  return gadget;
+
+}
\ No newline at end of file

Modified: 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/static/html/templates.html
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/static/html/templates.html
  (original)
+++ 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/static/html/templates.html
  Mon Aug 15 14:33:41 2011
@@ -75,9 +75,9 @@
   <div class="panel-body">
     <b>Add custom gadget</b>
     <div class="new-button">
-      URI:<input id="newgadgeturi" type="edit" size="65">
-      <input class="macro-button-add addwidget" id="newwidget" value="Add" 
type="button"><br/>
-      <input type="checkbox" type="button">Add to store
+      URI:<input  class="macro-hidden-uri" id="newgadgeturi" type="edit" 
size="65">
+      <input class="macro-button-add newgadget" id="newgadget" value="Add" 
type="button"><br/>
+      <input type="checkbox" type="button" id="addtostore">Add to store
       <br/><hr/><br/>
     </div>
     <ol id="category-all" class="widgets">

Modified: 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementRESTServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementRESTServiceImpl.java
       (original)
+++ 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementRESTServiceImpl.java
       Mon Aug 15 14:33:41 2011
@@ -24,6 +24,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
@@ -67,7 +68,7 @@
 
     private static final String[] GADGET_MODULEPREFS =
         new String[] {"title", "description", "author", "author_email", 
"author_affiliation", "screenshot",
-            "title_url"};
+    "title_url"};
 
     // Use the startId as prefix for the gadget id to ensure each gadget has a 
unique id
     private static int START_ID = 1;
@@ -140,7 +141,7 @@
         }
 
         return Response.ok(jsonObject.toString(), 
MediaType.APPLICATION_JSON_TYPE).cacheControl(MAXAGE_CACHE_CONTROL)
-            .build();
+        .build();
     }
 
     /**
@@ -184,7 +185,82 @@
             throw new WebApplicationException(e, 
Response.Status.INTERNAL_SERVER_ERROR);
         }
         return Response.ok(jsonObject.toString(), 
MediaType.APPLICATION_JSON_TYPE).cacheControl(NO_CACHE_CONTROL)
-            .build();
+        .build();
+    }
+
+    /**
+     * Returns a single gadget. URL to this resource:
+     * GET /rest/gadgetstore/gadgets?uri=...
+     * 
+     * @return The gadget.
+     */
+    @GET
+    @Path("gadgets")
+    @Produces({MediaType.APPLICATION_JSON})
+    public Response getGadget(@Context final HttpHeaders headers, @Context 
final HttpServletRequest request,
+        @QueryParam("uri") final String uri) {
+        try {
+            // Now generate the JSON expression to return
+            JSONObject jsonGadget = retrieveGadget(request, toAbsoluteUrl(uri, 
request));
+            if (jsonGadget != null) {
+                // Be sure to add the security token
+                String st = m_openSocialContainer.getSecurityToken(uri, 
request);
+                jsonGadget.getJSONObject("metadata").put("securetoken", st);
+                return Response.ok(jsonGadget.toString(), 
MediaType.APPLICATION_JSON_TYPE).cacheControl(NO_CACHE_CONTROL).build();
+            }
+            return 
Response.status(Response.Status.NOT_FOUND).cacheControl(NO_CACHE_CONTROL).build();
+        }
+        catch (Exception e) {
+            throw new WebApplicationException(e, 
Response.Status.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    /**
+     * Returns gadgets available in the gadget store. URL to this resource:
+     * /rest/gadgetstore/gadgets
+     * 
+     * @return All available gadgets.
+     */
+    @POST
+    @Path("gadgets")
+    @Produces({MediaType.APPLICATION_JSON})
+    public Response addGadget(@Context final HttpHeaders headers, @Context 
final HttpServletRequest request,
+        @QueryParam("uri") final String uri, @QueryParam("category") final 
String categoryId) {
+        try {
+            if (uri == null || "".equals(uri) || categoryId == null || 
"".equals(categoryId)) {
+                return 
Response.status(Response.Status.BAD_REQUEST).cacheControl(NO_CACHE_CONTROL).build();
+            }
+            GadgetCategory category = 
m_gadgetManagement.getCategory(categoryId);
+            if (category == null) {
+                // Category does not exist, return a 400 - BAD REQUEST
+                return 
Response.status(Response.Status.BAD_REQUEST).cacheControl(NO_CACHE_CONTROL).build();
+
+            }
+            for (GadgetDefinition gadget : m_gadgetManagement.getGadgets()) {
+                if (gadget.getUrl().equals(uri)) {
+                    // The gadget with this URI was already registered, return 
a 200
+                    return 
Response.status(Response.Status.OK).cacheControl(NO_CACHE_CONTROL).build();
+                }
+            }
+
+            // Create a new gadget definition
+            GadgetDefinition gadget = new GadgetDefinition(uri, category, 
false);
+            m_gadgetManagement.addGadget(gadget);
+
+            // Now generate the JSON expression to return
+            JSONObject jsonGadget = retrieveGadget(request, toAbsoluteUrl(uri, 
request));
+            if (jsonGadget != null) {
+                // Be sure to add the security token
+                String st = m_openSocialContainer.getSecurityToken(uri, 
request);
+                jsonGadget.getJSONObject("metadata").put("securetoken", st);
+
+                return Response.ok(jsonGadget.toString(), 
MediaType.APPLICATION_JSON_TYPE).cacheControl(NO_CACHE_CONTROL).build();
+            }
+            return 
Response.status(Response.Status.NOT_FOUND).cacheControl(NO_CACHE_CONTROL).build();
+        }
+        catch (Exception e) {
+            throw new WebApplicationException(e, 
Response.Status.INTERNAL_SERVER_ERROR);
+        }
     }
 
     /**
@@ -273,11 +349,11 @@
         }
 
         return Response.ok(jsonObject.toString(), 
MediaType.APPLICATION_JSON_TYPE).cacheControl(NO_CACHE_CONTROL)
-            .build();
+        .build();
     }
-    
+
     private String[] getDefaultGadgetUrls(final HttpServletRequest request) {
-     // No gadgets are associated with this user, by default we assign the 
default gadgets (duh)
+        // No gadgets are associated with this user, by default we assign the 
default gadgets (duh)
         GadgetDefinition[] defaultGadgets = 
m_gadgetManagement.getDefaultGadgets();
 
         // Normalize the URLs from potential relative URLs (as stored in the 
gadget store)
@@ -299,7 +375,7 @@
     }
 
     private JSONObject retrieveGadget(final HttpServletRequest request, final 
String gadgetUrl) throws JSONException,
-        NoSuchAlgorithmException, UnsupportedEncodingException {
+    NoSuchAlgorithmException, UnsupportedEncodingException {
         Map<String, String> gadgetSpec = 
m_openSocialContainer.getGadgetSpec(request, gadgetUrl);
         if (gadgetSpec != null) {
             JSONObject gadget = new JSONObject();
@@ -324,7 +400,7 @@
                     gadget.put("author", gadgetSpec.get("author_email"));
                 }
                 else if (!"".equals(gadgetSpec.get("author_affiliation"))
-                            && gadgetSpec.get("author_affiliation") != null) {
+                                && gadgetSpec.get("author_affiliation") != 
null) {
                     gadget.put("author", gadgetSpec.get("author_affiliation"));
                 }
                 else {
@@ -344,7 +420,7 @@
     }
 
     private void setGadgetId(final JSONObject gadget, final String gadgetId, 
final boolean generateStartId)
-        throws JSONException {
+    throws JSONException {
         String id, url;
         if (generateStartId) {
             // For new default gadgets, use generated startId
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to