You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2014/02/08 18:16:49 UTC

svn commit: r1566057 - /wookie/trunk/wookie-server/src/main/java/org/apache/wookie/ajaxmodel/impl/WidgetAPIImpl.java

Author: scottbw
Date: Sat Feb  8 17:16:49 2014
New Revision: 1566057

URL: http://svn.apache.org/r1566057
Log:
Use SPIs instead of persistence manager for Ajax API.

Modified:
    wookie/trunk/wookie-server/src/main/java/org/apache/wookie/ajaxmodel/impl/WidgetAPIImpl.java

Modified: wookie/trunk/wookie-server/src/main/java/org/apache/wookie/ajaxmodel/impl/WidgetAPIImpl.java
URL: http://svn.apache.org/viewvc/wookie/trunk/wookie-server/src/main/java/org/apache/wookie/ajaxmodel/impl/WidgetAPIImpl.java?rev=1566057&r1=1566056&r2=1566057&view=diff
==============================================================================
--- wookie/trunk/wookie-server/src/main/java/org/apache/wookie/ajaxmodel/impl/WidgetAPIImpl.java (original)
+++ wookie/trunk/wookie-server/src/main/java/org/apache/wookie/ajaxmodel/impl/WidgetAPIImpl.java Sat Feb  8 17:16:49 2014
@@ -24,17 +24,16 @@ import javax.servlet.http.HttpServletReq
 import org.apache.log4j.Logger;
 import org.apache.wookie.Messages;
 import org.apache.wookie.ajaxmodel.IWidgetAPI;
+import org.apache.wookie.auth.AuthToken;
+import org.apache.wookie.auth.AuthTokenUtils;
+import org.apache.wookie.auth.InvalidAuthTokenException;
 import org.apache.wookie.w3c.IDescription;
 import org.apache.wookie.w3c.IName;
 import org.apache.wookie.beans.IPreference;
 import org.apache.wookie.beans.IWidget;
-import org.apache.wookie.beans.IWidgetInstance;
-import org.apache.wookie.beans.util.IPersistenceManager;
-import org.apache.wookie.beans.util.PersistenceManagerFactory;
-import org.apache.wookie.controller.PropertiesController;
-import org.apache.wookie.queues.QueueManager;
-import org.apache.wookie.server.ContextListener;
 import org.apache.wookie.server.LocaleHandler;
+import org.apache.wookie.services.PreferencesService;
+import org.apache.wookie.services.WidgetMetadataService;
 import org.apache.wookie.util.WidgetFormattingUtils;
 import org.apache.wookie.w3c.util.LocalizationUtils;
 import org.directwebremoting.WebContextFactory;
