Author: scottbw
Date: Sat Apr 17 17:50:58 2010
New Revision: 935216

URL: http://svn.apache.org/viewvc?rev=935216&view=rev
Log:
Enables a client to request an existing widget instance with a different 
locale, and for this updated locale to be persisted for the instance (see 
WOOKIE-135 for more details); also added a test case for the new behaviour.

Modified:
    
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java
    
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java

Modified: 
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java?rev=935216&r1=935215&r2=935216&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java
 (original)
+++ 
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java
 Sat Apr 17 17:50:58 2010
@@ -147,5 +147,77 @@ public class FolderLocalizationTest exte
                String url = WIDGET_START_URL_ROOT + resource;
                assertEquals(WIDGET_START_URL_ROOT+"locales/en/test.txt", 
getResource(url));
        }
+       
+       @Test
+       // Request the instance with different locales, and check that the 
correct
+       // resources are returned in each case (i.e. that the locale of the 
instance has changed)
+       public void updateLocalizedResources(){
+               
+        // Update the widget instance localized to French 
+        try {
+               PostMethod post = new 
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+               
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=foldertest1&shareddatakey=foldertest1&locale=fr");
+               client.executeMethod(post);
+             
+               WIDGET_START_URL_ROOT = 
getStartFile(post.getResponseBodyAsString());
+               // We have to load the start file in order to start the session
+               getResource(WIDGET_START_URL_ROOT);
+               // take off the resource bit
+               String path = 
WIDGET_START_URL_ROOT.substring(WIDGET_START_URL_ROOT.indexOf("locales"));
+               WIDGET_START_URL_ROOT= 
StringUtils.remove(WIDGET_START_URL_ROOT, path);
+               post.releaseConnection();
+           }
+           catch (Exception e) {
+               e.printStackTrace();
+           }   
+               
+               String resource = "index.htm";
+               String url = WIDGET_START_URL_ROOT + resource;
+               assertEquals(WIDGET_START_URL_ROOT+"locales/fr/index.htm", 
getResource(url));
+               
+        // Update the widget instance localized to English 
+        try {
+               PostMethod post = new 
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+               
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=foldertest1&shareddatakey=foldertest1&locale=en");
+               client.executeMethod(post);
+             
+               WIDGET_START_URL_ROOT = 
getStartFile(post.getResponseBodyAsString());
+               // We have to load the start file in order to start the session
+               getResource(WIDGET_START_URL_ROOT);
+               // take off the resource bit
+               String path = 
WIDGET_START_URL_ROOT.substring(WIDGET_START_URL_ROOT.indexOf("locales"));
+               WIDGET_START_URL_ROOT= 
StringUtils.remove(WIDGET_START_URL_ROOT, path);
+               post.releaseConnection();
+           }
+           catch (Exception e) {
+               e.printStackTrace();
+           }   
+               
+               resource = "index.htm";
+               url = WIDGET_START_URL_ROOT + resource;
+               assertEquals(WIDGET_START_URL_ROOT+"locales/en/index.htm", 
getResource(url));
+               
+            // Update the widget instance unlocalized (expecting the default 
locale here to be "en")
+        try {
+               PostMethod post = new 
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+               
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=foldertest1&shareddatakey=foldertest1");
+               client.executeMethod(post);
+             
+               WIDGET_START_URL_ROOT = 
getStartFile(post.getResponseBodyAsString());
+               // We have to load the start file in order to start the session
+               getResource(WIDGET_START_URL_ROOT);
+               // take off the resource bit
+               String path = 
WIDGET_START_URL_ROOT.substring(WIDGET_START_URL_ROOT.indexOf("locales"));
+               WIDGET_START_URL_ROOT= 
StringUtils.remove(WIDGET_START_URL_ROOT, path);
+               post.releaseConnection();
+           }
+           catch (Exception e) {
+               e.printStackTrace();
+           }   
+               
+               resource = "index.htm";
+               url = WIDGET_START_URL_ROOT + resource;
+               assertEquals(WIDGET_START_URL_ROOT+"locales/en/index.htm", 
getResource(url));
+       }
 
 }

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java?rev=935216&r1=935215&r2=935216&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
 (original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
 Sat Apr 17 17:50:58 2010
@@ -205,6 +205,16 @@ public class WidgetInstancesController e
                        instance = 
WidgetInstanceFactory.getWidgetFactory(session, 
localizedMessages).newInstance(apiKey, userId, sharedDataKey, serviceType, 
widgetId, locale);
                        response.setStatus(HttpServletResponse.SC_CREATED);
                } else {
+                       // If the requested locale is different to the saved 
locale, update the "lang" attribute
+                       // of the widget instance and save it
+                       if (
+                                       (locale == null && 
instance.getLang()!=null) || 
+                                       (locale != null && 
instance.getLang()==null) ||                                         
+                                       (locale != null && 
!instance.getLang().equals(locale))
+                       ){
+                                       instance.setLang(locale);
+                                       instance.save();
+                       }
                        response.setStatus(HttpServletResponse.SC_OK);          
        
                }
                


Reply via email to