Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.wookie.helpers;
 
 
 import  org.apache.wookie.util.HashGenerator;
 import  org.apache.wookie.util.opensocial.OpenSocialUtils;
A class to create widget instances

Author(s):
Paul Sharples
Scott Wilson
Version:
$Id
 
 public class WidgetInstanceFactory{
 
 	static Logger _logger = Logger.getLogger(WidgetInstanceFactory.class.getName());
 	
 	private HttpSession session;
 	
 	boolean showProcess = false;

Return a handle on a Widget Factory localized for the session

Parameters:
session
localizedMessages
Returns:
the WidgetInstanceFactory
 
 	public static WidgetInstanceFactory getWidgetFactory(HttpSession sessionMessages localizedMessages){
 		WidgetInstanceFactory factory = (WidgetInstanceFactory)session.getAttribute(WidgetInstanceFactory.class.getName());				
 		if(factory == null){
 			factory = new WidgetInstanceFactory();
 			factory.localizedMessages = localizedMessages;
 			factory.session = session;
 			session.setAttribute(WidgetInstanceFactory.class.getName(), factory);
 		}
 		return factory;
 	}

Return the "default widget" instance

Returns:
an IWidgetInstance for the default widget, typically the "unsupported widget widget"
 
 	public static IWidgetInstance defaultInstance(String locale){
         IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
 		IWidgetInstance instance = persistenceManager.newInstance(IWidgetInstance.class);
 		instance.setWidget(persistenceManager.findWidgetDefaultByType("unsupported")); //$NON-NLS-1$
 		instance.setIdKey("0000");
 		instance.setLang(locale);
 		instance.setOpensocialToken("");
 		return instance;
 	}

Create a new widget instance with the given parameters

Parameters:
apiKey
userId
sharedDataKey
serviceType
widgetId
Returns:
a new WidgetInstance, or null if the instance cannot be created
 
 	public IWidgetInstance newInstance(String apiKeyString userIdString sharedDataKeyString serviceTypeString widgetIdString lang){
 		try {
 			IWidget widget;
 			IWidgetInstance widgetInstance;
 			// Widget ID or Widget Type?
 	        IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
 			if (widgetId != null){
 				widget = persistenceManager.findWidgetByGuid(widgetId);
 			} 
 			else {
 				// does this type of widget exist?
				widget = persistenceManager.findWidgetDefaultByType(serviceType);				
			}
			// Unsupported
			if (widget == nullreturn null;
			// generate a nonce
			RandomGUID r = new RandomGUID();
			String nonce = "nonce-" + r.toString();				 //$NON-NLS-1$
			// now use SHA hash on the nonce				
			String hashKey = HashGenerator.getInstance().encrypt(nonce);	
			// get rid of any chars that might upset a url...
			hashKey = hashKey.replaceAll("="".eq."); //$NON-NLS-1$ //$NON-NLS-2$
			hashKey = hashKey.replaceAll("\\?"".qu."); //$NON-NLS-1$ //$NON-NLS-2$
			hashKey = hashKey.replaceAll("&"".am."); //$NON-NLS-1$ //$NON-NLS-2$
			hashKey = hashKey.replaceAll("\\+"".pl."); //$NON-NLS-1$ //$NON-NLS-2$
            hashKey = hashKey.replaceAll("/"".sl."); //$NON-NLS-1$ //$NON-NLS-2$
			Configuration properties = (Configuration) session.getServletContext().getAttribute("opensocial"); //$NON-NLS-1$
			widgetInstance = addNewWidgetInstance(persistenceManager, apiKey, userId, sharedDataKey, widget, nonce, hashKey, properties, lang);
			return widgetInstance;
catch (Exception ex) {
			return null;
		}
	}

Create a new widget instance object, populate its default values, and save it.

Parameters:
persistenceManager
api_key
userId
sharedDataKey
widget
nonce
idKey
properties
Returns:
	private IWidgetInstance addNewWidgetInstance(IPersistenceManager persistenceManagerString api_keyString userIdString sharedDataKeyIWidget widgetString nonceString idKeyConfiguration propertiesString lang) {		
		IWidgetInstance widgetInstance = persistenceManager.newInstance(IWidgetInstance.class);
		widgetInstance.setUserId(userId);
		widgetInstance.setSharedDataKey(sharedDataKey);
		widgetInstance.setIdKey(idKey);
		widgetInstance.setNonce(nonce);
		widgetInstance.setApiKey(api_key);
		if (LocalizationUtils.isValidLanguageTag(lang)) widgetInstance.setLang(lang);
		// set the defaults widget for this type			
		widgetInstance.setWidget(widget);						
		widgetInstance.setHidden(false);
		widgetInstance.setShown(true);
		widgetInstance.setUpdated(false);
		widgetInstance.setLocked(false);
		// Setup opensocial token if needed
		widgetInstance.setOpensocialToken(""); //$NON-NLS-1$
		if (properties.getBoolean("opensocial.enable")){ //$NON-NLS-1$
			try {
				if (properties.getString("opensocial.token").equals("secure")){ //$NON-NLS-1$ //$NON-NLS-2$
					widgetInstance.setOpensocialToken(OpenSocialUtils.createEncryptedToken(widgetInstance,properties.getString("opensocial.key"), localizedMessages)); //$NON-NLS-1$
				else {
					widgetInstance.setOpensocialToken(OpenSocialUtils.createPlainToken(widgetInstance, localizedMessages));					
				}
catch (Exception e) {
				_logger.error(e.getMessage());
			}
		}
		// add in the sharedDataKey as a preference so that a widget can know
		// what sharedData event to listen to later
		setPreference(persistenceManager, widgetInstance, "sharedDataKey", sharedDataKey, true);//$NON-NLS-1$
		// add in widget defaults
		for (IPreferenceDefault pref: widget.getPreferenceDefaults()){
			setPreference(persistenceManager, widgetInstance, pref.getPreference(), pref.getValue(),pref.isReadOnly());
		}	
		// Save
		persistenceManager.save(widgetInstance);
		return widgetInstance;
	}

Initialize a preference for the instance

Parameters:
persistenceManager
instance
key
value
	private void setPreference(IPersistenceManager persistenceManagerIWidgetInstance widgetInstanceString keyString valueboolean readOnly){
		IPreference pref = persistenceManager.newInstance(IPreference.class);
		pref.setDkey(key);				
		pref.setDvalue(value);
		pref.setReadOnly(readOnly);
		widgetInstance.getPreferences().add(pref);
	}

Destroy a widget instance and all references to it

Parameters:
instance
	public static void destroy(IWidgetInstance instance){
	    IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
		persistenceManager.delete(instance);
	}
New to GrepCode? Check out our FAQ X