using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlServerCe; using Foo.DataAccessLayer.DataTypes; namespace Foo.DataAccessLayer { public class dState { private const string EMPTY_WORKSPACE_NAME_STATE = "____CURRENT__WORKSPACE____IS___SET_TO__NIL__"; /// /// Get the Current WorkspaceName in the State Table /// /// the name of the current's state workspace public string GetCurrentWorkspaceName() { object obj = null; lock (this) { string sql = "SELECT [CurrentWorkspaceName] FROM State"; obj = DB.RunSQLCommandTextExecuteScalar(sql); } if (obj != null) { if (obj.ToString() == EMPTY_WORKSPACE_NAME_STATE) return string.Empty; else return obj.ToString(); } else return string.Empty; } /// /// Set the Current WorkspaceName in the State Table /// /// the name of the new workspace name (Can NOT be String.Empty!, use ClearCurrentWorkspaceName() instead) /// true if successful, false otherwise public bool SetCurrentWorkspaceName(string currentWorkspaceName) { if (!String.IsNullOrEmpty(currentWorkspaceName) && DataTypeValidation.IsValidWorkspaceName(currentWorkspaceName)) { lock (this) { // Existence Check if ((currentWorkspaceName != EMPTY_WORKSPACE_NAME_STATE) && !Data.Workspace.DoesWorkspaceNameExist(currentWorkspaceName)) return false; string currentWorkspaceNameInDB = GetCurrentWorkspaceName(); // check if different bool bIsDifferent = (currentWorkspaceNameInDB.ToLower() != currentWorkspaceName.ToLower()); if (!bIsDifferent) return false; bool bInsert = !IsInEmptyWorkspaceNameState() && String.IsNullOrEmpty(currentWorkspaceNameInDB); if (bInsert) { string sql = "INSERT INTO State ([CurrentWorkspaceName]) VALUES (@workspacename)"; SqlCeParameter[] sqlparams = new SqlCeParameter[] { new SqlCeParameter("@workspacename", currentWorkspaceName) }; int nResult = DB.RunSQLCommandTextExecuteNonQuery(sql, sqlparams); if (nResult != 1) return false; else return true; } else { string sql = "UPDATE State SET [CurrentWorkspaceName]=@newworkspacename WHERE [CurrentWorkspaceName]=@oldworkspacename"; SqlCeParameter[] sqlparams; if (!IsInEmptyWorkspaceNameState()) { sqlparams = new SqlCeParameter[] { new SqlCeParameter("@oldworkspacename", currentWorkspaceNameInDB), new SqlCeParameter("@newworkspacename", currentWorkspaceName), }; } else { sqlparams = new SqlCeParameter[] { new SqlCeParameter("@oldworkspacename", EMPTY_WORKSPACE_NAME_STATE), new SqlCeParameter("@newworkspacename", currentWorkspaceName), }; } int nResult = DB.RunSQLCommandTextExecuteNonQuery(sql, sqlparams); if (nResult != 1) return false; else return true; } } } return false; } /// /// Mostly used internally for the Empty WorkspaceName State /// /// true if we are in this state, false otherwise internal bool IsInEmptyWorkspaceNameState() { object obj = null; lock (this) { string sql = "SELECT [CurrentWorkspaceName] FROM State"; obj = DB.RunSQLCommandTextExecuteScalar(sql); } if (obj != null) { if (obj.ToString() == EMPTY_WORKSPACE_NAME_STATE) return true; else return false; } else return false; } /// /// Clears the current workspace in the State Table /// public void ClearCurrentWorkspaceName() { SetCurrentWorkspaceName(EMPTY_WORKSPACE_NAME_STATE); } } }