@@ -54,17 +53,18 @@ public class WidgetAPIImpl implements IW
 	 * @see org.apache.wookie.ajaxmodel.IWidgetAPI#preferences(java.lang.String)
 	 */
 	public List<IPreference> preferences(String id_key) {
-	  ArrayList<IPreference> prefs = new ArrayList<IPreference>();
-	  if(id_key == null) return prefs;
-	  // check if instance is valid
-	  IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
-	  IWidgetInstance widgetInstance = persistenceManager.findWidgetInstanceByIdKey(id_key);
-	  if(widgetInstance==null) return prefs;
-	  
-	  for (IPreference pref: widgetInstance.getPreferences()){
-	    prefs.add(new PreferenceDelegate(pref));		  
-	  }
-	  return prefs;
+		ArrayList<IPreference> prefs = new ArrayList<IPreference>();
+
+		try {
+			AuthToken authToken = AuthTokenUtils.decryptAuthToken(id_key);
+			for (IPreference pref: PreferencesService.Factory.getInstance().getPreferences(authToken.toString())){
+				prefs.add(new PreferenceDelegate(pref));		  
+			}
+			return prefs;
+		} catch (InvalidAuthTokenException e) {
+			return prefs;
+		}
+
 	}
 
 	/* (non-Javadoc)
@@ -72,60 +72,62 @@ public class WidgetAPIImpl implements IW
 	 */
 	public Map<String, String> metadata(String id_key) {
 		Map<String, String> map = new HashMap<String, String>();
-		if(id_key == null) return map;
-	
-		// check if instance is valid
-        IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
-        IWidgetInstance widgetInstance = persistenceManager.findWidgetInstanceByIdKey(id_key);
-		if(widgetInstance==null) return map;
-		
-		// Get i18n-enabled metadata for the Widget's locale and encode it using unicode control characters.
+		AuthToken authToken = null;
 		
-		String locales[] = {widgetInstance.getLang()};
-		IWidget widget = widgetInstance.getWidget();
+		try {
+			authToken = AuthTokenUtils.decryptAuthToken(id_key);
+			// Get i18n-enabled metadata for the Widget's locale and encode it using unicode control characters.
 			
-		String author = "";
-        String email = "";
-        String href = "";
-		if (widget.getAuthor() != null){
-            if (widget.getAuthor().getAuthorName() != null) author = WidgetFormattingUtils.getEncoded(widget.getAuthor().getDir(), widget.getAuthor().getAuthorName());
-	        if (widget.getAuthor().getEmail() != null) email = widget.getAuthor().getEmail();
-	        if (widget.getAuthor().getHref() != null) href = widget.getAuthor().getHref();
-		}
+			String locales[] = {authToken.getLang()};
+			IWidget widget = WidgetMetadataService.Factory.getInstance().getWidget(authToken.getWidgetId());
+				
+			String author = "";
+	        String email = "";
+	        String href = "";
+			if (widget.getAuthor() != null){
+	            if (widget.getAuthor().getAuthorName() != null) author = WidgetFormattingUtils.getEncoded(widget.getAuthor().getDir(), widget.getAuthor().getAuthorName());
+		        if (widget.getAuthor().getEmail() != null) email = widget.getAuthor().getEmail();
+		        if (widget.getAuthor().getHref() != null) href = widget.getAuthor().getHref();
+			}
+
+			String name = "";
+			IName iname = (IName)LocalizationUtils.getLocalizedElement(widget.getNames().toArray(new IName[widget.getNames().size()]), locales, widget.getDefaultLocale());
+			if (iname != null && iname.getName() != null) name = WidgetFormattingUtils.getEncoded(iname.getDir(), iname.getName());
+			String shortName = "";
+			if (iname != null && iname.getShort() != null) shortName = WidgetFormattingUtils.getEncoded(iname.getDir(), iname.getShort());
+			
+			String description = "";
+			IDescription idescription = (IDescription)LocalizationUtils.getLocalizedElement(widget.getDescriptions().toArray(new IDescription[widget.getDescriptions().size()]), locales, widget.getDefaultLocale());
+			if (idescription != null && idescription.getDescription() != null) description = WidgetFormattingUtils.getEncoded(idescription.getDir(), idescription.getDescription());
+			
+			String version = "";
+			if (widget.getVersion() != null) version = WidgetFormattingUtils.getEncoded(widget.getDir(), widget.getVersion());
+			
+			String width = "0";
+			if (widget.getWidth() != null) width = String.valueOf(widget.getWidth());
+			
+			String height = "0";
+			if (widget.getHeight() != null) height = String.valueOf(widget.getHeight());
+			
+			// Add in metadata
 
-		String name = "";
-		IName iname = (IName)LocalizationUtils.getLocalizedElement(widget.getNames().toArray(new IName[widget.getNames().size()]), locales, widget.getDefaultLocale());
-		if (iname != null && iname.getName() != null) name = WidgetFormattingUtils.getEncoded(iname.getDir(), iname.getName());
-		String shortName = "";
-		if (iname != null && iname.getShort() != null) shortName = WidgetFormattingUtils.getEncoded(iname.getDir(), iname.getShort());
-		
-		String description = "";
-		IDescription idescription = (IDescription)LocalizationUtils.getLocalizedElement(widget.getDescriptions().toArray(new IDescription[widget.getDescriptions().size()]), locales, widget.getDefaultLocale());
-		if (idescription != null && idescription.getDescription() != null) description = WidgetFormattingUtils.getEncoded(idescription.getDir(), idescription.getDescription());
-		
-		String version = "";
-		if (widget.getVersion() != null) version = WidgetFormattingUtils.getEncoded(widget.getDir(), widget.getVersion());
-		
-		String width = "0";
-		if (widget.getWidth() != null) width = String.valueOf(widget.getWidth());
-		
-		String height = "0";
-		if (widget.getHeight() != null) height = String.valueOf(widget.getHeight());
+			map.put("id", String.valueOf(widget.getIdentifier()));	//$NON-NLS-1$
+			map.put("author", author);	//$NON-NLS-1$
+			map.put("authorEmail", email);//$NON-NLS-1$
+			map.put("authorHref", href);//$NON-NLS-1$
+			map.put("name", name);//$NON-NLS-1$
+			map.put("description", description);//$NON-NLS-1$	
+			map.put("shortName", shortName); //$NON-NLS-1$
+			map.put("version",version);//$NON-NLS-1$
+			map.put("width", width);//$NON-NLS-1$
+			map.put("height", height);//$NON-NLS-1$
+			
+			return map;
+		} catch (InvalidAuthTokenException e) {
+			return map;
+		}
 		
-		// Add in metadata
-
-		map.put("id", String.valueOf(widget.getIdentifier()));	//$NON-NLS-1$
-		map.put("author", author);	//$NON-NLS-1$
-		map.put("authorEmail", email);//$NON-NLS-1$
-		map.put("authorHref", href);//$NON-NLS-1$
-		map.put("name", name);//$NON-NLS-1$
-		map.put("description", description);//$NON-NLS-1$	
-		map.put("shortName", shortName); //$NON-NLS-1$
-		map.put("version",version);//$NON-NLS-1$
-		map.put("width", width);//$NON-NLS-1$
-		map.put("height", height);//$NON-NLS-1$
 		
-		return map;
 	}
 	
   /*
@@ -141,16 +143,14 @@ public class WidgetAPIImpl implements IW
   public String setPreferenceForKey(String id_key, String key, String value) {
     HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
     Messages localizedMessages = LocaleHandler.localizeMessages(request);   
-    IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();   
-    IWidgetInstance widgetInstance = persistenceManager.findWidgetInstanceByIdKey(id_key);
-    if (widgetInstance == null) return localizedMessages.getString("WidgetAPIImpl.0");
-    if(ContextListener.usePreferenceInstanceQueues){
-      QueueManager.getInstance().queueSetPreferenceRequest(id_key, key, value); 
-    }
-    else{
-      PropertiesController.updatePreference(widgetInstance, key, value);
-    }
-    return "okay"; //$NON-NLS-1$
+    
+    try {
+		AuthToken authToken = AuthTokenUtils.decryptAuthToken(id_key);
+		PreferencesService.Factory.getInstance().setPreference(authToken.toString(), key, value);
+	    return "okay"; //$NON-NLS-1$
+	} catch (InvalidAuthTokenException e) {
+		return localizedMessages.getString("WidgetAPIImpl.0");
+	}
   }