rave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jc...@apache.org
Subject svn commit: r1148926 - in /incubator/rave/trunk/rave-portal/src: main/java/org/apache/rave/portal/model/util/ main/java/org/apache/rave/provider/opensocial/web/renderer/ main/webapp/WEB-INF/views/ main/webapp/script/ test/java/org/apache/rave/portal/mo...
Date Wed, 20 Jul 2011 20:14:45 GMT
Author: jcian
Date: Wed Jul 20 20:14:44 2011
New Revision: 1148926

URL: http://svn.apache.org/viewvc?rev=1148926&view=rev
Log:
Finishing client side support for saving user preferences (RAVE-27) -- this finishes off the
major effort around support for saving user preferences and leaves only a few small items
to cleanup which I'll open subsequent JIRA tickets for.  Saving preferences for any opensocial
gadget which has end user customizable preferences should now be working. 

https://issues.apache.org/jira/browse/RAVE-27

Modified:
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/ModelUtils.java
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRenderer.java
    incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/home.jsp
    incubator/rave/trunk/rave-portal/src/main/webapp/script/rave.js
    incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_api.js
    incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_opensocial.js
    incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/ModelUtilsTest.java
    incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRendererTest.java

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/ModelUtils.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/ModelUtils.java?rev=1148926&r1=1148925&r2=1148926&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/ModelUtils.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/ModelUtils.java
Wed Jul 20 20:14:44 2011
@@ -25,7 +25,7 @@ import java.util.List;
 
 public class ModelUtils {
 
-    private ModelUtils(){};
+    private ModelUtils(){}
 
     public static void normalizeRegionWidgetPreferences(long regionWidgetId, List<RegionWidgetPreference>
preferences) {
         for (RegionWidgetPreference preference : preferences) {
@@ -35,6 +35,5 @@ public class ModelUtils {
 
     public static void normalizeRegionWidgetPreference(long regionWidgetId, RegionWidgetPreference
regionWidgetPreference) {
         regionWidgetPreference.setRegionWidgetId(regionWidgetId);
-        regionWidgetPreference.setName(regionWidgetPreference.getName().toLowerCase());
     }
 }
\ No newline at end of file

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRenderer.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRenderer.java?rev=1148926&r1=1148925&r2=1148926&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRenderer.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRenderer.java
Wed Jul 20 20:14:44 2011
@@ -21,9 +21,14 @@ package org.apache.rave.provider.opensoc
 
 import org.apache.rave.exception.NotSupportedException;
 import org.apache.rave.portal.model.RegionWidget;
+import org.apache.rave.portal.model.RegionWidgetPreference;
 import org.apache.rave.portal.web.renderer.RegionWidgetRenderer;
 import org.apache.rave.provider.opensocial.Constants;
 import org.apache.rave.provider.opensocial.service.OpenSocialService;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -34,6 +39,8 @@ import org.springframework.stereotype.Co
  */
 @Component
 public class OpenSocialWidgetRenderer implements RegionWidgetRenderer {
+    private static Logger logger = LoggerFactory.getLogger(OpenSocialWidgetRenderer.class);
+
     private OpenSocialService openSocialService;
 
     @Autowired
@@ -48,7 +55,7 @@ public class OpenSocialWidgetRenderer im
             " regionWidgetId: %2$s," +
             " widgetUrl: '%3$s', " +
             " metadata: %4$s," +
-            " userPrefs: {}});";
+            " userPrefs: %5$s});";
 
     @Override
     public String getSupportedContext() {
@@ -68,7 +75,18 @@ public class OpenSocialWidgetRenderer im
             throw new NotSupportedException("Invalid widget type passed to renderer: " +
type);
         }
 
+        JSONObject userPrefs = new JSONObject();
+        if (item.getPreferences() != null) {
+            for (RegionWidgetPreference regionWidgetPreference : item.getPreferences()) {
+                try {
+                    userPrefs.put(regionWidgetPreference.getName(), regionWidgetPreference.getValue());
+                } catch (JSONException e) {
+                    logger.error("Exception caught adding preference to JSONObject: " + regionWidgetPreference,
e);
+                }
+            }
+        }
+
         return String.format(IFRAME_MARKUP, Constants.WIDGET_TYPE, item.getId(), item.getWidget().getUrl(),
-                openSocialService.getGadgetMetadata(item.getWidget().getUrl()));
+                openSocialService.getGadgetMetadata(item.getWidget().getUrl()), userPrefs.toString());
     }
-}
+}
\ No newline at end of file

Modified: incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/home.jsp
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/home.jsp?rev=1148926&r1=1148925&r2=1148926&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/home.jsp (original)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/home.jsp Wed Jul 20 20:14:44
2011
@@ -69,6 +69,7 @@
         </c:forEach>
         <div class="clear-float">&nbsp;</div>
     </div>
+    <script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
     <script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
     <script src="//ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.min.js"></script>
     <script src="${opensocial_engine_url}/js/container.js?c=1&container=default&debug=1"></script>

