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;
 
 
Service facade for managing SharedDataKeys in a consistent fashion. SharedDataKeys have an external form used by the host application (e.g. "tab23"). However we also have an internal form which avoids potential collisions, and which is unique to the combination of the host application (API key), external shared data key, and the widget URI
 
 public class SharedDataHelper {
  
  
Get the external form of the shared data key; i.e. the key provided by the client and unique only within the scope of the client API key

Parameters:
instance the widget instance
Returns:
the external shared data key for the widget instance FIXME Not implemented yet. We need somewhere to safely store external shared data keys
 
     public static String getExternalSharedDataKey(IWidgetInstance instance){
       return null;
     }

  
Get the internal form of the shared data key

Parameters:
instance the widget instance
Returns:
the internal shared data key for the widget instance
 
 	public static String getInternalSharedDataKey(IWidgetInstance instance){
 	  return instance.getSharedDataKey();
 	}

Create an internal shared data key given a widget instance and external shared data key

Parameters:
instance the widget instance
externalSharedDataKey the shared data key provided by the client plugin
Returns:
the internal shared data key for the instance
 
 	public static String getInternalSharedDataKey(IWidgetInstance instanceString externalSharedDataKey){
 		String key = externalSharedDataKey + ":" + instance.getApiKey() + ":" + instance.getWidget().getGuid();
 		return String.valueOf(key.hashCode());
 	}

Create an internal shared data key given an API key, widget URI and external shared data key

Parameters:
apiKey the API key used to request widget instances
widgetUri the Widget's external URI-based identifier
externalSharedDataKey the shared data key provided by the client plugin
Returns:
the internal shared data key
 
 	 public static String getInternalSharedDataKey(String apiKeyString widgetUriString externalSharedDataKey){
 	    String key = externalSharedDataKey + ":" + apiKey + ":" + widgetUri;
 	    return String.valueOf(key.hashCode());
 	  }

 
Find shared data for a Widget Instance

Parameters:
instance the widget instance
Returns:
an array of SharedData objects for the widget instance
 
 	public static ISharedData[] findSharedData(IWidgetInstance instance){
 	  
 	  //
     // Use the internal shared data key of the instance for one index of the query
     //
     String sharedDataKey = SharedDataHelper.getInternalSharedDataKey(instance);
     
     //
     // Obtain a persistence manager and return the results of executing a query of SharedData objects matching the sharedDataKey
     //
     IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
     return (ISharedData[]) persistenceManager.findByValue(ISharedData.class"sharedDataKey", sharedDataKey);
 	}

Find a specific shared data object for a given Widget Instance and object key

Parameters:
instance the widget instance
key the key of the shared data object, i.e. the tuple key not the shared data key
Returns:
a SharedData object, or null if no matches are found
	public static ISharedData findSharedData(IWidgetInstance instanceString key){
	  
	  //
	  // Use the internal shared data key of the instance for one index of the query
	  //
    String sharedDataKey = SharedDataHelper.getInternalSharedDataKey(instance);
    
    //
    // Obtain a persistence manager and construct a query of SharedData objects matching the sharedDataKey and dkey
    //
    IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("sharedDataKey", sharedDataKey);
    params.put("dkey", key);
    
    //
    // Execute the query and obtain array of results
    // We assert that there are never duplicates.
    //
    ISharedData[] results = (ISharedData[]) persistenceManager.findByValues(ISharedData.class, params);
    assert(results.length <= 1);
    
    //
    // If the result contains a single item, return it, otherwise return null.
    //
    if (results.length != 0) return results[0];
    return null;
	}	
New to GrepCode? Check out our FAQ X