Smart Space 11.1.1: Storing Properties and Maintaining a Gadget's State |
Note: This article assumes basic knowledge of how to create a Smart Space gadget. For an introduction to gadget creation please see
Creating Your First Gadget.
Smart Space Development Kit (SDK) provides the ability to store and retrieve user-defined properties locally and on the Smart Space server through the PreferenceManager property of the gadget class.
Local Properties
Local properties are stored on the client and are accessible through the PreferenceManager.LocalProperties collection, which implements the ICommonPreferences interface and provides generic Save and Load methods to store and retrieve user-defined data structures.
' Declare a variable to hold some data.
Private files As ArrayList
' Retrieve the contents of the ArrayList by using the "files" key to access it.
files = Me.Services.PreferenceManager.LocalProperties.Load(Of ArrayList)("files")
' Save the contents of the ArrayList using the "files" key.
Me.Services.PreferenceManager.LocalProperties.Save(Of ArrayList)("files", files)
|
// Declare a variable to hold some data.
private ArrayList files;
// Retrieve the contents of the ArrayList by using the "files" key to access it.
files = this.Services.PreferenceManager.LocalProperties.Load("files");
// Save the contents of the ArrayList using the "files" key.
this.Services.PreferenceManager.LocalProperties.Save("files", files);
|
Server Properties
Smart Space allows properties to be stored on the server. Properties are grouped by service types. A service type may have default properties, which are common to all services of that type.
If a service type or service name is known then the server properties can be obtained by using the GetServiceTypeProperties and GetServiceProperties methods that are defined in IPreferenceManager (which in turn can be retrieved through the Services.PreferenceManager property of the gadget class).
To obtain all provider types or all providers, the methods GetAllServiceTypeProperties and GetAllServiceProperties of IPreferenceManager should be used. Both IServiceTypeProperties and IServiceProperties expose a collection of objects that implement the IServiceProperty interface. Each such object can be used to retrieve the key and the value of a server property.
The following example demonstrates how to obtain a value of the "GSM Host" property of the "SmartSpace" provider, which is of type "Reporting and Analysis."
Dim gsmHost As String = ""
' Get the provider type.
Dim essbaseServiceProps As IServiceTypeProperties = _
Me.Services.PreferenceManager.GetServiceTypeProperties("Reporting and Analysis")
' Loop through all providers in this provider type,
' locating the "SmartSpace" provider.
For Each propertiesItem As IServiceProperties In essbaseServiceProps.Items
If propertiesItem.Name.Equals("SmartSpace") Then
' Loop through all properties for this provider,
' locating the "GSM Host" property.
For Each serviceProperty As IServiceProperty In propertiesItem.Properties
If serviceProperty.Key.Equals("GSM Host") Then
' Retrieve the value of the "GSM Host" property.
gsmHost = serviceProperty.Value
End If
Next
End If
Next |
string gsmHost = "";
// Get the provider type.
IServiceTypeProperties essbaseServiceProps =
this.Services.PreferenceManager.GetServiceTypeProperties("Reporting and Analysis");
if (essbaseServiceProps != null)
{
// Loop through all providers in this provider type,
// locating the "SmartSpace" provider.
foreach (IServiceProperties propertiesItem in essbaseServiceProps.Items)
{
if (propertiesItem.Name.Equals("SmartSpace"))
{
// Loop through all properties for this provider,
// locating the "GSM Host" property.
foreach (IServiceProperty serviceProperty in propertiesItem.Properties)
{
if (serviceProperty.Key.Equals("GSM Host"))
{
// Retrieve the value of the "GSM Host" property.
gsmHost = serviceProperty.Value;
}
}
}
}
} |
Saving a Gadget's State
The IPreferenceManager interface exposes SavePreferences and LoadPreferences methods that allow arbitrary data to be saved between gadget invocations. Both methods are generic and enable you to store and retrieve a custom, user-defined data structure. One useful application of this mechanism is to save a gadget's state before the
gadget is closed, restoring the state when the gadget is opened. The following example defines a custom data structure and shows how to store and retrieve it.
' Data structure to store the state of the gadget.
Public Structure GadgetPreferences
Public Server As String
Public XML As ArrayList
End Structure
' Declare the variable to store the state of the gadget.
Private preferences As GadgetPreferences
' Save current gadget state, to be able to restore on next invocation.
Me.Services.PreferenceManager.SavePreferences(Of GadgetPreferences)(preferences)
' Load previously stored gadget state.
preferences = Me.Services.PreferenceManager.LoadPreferences(Of GadgetPreferences)()
|
// Data structure to store the state of the gadget.
public struct GadgetPreferences
{
public String Server;
public ArrayList XML;
}
// Declare the variable to store the state of the gadget.
private GadgetPreferences preferences;
// Save current gadget state, to be able to restore on next invocation.
this.Services.PreferenceManager.SavePreferences(preferences);
// Load previously stored gadget state.
preferences = this.Services.PreferenceManager.LoadPreferences();
|
|