using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace rasp.Workflow
{
/// <summary>
/// A process bucket is a generic container for data collected during a process.
/// </summary>
/// <remarks>
/// NOTE: Any add methods that are put into this class can only add objects that are themselves serializable. The process bucket as a
/// whole must be serializable due to the fact that a process must be serializable.
/// </remarks>
[Serializable]
public class ProcessBucket {
/// <summary>
/// The actual bucket, yes it's a hashtable.
/// </summary>
protected Hashtable _Bucket = new Hashtable();
/// <summary>
/// Gets the Bucket.
/// </summary>
/// <remarks>
/// This actually returns a clone of the bucket.
/// </remarks>
public Hashtable Bucket { get { return (Hashtable)_Bucket.Clone(); } }
/// <summary>
/// Constructs a bucket with the given parameters.
/// </summary>
/// <param name="Key">A process key that contains all of the nessicary information to store and run itself.</param>
public ProcessBucket(ProcessKey Key) {
Initialize(Key.ProcessName, Key.HubName, Key.HubID);
}
/// <summary>
/// Constructs a bucket with the given parameters.
/// </summary>
/// <param name="ProcessName">The name of the process.</param>
/// <param name="HubName">The hubname is a table name that represents the table that will be referenced throughout the process.</param>
/// <param name="HubID">The ID of the row in the HubName table.</param>
public ProcessBucket(string ProcessName, string HubName, int HubID) {
Initialize(ProcessName + "-" + HubName + "-" + HubID.ToString());
}
/// <summary>
/// Adds a string value to the bucket.
/// </summary>
/// <param name="key">The key for the string value.</param>
/// <param name="val">The associated value.</param>
public void Add(string key, string val) {
_Bucket.Add(key, val);
}
/// <summary>
/// Adds an integer value to the bucket.
/// </summary>
/// <param name="key">The key for the int value.</param>
/// <param name="val">The associated value.</param>
public void Add(string key, int val) {
_Bucket.Add(key, val);
}
/// <summary>
/// Adds a StringCollection to the bucket.
/// </summary>
/// <remarks>
/// A StringCollection is by default serializable, so this is a good option for information to add to the bucket.
/// </remarks>
/// <param name="key">The key for the collection.</param>
/// <param name="val">The associated value.</param>
public void Add(string key, System.Collections.Specialized.StringCollection val) {
_Bucket.Add(key, val);
}
/// <summary>
/// Adds a StringCollection to the bucket.
/// </summary>
/// <remarks>
/// A NameValueCollection is by default serializable, so this is a good option for information to add to the bucket. In addition,
/// the Request.Form object is this same object, so this will work out well.
/// </remarks>
/// <param name="key">The key for the collection.</param>
/// <param name="val">The associated value.</param>
public void Add(string key, System.Collections.Specialized.NameValueCollection val) {
_Bucket.Add(key, val);
}
/// <summary>
/// Serializes the bucket from the hard drive, with the name derived from:
/// <b>constants.bucketroot</b>\<b>processname-hubname-hubid</b>.bucket
/// </summary>
/// <param name="Key">A processkey with the processname, hubname and hubid defined</param>
public void SerializeBucket(ProcessKey Key) {
SerializeBucket(Key.ProcessName, Key.HubName, Key.HubID);
}
/// <summary>
/// Serializes the bucket from the hard drive, with the name derived from:
/// <b>constants.bucketroot</b>\<b>processname-hubname-hubid</b>.bucket
/// </summary>
/// <param name="ProcessName">The name of the process.</param>
/// <param name="HubName">The hubname is a table name that represents the table that will be referenced throughout the process.</param>
/// <param name="HubID">The ID of the row in the HubName table.</param>
public void SerializeBucket(string ProcessName, string HubName, int HubID) {
SerializeBucket(ProcessName + "-" + HubName + "-" + HubID.ToString());
}
/// <summary>
/// Serializes the bucket from the hard drive, with the name derived from:
/// <b>constants.bucketroot</b>\<b>processname-hubname-hubid</b>.bucket
/// </summary>
/// <param name="BucketName">The name of the bucket file on disk.</param>
public void SerializeBucket(string BucketName) {
FileStream fs = new FileStream(BucketName + ".bucket", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs,Bucket);
}
/// <summary>
/// Deserializes the bucket from the hard drive, with the name derived from:
/// <b>constants.bucketroot</b>\<b>processname-hubname-hubid</b>.bucket
/// </summary>
/// <param name="Key">A processkey with the processname, hubname and hubid defined</param>
/// <returns></returns>
private void Initialize(ProcessKey Key) {
Initialize(Key.ProcessName, Key.HubName, Key.HubID);
}
/// <summary>
/// Deserializes the bucket from the hard drive, with the name derived from:
/// <b>constants.bucketroot</b>\<b>processname</b>-<b>hubname</b>-<b>hubid</b>.bucket
/// </summary>
/// <param name="ProcessName">The processname which distingushes the bucket</param>
/// <param name="HubName">The hubname </param>
/// <param name="HubID"></param>
/// <returns></returns>
private void Initialize(string ProcessName, string HubName, int HubID) {
Initialize(ProcessName + "-" + HubName + "-" + HubID.ToString());
}
/// <summary>
/// Deserializes the bucket from the hard drive, with the name derived from:
/// <b>constants.bucketroot</b>\<b>processname-hubname-hubid</b>.bucket
/// </summary>
/// <param name="ProcessKey">A string representation of
/// <b>processname</b>-<b>hubname</b>-<b>hubid</b></param>
/// <returns></returns>
private void Initialize(string ProcessKey) {
if (File.Exists(ProcessKey + ".bucket")) {
FileStream fs = new FileStream(ProcessKey + ".bucket", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
_Bucket = (Hashtable)bf.Deserialize(fs);
} else {
_Bucket = new Hashtable();
}
}
}
}