// ========================================================================
// Copyright 2007 - Volian Enterprises, Inc. All rights reserved.          
// Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
// ------------------------------------------------------------------------
// $Workfile: $     $Revision: $                                           
// $Author: $   $Date: $                                                   
//                                                                         
// $History: $                                                             
// ========================================================================
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using System.Configuration;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;
namespace VEPROMS.CSLA.Library
{
	/// 
	///	Database Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	public static partial class Database
	{
		#region Log4Net
		private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		#endregion
		public static void LogException(string s, Exception ex)
		{
			int i = 0;
			Console.WriteLine("Error - {0}", s);
			for (; ex != null; ex = ex.InnerException)
			{
				Console.WriteLine("{0}{1} - {2}", "".PadLeft(++i * 2), ex.GetType().ToString(), ex.Message);
			}
		}
		private static bool _LoggingInfo = false; // By default don't log info 
		public static bool LoggingInfo
		{
			get { return _LoggingInfo; }
			set { _LoggingInfo = value; }
		}
		static System.Diagnostics.Process _CurrentProcess = System.Diagnostics.Process.GetCurrentProcess();
		public static void LogInfo(string s, int hashCode)
		{
			if (_LoggingInfo)
				Console.WriteLine("{0} MB {1}", _CurrentProcess.WorkingSet64 / 1000000, string.Format(s, hashCode));
		}
		public static void LogDebug(string s, int hashCode)
		{
			if (_LoggingInfo)
				Console.WriteLine("{0} MB {1}", _CurrentProcess.WorkingSet64 / 1000000, string.Format(s, hashCode));
		}
		private static string _ConnectionName = "VEPROMS";
		public static string ConnectionName
		{
			get { return Database._ConnectionName; }
			set { Database._ConnectionName = value; }
		}
		public static string VEPROMS_Connection
		{
			get
			{
				DateTime.Today.ToLongDateString();
				ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings[ConnectionName];
				if (cs == null)
				{
					throw new ApplicationException("Database.cs Could not find connection " + ConnectionName);
				}
				return cs.ConnectionString;
			}
		}
		public static SqlConnection VEPROMS_SqlConnection
		{
			get
			{
				string strConn = VEPROMS_Connection; // If failure - Fail (Don't try to catch)
				// Attempt to make a connection 
				try
				{
					SqlConnection cn = new SqlConnection(strConn);
					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  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
					{
						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);
				}
			}
		}
		public static void PurgeData()
		{
			try
			{
				SqlConnection cn = VEPROMS_SqlConnection;
				SqlCommand cmd = new SqlCommand("purgedata", cn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.CommandTimeout = 0;
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				if (_MyLog.IsErrorEnabled) _MyLog.Error("Purge Error", ex);
				throw new ApplicationException("Failure on Purge", ex);
			}
		}
	}
	public class DbCslaException : Exception
	{
		internal DbCslaException(string message, Exception innerException) : base(message, innerException) { ;}
		internal DbCslaException(string message) : base(message) { ;}
		internal DbCslaException() : base() { ;}
	} // Class
} // Namespace