Modified: incubator/rave/trunk/rave-portal/src/main/webapp/script/rave.js
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/script/rave.js?rev=1148926&r1=1148925&r2=1148926&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/script/rave.js (original)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/script/rave.js Wed Jul 20 20:14:44 2011
@@ -241,17 +241,37 @@ var rave = rave || (function() {
         }
 
         function saveEditPrefsAction(args) {
-            var prefsElement = $("#" + WIDGET_PREFS_CONTENT(args.data.id));
+            var regionWidget = getWidgetById(args.data.id);
+            var prefsElement = $("#" + WIDGET_PREFS_CONTENT(regionWidget.regionWidgetId));
+
+            var updatedPrefs = {};
+            prefsElement.find("*").filter(":input").each(function(index, element) {
+                switch (element.type) {
+                    case "text":
+                    case "select-one":
+                    case "hidden":
+                        updatedPrefs[element.name] = $(element).val();
+                        break;
+                    case "checkbox":
+                        updatedPrefs[element.name] = $(element).is(':checked').toString();
+                        break;
+                    case "textarea":
+                        var valuesToPersist = [];
+                        var textareaValues = $(element).val().split("\n");
+                        for (var i = 0; i < textareaValues.length; i++) {
+                            var value = $.trim(textareaValues[i]);
+                            if (value.length > 0) {
+                                valuesToPersist.push(value);
+                            }
+                        }
+                        updatedPrefs[element.name] = valuesToPersist.join("|");
+                        break;
+                }
+            });
 
-            /**
-             -- Update preferences in the widget object
-             -- Send a batch save to the rest API
-             -- Re-render the widget with the updated preferences
-
-             $("#widget-17-prefs-content").find("*").filter(":input").each(function(index,
element){
-                console.log(element.nodeName, element.type);
-             });
-             */
+            if(typeof regionWidget.savePreferences == "function") {
+                regionWidget.savePreferences(updatedPrefs);
+            }
 
             prefsElement.html("");
             prefsElement.hide();

Modified: incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_api.js
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_api.js?rev=1148926&r1=1148925&r2=1148926&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_api.js (original)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_api.js Wed Jul 20 20:14:44
2011
@@ -22,6 +22,40 @@ var rave = rave || {};
  * Note required jquery libraries must be imported by the containing page.
  */
 rave.api = rave.api || (function() {
+    function handleError(jqXhr, status, error) {
+        alert("Rave encountered an error while trying to contact the server.  Please reload
the page and try again. error: " + error);
+    }
+
+    var restApi = (function() {
+        //Base path to RPC services
+        var path = "api/rest/";
+
+        function saveWidgetPreferences(args) {
+            var preferencesData = {"preferences": []};
+            for (var prefName in args.userPrefs) {
+                preferencesData.preferences.push({"name":prefName, "value":args.userPrefs[prefName]});
+            }
+
+            $.ajax({
+                type: 'PUT',
+                url: rave.getContext() + path + "regionWidgets/" + args.regionWidgetId +
"/preferences",
+                data: JSON.stringify(preferencesData),
+                contentType: 'application/json',
+                dataType: 'json',
+                success: function(result) {
+                    if (typeof args.successCallback == 'function') {
+                        args.successCallback();
+                    }
+                },
+                error: handleError
+            });
+        }
+
+        return {
+            saveWidgetPreferences : saveWidgetPreferences
+        };
+    })();
+
     var rpcApi = (function() {
         //Base path to RPC services
         var path = "api/rpc/";
@@ -80,10 +114,6 @@ rave.api = rave.api || (function() {
                 }).error(handleError);
         }
 
-        function handleError(jqXhr, status, error) {
-            alert("Rave encountered an error while trying to contact the server.  Please
reload the page and try again. error: " + error);
-        }
-
         //TODO: Create a more robust error handling system and interrogation of RPC results
         function handleRpcError(rpcResult) {
             switch (rpcResult.errorCode) {
@@ -109,6 +139,7 @@ rave.api = rave.api || (function() {
     })();
 
     return {
+        rest : restApi,
         rpc : rpcApi
     };
 })();

Modified: incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_opensocial.js
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_opensocial.js?rev=1148926&r1=1148925&r2=1148926&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_opensocial.js (original)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/script/rave_opensocial.js Wed Jul 20
20:14:44 2011
@@ -96,6 +96,11 @@ rave.opensocial = rave.opensocial || (fu
         gadget.site = container.newGadgetSite(widgetBodyElement);
         gadget.maximize = function() { renderGadgetView("canvas", this); };
         gadget.minimize = function() { renderGadgetView("home",   this); };
+        gadget.savePreferences = function(userPrefs) {
+            this.userPrefs = userPrefs;
+            rave.api.rest.saveWidgetPreferences({regionWidgetId: this.regionWidgetId, userPrefs:
userPrefs});
+            renderGadgetView("home", this); //TODO: figure out how to get the current view
instead of assuming "home"
+        };
         renderGadgetView("home", gadget);
     }
 

Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/ModelUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/ModelUtilsTest.java?rev=1148926&r1=1148925&r2=1148926&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/ModelUtilsTest.java
(original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/ModelUtilsTest.java
Wed Jul 20 20:14:44 2011
@@ -25,7 +25,6 @@ import org.junit.Test;
 import java.util.Arrays;
 import java.util.List;
 
-import static org.apache.commons.lang.StringUtils.isAllLowerCase;
 import static org.junit.Assert.assertTrue;
 
 public class ModelUtilsTest {
@@ -36,7 +35,6 @@ public class ModelUtilsTest {
         RegionWidgetPreference testPreference = new RegionWidgetPreference(null, null, "camelCaseName",
"FOO");
         ModelUtils.normalizeRegionWidgetPreference(VALID_REGION_WIDGET_ID, testPreference);
 
-        assertTrue(isAllLowerCase(testPreference.getName()));
         assertTrue(testPreference.getRegionWidgetId() == VALID_REGION_WIDGET_ID);
     }
 
@@ -45,7 +43,6 @@ public class ModelUtilsTest {
         List<RegionWidgetPreference> testPreferences = getTestRegionWidgetPreferences();
         ModelUtils.normalizeRegionWidgetPreferences(VALID_REGION_WIDGET_ID, testPreferences);
         for (RegionWidgetPreference testPreference : testPreferences) {
-            assertTrue(isAllLowerCase(testPreference.getName()));
             assertTrue(testPreference.getRegionWidgetId() == VALID_REGION_WIDGET_ID);
         }
     }
@@ -55,5 +52,4 @@ public class ModelUtilsTest {
                 new RegionWidgetPreference(null, 20L, "lowercasename", "FOO"),
                 new RegionWidgetPreference(null, -100L, "UPPERCASENAME", "FOO"));
     }
-
 }
\ No newline at end of file

Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRendererTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRendererTest.java?rev=1148926&r1=1148925&r2=1148926&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRendererTest.java
(original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/provider/opensocial/web/renderer/OpenSocialWidgetRendererTest.java
Wed Jul 20 20:14:44 2011
@@ -21,13 +21,18 @@ package org.apache.rave.provider.opensoc
 
 import org.apache.rave.exception.NotSupportedException;
 import org.apache.rave.portal.model.RegionWidget;
+import org.apache.rave.portal.model.RegionWidgetPreference;
 import org.apache.rave.portal.model.Widget;
 import org.apache.rave.portal.web.renderer.Renderer;
 import org.apache.rave.provider.opensocial.Constants;
 import org.apache.rave.provider.opensocial.service.OpenSocialService;
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.util.Arrays;
+
 import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
@@ -54,7 +59,7 @@ public class OpenSocialWidgetRendererTes
     }
 
     @Test
-    public void render_valid() {
+    public void render_valid() throws JSONException {
         expect(openSocialService.getGadgetMetadata(VALID_GADGET_URL)).andReturn(VALID_METADATA);
         replay(openSocialService);
 
@@ -65,12 +70,26 @@ public class OpenSocialWidgetRendererTes
         RegionWidget rw = new RegionWidget();
         rw.setId(1L);
         rw.setWidget(w);
+        rw.setPreferences(Arrays.asList(new RegionWidgetPreference(1L, 1L, "color", "blue"),
+                new RegionWidgetPreference(2L, 1L, "speed", "fast")));
 
         String result = renderer.render(rw);
-        assertThat(result.matches(".*regionWidgetId[ ]*:[ ]*1,.*"), is(true));
-        assertThat(result.matches(".*type[ ]*:[ ]*'OpenSocial',.*"), is(true));
-        assertThat(result.matches(".*widgetUrl[ ]*:[ ]*'http://www.example.com/gadget.xml',.*"),
is(true));
-        assertThat(result.matches(".*metadata[ ]*:[ ]*" + VALID_METADATA + ",.*"), is(true));
+        /*
+            result should look like:
+
+            widgets.push({type: 'OpenSocial', regionWidgetId: 1, widgetUrl: 'http://www.example.com/gadget.xml',
+                metadata: metadata, userPrefs: {"speed":"fast","color":"blue"}});
+        */
+
+        JSONObject jsonObject = new JSONObject(
+                result.substring("widgets.push(".length(), result.length() - ");".length()));
+
+        assertThat((Integer) jsonObject.get("regionWidgetId"), is(equalTo(1)));
+        assertThat((String) jsonObject.get("type"), is(equalTo("OpenSocial")));
+        assertThat((String) jsonObject.get("widgetUrl"), is(equalTo("http://www.example.com/gadget.xml")));
+        assertThat((String) jsonObject.get("metadata"), is(equalTo(VALID_METADATA)));
+        assertThat((String) ((JSONObject) jsonObject.get("userPrefs")).get("color"), is(equalTo("blue")));
+        assertThat((String) ((JSONObject) jsonObject.get("userPrefs")).get("speed"), is(equalTo("fast")));
     }
 
     @Test



Mime
View raw message