CLSA - Ds

This commit is contained in:
2026-09-08 08:55:36 -04:00
parent c21f366bb9
commit a69a2270a6
24 changed files with 1609 additions and 3414 deletions
+169 -193
View File
@@ -15,7 +15,6 @@ using Csla;
using Csla.Data;
using System.Configuration;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text.RegularExpressions;
@@ -35,79 +34,76 @@ namespace VEPROMS.CSLA.Library
private static string _DBServer = null;
private static DateTime _RevDate=DateTime.MinValue;
public static DateTime RevDate
{
get { return Database._RevDate; }
set { Database._RevDate = value; }
}
public static DateTime RevDate
{
get { return _RevDate; }
set { _RevDate = value; }
}
private static string _RevDescription= "Unknown";
public static string RevDescription
{
get { return Database._RevDescription; }
set { Database._RevDescription = value; }
}
public static string DBServer
{
get
{
if (_DBServer == null)
public static string RevDescription
{
string cnstr = null;
try
get { return _RevDescription; }
set { _RevDescription = value; }
}
public static string DBServer
{
get
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
if (_DBServer == null)
{
cnstr = cn.ConnectionString;
string cnstr = null;
try
{
using (SqlCommand cmd = new SqlCommand("vesp_GetSQLCodeRevision", cn))
using (SqlConnection cn = VEPROMS_SqlConnection)
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
cnstr = cn.ConnectionString;
try
{
_RevDate = dr.GetDateTime(0);
_RevDescription = dr.GetString(1);
using (SqlCommand cmd = new SqlCommand("vesp_GetSQLCodeRevision", cn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
_RevDate = dr.GetDateTime(0);
_RevDescription = dr.GetString(1);
}
}
}
catch (Exception)
{
_RevDate = DateTime.MinValue;
_RevDescription = "Unknown";
}
string server = "";
string db = "";
Match m = Regex.Match(cnstr, "Data Source=([^;]+)(;[^;]+)*;*Initial Catalog=([^;]+)(;[^;]+)*");
if (m.Success && m.Groups.Count > 4)
{
server = m.Groups[1].Value;
db = m.Groups[3].Value;
}
_DBServer = string.Format("{0} - {1} [SQL:{2:yyMM.ddHH}]", server, db, RevDate);
}
}
catch (Exception ex)
catch (Exception)
{
_RevDate = DateTime.MinValue;
_RevDescription = "Unknown";
_DBServer = cnstr;
}
string server = "";
string db = "";
Match m = Regex.Match(cnstr, "Data Source=([^;]+)(;[^;]+)*;*Initial Catalog=([^;]+)(;[^;]+)*");
if (m.Success && m.Groups.Count > 4)
{
server = m.Groups[1].Value;
db = m.Groups[3].Value;
}
_DBServer = string.Format("{0} - {1} [SQL:{2:yyMM.ddHH}]", server, db, RevDate);
}
}
catch (Exception)
{
_DBServer = cnstr;
return _DBServer;
}
}
return _DBServer;
}
}
private static int _DefaultTimeout = 600; // 600 seconds, i.e. 10 minutes
public static int DefaultTimeout
{
get { return _DefaultTimeout; }
set { _DefaultTimeout = value; }
}
public static int SQLTimeout
{
get { return _DefaultTimeout/20; }
}
public static void LogException(string s, Exception ex)
public static int SQLTimeout => _DefaultTimeout / 20;
public static void LogException(string s, Exception ex)
{
int i = 0;
Console.WriteLine("Error - {0}", s);
@@ -122,7 +118,7 @@ public static string DBServer
get { return _LoggingInfo; }
set { _LoggingInfo = value; }
}
static System.Diagnostics.Process _CurrentProcess = System.Diagnostics.Process.GetCurrentProcess();
static readonly System.Diagnostics.Process _CurrentProcess = Process.GetCurrentProcess();
public static void LogInfo(string s, int hashCode)
{
if (_LoggingInfo)
@@ -136,8 +132,8 @@ public static string DBServer
private static string _ConnectionName = "VEPROMS";
public static string ConnectionName
{
get { return Database._ConnectionName; }
set { Database._ConnectionName = value; _VEPROMS_Connection = null; /* Reset Connection */ }
get { return _ConnectionName; }
set { _ConnectionName = value; _VEPROMS_Connection = null; /* Reset Connection */ }
}
private static bool _TrackDBUsage = false;
public static bool TrackDBUsage
@@ -145,7 +141,8 @@ public static string DBServer
get { return _TrackDBUsage; }
set { _TrackDBUsage = value; }
}
private static Dictionary<string, int> _Methods = new Dictionary<string, int>();
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
private static Dictionary<string, int> _Methods = new Dictionary<string, int>();
public static void ShowDBTracking(string fileName)
{
DebugDBTrack.Open(VlnSettings.TemporaryFolder + "\\" + fileName);
@@ -159,12 +156,7 @@ public static string DBServer
{
if (TrackDBUsage)
{
string str = Volian.Base.Library.vlnStackTrace.CalledFromCSLA;
//if (str.Contains("ItemAndChildren"))
//{
// ShowDictionary(Methods);
// Methods = new Dictionary<string, int>();
//}
string str = vlnStackTrace.CalledFromCSLA;
if (!_Methods.ContainsKey(str))
_Methods.Add(str, 1);
else
@@ -177,7 +169,7 @@ public static string DBServer
DateTime.Today.ToLongDateString();
// If DBConnection.XML exists, use the connection string from DBConnection.XML
string cnOverride = System.Windows.Forms.Application.StartupPath + @"\DBConnection.XML";
if (System.IO.File.Exists(cnOverride))
if (File.Exists(cnOverride))
{
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load(cnOverride);
@@ -209,12 +201,8 @@ public static string DBServer
}
}
// Otherwise get the value from the ConfigurationManager
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings[ConnectionName];
if (cs == null)
{
throw new ApplicationException("Database.cs Could not find connection " + ConnectionName);
}
string constr = FixServer(cs.ConnectionString);
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings[ConnectionName] ?? throw new ApplicationException("Database.cs Could not find connection " + ConnectionName);
string constr = FixServer(cs.ConnectionString);
if (constr.Contains("{MENU}"))
{
constr = ChooseDatabase(constr);
@@ -240,10 +228,10 @@ public static string DBServer
}
private static string FixServer(string connectionString)
{
string serverName = Volian.Base.Library.VlnSettings.GetServer();
string serverName = VlnSettings.GetServer();
if (serverName != null && serverName != "")
{
System.Text.RegularExpressions.Match mServer = System.Text.RegularExpressions.Regex.Match(connectionString, ".*Data Source=([^;]*).*");
System.Text.RegularExpressions.Match mServer = Regex.Match(connectionString, ".*Data Source=([^;]*).*");
if (mServer.Success)
{
connectionString = connectionString.Substring(0, mServer.Groups[1].Index) + serverName + connectionString.Substring(mServer.Groups[1].Index + mServer.Groups[1].Length);
@@ -254,24 +242,22 @@ public static string DBServer
private static string _SelectedDatabase;
public static string SelectedDatabase
{
get { return Database._SelectedDatabase; }
set { Database._SelectedDatabase = value; }
get { return _SelectedDatabase; }
set { _SelectedDatabase = value; }
}
public static string ActiveDatabase
{
get
{
string activeDatabase = SelectedDatabase;
if(activeDatabase == null)
activeDatabase = Regex.Replace(VEPROMS_Connection, "^.*Initial Catalog=([^;]*);.*$", "$1", RegexOptions.IgnoreCase);
return activeDatabase;
string activeDatabase = SelectedDatabase ?? Regex.Replace(VEPROMS_Connection, "^.*Initial Catalog=([^;]*);.*$", "$1", RegexOptions.IgnoreCase);
return activeDatabase;
}
}
private static string _LastDatabase="NoDefault";
public static string LastDatabase
{
get { return Database._LastDatabase; }
set { Database._LastDatabase = value; }
get { return _LastDatabase; }
set { _LastDatabase = value; }
}
private static string ChooseDatabase(string constr)
{
@@ -300,95 +286,98 @@ public static string DBServer
return constr.Replace("{MENU}", _SelectedDatabase);
}
private static System.Windows.Forms.ContextMenuStrip BuildDatabaseMenu(string constr)
{
string tmp = constr.Replace("{MENU}", "master");
SqlConnection cn = new SqlConnection(tmp);
cn.Open();
// SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases where name like 'VEP%' order by name", cn);
//SqlDataAdapter da = new SqlDataAdapter("select name, case when object_id('[' + name + ']..Items') is null then 'Not PROMS' when object_id('[' + name + ']..Revisions') is not null then 'Approval' when object_id('[' + name + ']..ContentAudits') is not null then 'Change Manager' else 'Original' end functionality from sysdatabases where name not in ('master','model','msdb','tempdb') order by name", cn);
SqlDataAdapter da = new SqlDataAdapter("select name, 'Approval' functionality from sysdatabases where name not in ('master','model','msdb','tempdb') order by name", cn);
da.SelectCommand.CommandTimeout = 300; // 300 sec timeout
DataSet ds = new DataSet();
try
{
da.Fill(ds);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.GetType().Name, ex.Message);
throw(new Exception("Cannot Load Data List",ex));
}
cn.Close();
System.Windows.Forms.ContextMenuStrip cms = new System.Windows.Forms.ContextMenuStrip();
cms.Items.Add("Choose Database");
System.Windows.Forms.ToolStripMenuItem tsmi = cms.Items[0] as System.Windows.Forms.ToolStripMenuItem;
tsmi.BackColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.ActiveCaption);// System.Drawing.Color.Pink;
tsmi.ForeColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.ActiveCaptionText);
tsmi.Font = new System.Drawing.Font(tsmi.Font, System.Drawing.FontStyle.Bold);
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["functionality"].ToString() == "Approval" && !dr["name"].ToString().EndsWith("_RO")) // don't display sql ro databases in list
cms.Items.Add(dr["name"].ToString(), null, new EventHandler(Database_Click));
}
return cms;
}
{
string tmp = constr.Replace("{MENU}", "master");
using (SqlConnection cn = new SqlConnection(tmp))
{
cn.Open();
using (SqlDataAdapter da = new SqlDataAdapter("select name, 'Approval' functionality from sysdatabases where name not in ('master','model','msdb','tempdb') order by name", cn))
{
da.SelectCommand.CommandTimeout = 300; // 300 sec timeout
DataSet ds = new DataSet();
try
{
da.Fill(ds);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.GetType().Name, ex.Message);
throw (new Exception("Cannot Load Data List", ex));
}
cn.Close();
System.Windows.Forms.ContextMenuStrip cms = new System.Windows.Forms.ContextMenuStrip();
cms.Items.Add("Choose Database");
System.Windows.Forms.ToolStripMenuItem tsmi = cms.Items[0] as System.Windows.Forms.ToolStripMenuItem;
tsmi.BackColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.ActiveCaption);// System.Drawing.Color.Pink;
tsmi.ForeColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.ActiveCaptionText);
tsmi.Font = new System.Drawing.Font(tsmi.Font, System.Drawing.FontStyle.Bold);
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["functionality"].ToString() == "Approval" && !dr["name"].ToString().EndsWith("_RO")) // don't display sql ro databases in list
cms.Items.Add(dr["name"].ToString(), null, new EventHandler(Database_Click));
}
return cms;
}
}
}
static void Database_Click(object sender, EventArgs e)
static void Database_Click(object sender, EventArgs e)
{
System.Windows.Forms.ToolStripMenuItem tsmi = sender as System.Windows.Forms.ToolStripMenuItem;
if (tsmi != null)
{
_SelectedDatabase = tsmi.Text;
}
}
if (sender is System.Windows.Forms.ToolStripMenuItem tsmi)
{
_SelectedDatabase = tsmi.Text;
}
}
public static SqlConnection VEPROMS_SqlConnection
{
get
{
string strConn = VEPROMS_Connection; // If failure - Fail (Don't try to catch)
// Attempt to make a connection
{
string strConn = VEPROMS_Connection; // If failure - Fail (Don't try to catch)
// Attempt to make a connection
//note - cannot use using here as it will close the connection and it needs to return it as open
SqlConnection cn = new SqlConnection(strConn);
try
{
cn.Open();
return cn;
}
catch (SqlException exsql)
{
const string strAttachError = "An attempt to attach an auto-named database for file ";
if (exsql.Message.StartsWith(strAttachError))
{// Check to see if the file is missing
string sFile = exsql.Message.Substring(strAttachError.Length);
sFile = sFile.Substring(0, sFile.IndexOf(" failed"));
// "An attempt to attach an auto-named database for file <mdf file> failed"
if (strConn.ToLower().IndexOf("user instance=true") < 0)
{
throw new ApplicationException("Connection String missing attribute: User Instance=True");
}
if (System.IO.File.Exists(sFile))
{
throw new ApplicationException("Database file " + sFile + " Cannot be opened\r\n", exsql);
}
else
{
throw new FileNotFoundException("Database file " + sFile + " Not Found", exsql);
}
}
else
{
//Open a MesageBox so the user is given some feedback that the connection has failed.
ReportInnermostException(exsql,strConn);
throw new ApplicationException("Failure on Connect", exsql);
}
}
catch (Exception ex)// Throw Application Exception on Failure
{
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection Error", ex);
throw new ApplicationException("Failure on Connect", ex);
}
}
}
try
{
cn.Open();
return cn;
}
catch (SqlException exsql)
{
const string strAttachError = "An attempt to attach an auto-named database for file ";
if (exsql.Message.StartsWith(strAttachError))
{// Check to see if the file is missing
string sFile = exsql.Message.Substring(strAttachError.Length);
sFile = sFile.Substring(0, sFile.IndexOf(" failed"));
// "An attempt to attach an auto-named database for file <mdf file> failed"
if (strConn.ToLower().IndexOf("user instance=true") < 0)
{
throw new ApplicationException("Connection String missing attribute: User Instance=True");
}
if (File.Exists(sFile))
{
throw new ApplicationException("Database file " + sFile + " Cannot be opened\r\n", exsql);
}
else
{
throw new FileNotFoundException("Database file " + sFile + " Not Found", exsql);
}
}
else
{
//Open a MesageBox so the user is given some feedback that the connection has failed.
ReportInnermostException(exsql, strConn);
throw new ApplicationException("Failure on Connect", exsql);
}
}
catch (Exception ex)// Throw Application Exception on Failure
{
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection Error", ex);
throw new ApplicationException("Failure on Connect", ex);
}
}
}
/// <summary>
/// Open a MessageBox with the exception type, the connection string and the exception message
/// </summary>
@@ -400,15 +389,13 @@ public static string DBServer
while (ex.InnerException != null)
ex = ex.InnerException;
System.Windows.Forms.MessageBox.Show(string.Format("{0}\r\n\r\nConnection String ={1}", ex.Message,conn)
,"PROMS - " + ex.GetType().FullName, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
System.Diagnostics.Process.GetCurrentProcess().Kill();
, $"PROMS - {ex.GetType().FullName}", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
Process.GetCurrentProcess().Kill();
}
public static void PurgeData()
{
try
{
//SqlConnection cn = VEPROMS_SqlConnection;
//SqlCommand cmd = new SqlCommand("purgedata", cn);
using (SqlConnection cn = VEPROMS_SqlConnection)
{
using (SqlCommand cmd = new SqlCommand("purgedata", cn))
@@ -458,8 +445,6 @@ public static string DBServer
{
try
{
//SqlConnection cn = VEPROMS_SqlConnection;
//SqlCommand cmd = new SqlCommand("purgedata", cn);
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cmd = new SqlCommand("purgedata", cn))
@@ -483,11 +468,8 @@ public static string DBServer
{
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private string _ErrorMessage = string.Empty;
public string ErrorMessage
{
get { return _ErrorMessage; }
}
private ProposedTransition _ProposedTransition;
public string ErrorMessage => _ErrorMessage;
private ProposedTransition _ProposedTransition;
public ProposedTransition ProposedTransition
{
get { return _ProposedTransition; }
@@ -508,10 +490,12 @@ public static string DBServer
#region Factory Methods
public static ProposedTransition Execute(int fromID, int toID)
{
CanTransitionBeCreatedCommand cmd = new CanTransitionBeCreatedCommand();
cmd.FromID = fromID;
cmd.ToID = toID;
cmd = DataPortal.Execute<CanTransitionBeCreatedCommand>(cmd);
CanTransitionBeCreatedCommand cmd = new CanTransitionBeCreatedCommand
{
FromID = fromID,
ToID = toID
};
cmd = DataPortal.Execute<CanTransitionBeCreatedCommand>(cmd);
return cmd.ProposedTransition;
}
private CanTransitionBeCreatedCommand()
@@ -536,8 +520,6 @@ public static string DBServer
{
try
{
//SqlConnection cn = VEPROMS_SqlConnection;
//SqlCommand cmd = new SqlCommand("purgedata", cn);
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cmd = new SqlCommand("vesp_CanTransitionBeCreated", cn))
@@ -571,16 +553,10 @@ public static string DBServer
{
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private string _ErrorMessage = string.Empty;
public string ErrorMessage
{
get { return _ErrorMessage; }
}
private List<InvalidTransition> _InvalidTransitions;
public List<InvalidTransition> InvalidTransitions
{
get { return _InvalidTransitions; }
}
private int _ItemID;
public string ErrorMessage => _ErrorMessage;
private List<InvalidTransition> _InvalidTransitions;
public List<InvalidTransition> InvalidTransitions => _InvalidTransitions;
private int _ItemID;
public int ItemID
{
get { return _ItemID; }
@@ -595,10 +571,12 @@ public static string DBServer
#region Factory Methods
public static List<InvalidTransition> Execute(int itemID, string newAppl)
{
WillTransitionsBeValidCommand cmd = new WillTransitionsBeValidCommand();
cmd.ItemID = itemID;
cmd.NewAppl = newAppl;
cmd = DataPortal.Execute<WillTransitionsBeValidCommand>(cmd);
WillTransitionsBeValidCommand cmd = new WillTransitionsBeValidCommand
{
ItemID = itemID,
NewAppl = newAppl
};
cmd = DataPortal.Execute<WillTransitionsBeValidCommand>(cmd);
return cmd.InvalidTransitions;
}
private WillTransitionsBeValidCommand()
@@ -626,8 +604,6 @@ public static string DBServer
{
try
{
//SqlConnection cn = VEPROMS_SqlConnection;
//SqlCommand cmd = new SqlCommand("purgedata", cn);
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cmd = new SqlCommand("vesp_WillTransitionsBeValid", cn))