using System;
using System.Collections;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using rasp.SQL.DBInfo;
//Process: This is the conductor class. Looking over it you will see that I have a hashtable to hold all of the active processes
//I will eventually add the code to serialize the processes, but I wanted to get the process framework working first.
//When you ask for a process, if is not contained in the collection, then it instantiates it, otherwise it grabs it from the collection.
//This is a pretty simple class.
namespace rasp.Workflow{
/// <summary>
/// Manages all the process running on the server.
/// </summary>
/// <remarks>
/// The workflow conductor keeps a hashtable of all of the currently running processes, tells processes to serialize themselves
/// when nessicary and gives process handles back to the BasePage that requests it.
/// </remarks>
public class Conductor {
//TODO: I don't think that a collection is appropriate here, since it is not part of the application (or global) class, it is re-instantiated every time that the BasePage is constructed.
private Hashtable _Processes = new Hashtable();
public Conductor() {
}
public void AddProcess(Process p, string ProcessName, string HubName, int HubID, UserInfo userinfo) {
ProcessKey key = new ProcessKey(ProcessName, HubName, HubID, userinfo);
_Processes.Remove(key);
_Processes.Add(key, p);
}
/// <summary>
/// Gets a process given the info needed to create the process key.
/// </summary>
/// <remarks>
/// This class determines if a process needs to be serialized if it is older than a certain time period, and will remove it from the
/// collection of processes. If the process is requested and is not contained in the collection, then it will de-serialize the process
/// and return it accordingly.
/// </remarks>
/// <param name="ProcessName">The name of the desired 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>
/// <returns>The appropriate Process object, either read in from the hard drive, or pulled from memory.</returns>
public Process GetProcess(string ProcessName, string HubName, int HubID, UserInfo userinfo, BasePage container) {
ProcessKey key = new ProcessKey(ProcessName, HubName, HubID, userinfo);
return GetProcess(key, container);
}
/// <summary>
/// Gets a process given the info needed to create the process key.
/// </summary>
/// <remarks>
/// This class determines if a process needs to be serialized if it is older than a certain time period, and will remove it from the
/// collection of processes. If the process is requested and is not contained in the collection, then it will de-serialize the process
/// and return it accordingly.
/// </remarks>
/// <param name="Key">A process key that refers to the process.</param>
/// <param name="container">A handle to the page that the process will be placed on.</param>
/// <returns></returns>
public Process GetProcess(ProcessKey Key, BasePage container) {
Process ret = null;
if (_Processes.Contains(Key)) {
ret = (Process)_Processes[Key];
} else {
ret = new Process(Key, container);
_Processes.Add(Key, ret);
}
ret.Container = container;
return ret;
}
/// <summary>
/// Serializes a given process.
/// </summary>
/// <param name="Key"></param>
public void SerializeProcess(ProcessKey Key) {
//TODO: Make this serialize the Process based on staleness or whatnot of the buckets it has, maybe the service needs to come in and do it?
}
}
/// <summary>
/// The ProcesKey class contains all of the info needed to uniquely identify a process.
/// </summary>
/// <remarks>
/// ProcessKeys are used to store processes into a hashtable. Because of that fact, we have overridden the Equals and
/// GetHashCode methods so that we can ensure that the keying of the hashtable utilizing these keys works correctly.
/// </remarks>
public class ProcessKey {
private string _ProcessName = "";
private string _HubName = "";
private int _HubID = 0;
private UserInfo _UserInfo;
/// <summary>
/// Constructs a ProcesKey with the given parameters.
/// </summary>
/// <param name="Process_Name">The name of the desired process.</param>
/// <param name="Hub_Name">The hubname is a table name that represents the table that will be referenced throughout the process.</param>
/// <param name="Hub_ID">The ID of the row in the HubName table.</param>
public ProcessKey(string Process_Name, string Hub_Name, int Hub_ID, UserInfo userinfo) {
_ProcessName = Process_Name;
_HubName = Hub_Name;
_HubID = Hub_ID;
_UserInfo = userinfo;
}
/// <summary>
/// Gets the processes name.
/// </summary>
public string ProcessName {
get { return _ProcessName;
}
}
/// <summary>
/// Gets the hub name.
/// </summary>
public string HubName {
get { return _HubName;
}
}
/// <summary>
/// Gets the hub ID
/// </summary>
public int HubID {
get { return _HubID;
}
}
/// <summary>
/// Gets the hub ID
/// </summary>
public int UserID {
get { return this._UserInfo.ID;
}
}
/// <summary>
/// Gets the hub ID
/// </summary>
public UserInfo UserInfo {
get { return this._UserInfo;
}
}
/// <summary>
/// Overrides the default Equals method so that keying on a hashtable will not just equate objects based off of their position
/// in memory.
/// </summary>
/// <param name="Key">A process key to test for equality.</param>
/// <returns>Returns a bool that indicates if the passed in object was equal.</returns>
public override bool Equals(Object Key) {
bool ret = false;
try {
ProcessKey what = (ProcessKey)Key;
if(what.ProcessName.Equals(ProcessName) && what.HubName.Equals(HubName) && what.HubID == HubID && this.UserID == _UserInfo.ID) {
ret = true;
}
} catch { }
return ret;
}
/// <summary>
/// Adds the hash code of the three main variables (ProcessName, HubName and HubID) to generate a somewhat unique hashcode.
/// </summary>
/// <remarks>
/// This implementation may not be the best way to preform this task. It needs to be researched further.
/// </remarks>
/// <returns>The hashcode used to store the object.</returns>
public override int GetHashCode() {
return HubID.GetHashCode() + ProcessName.GetHashCode() + HubName.GetHashCode();
}
}
}