/// <summary> /// Gets the description of a specified error. /// </summary> /// <typeparam name="TErrorCodeEnum">The type of the enum that lists all the valid error /// codes.</typeparam> /// <param name="codeToLookup">Error code whose description is to be returned.</param> /// <param name="databaseManager">DatabaseManager that handles the connection to the /// database.</param> /// <returns>The description for the specified error code. If the specified error code is /// not found, returns an empty string.</returns> public static string GetErrorDescription <TErrorCodeEnum>(TErrorCodeEnum codeToLookup, DatabaseManager2 databaseManager) where TErrorCodeEnum : IComparable, IFormattable, IConvertible { lock (_lockGetErrorDescription_2) { try { SqlParameter prmError = new SqlParameter("@ErrorCode", codeToLookup); SqlParameter oprmDescription = new SqlParameter("@oDescription", SqlDbType.VarChar, 100); oprmDescription.Direction = ParameterDirection.Output; SqlParameter[] aParams = new SqlParameter[] { prmError, oprmDescription }; int dummyReturnValue; string dummyErrorMessage; databaseManager.ExecStoredProc("p_GetErrorDescription", aParams, out dummyReturnValue, out dummyErrorMessage); if (oprmDescription.Value != DBNull.Value) { return((string)oprmDescription.Value); } return(string.Empty); } catch { return(string.Empty); } } }
/// <summary> /// Retrieves information about a database that the application is currently connected to. /// </summary> /// <param name="databaseManager">Database manager object that handles the connection to /// the database.</param> /// <param name="commonDatabaseErrorCode">Structure holding the common database-related /// error codes.</param> /// <param name="oErrorInfo">Output parameter: Details of any error that may have /// occurred.</param> /// <returns>A structure containing the database metadata.</returns> public static DatabaseInfo GetDatabaseInfo <T>(DatabaseManager2 databaseManager, CommonDatabaseErrorCode <T> commonDatabaseErrorCode, out ErrorInfo <T> oErrorInfo) where T : IComparable, IFormattable, IConvertible { DatabaseInfo dbInfo = new DatabaseInfo(); ErrorInfo <T> errorInfo = new ErrorInfo <T>(); lock (_lockGetDatabaseInfo) { try { errorInfo.ErrorCode = commonDatabaseErrorCode.GeneralError; errorInfo.Message = string.Empty; // Cannot convert generic type to int directly so cheat by using object. object value = commonDatabaseErrorCode.Success; errorInfo.DatabaseErrorValue = (int)value; DataTable dbMetaData = databaseManager.GetDataTable("p_GetDatabaseInfo", out errorInfo.DatabaseErrorValue, out errorInfo.Message); // Check for unrecognised error code from the database. errorInfo.ErrorCode = BBError.ValidateDBErrorValue <T>( errorInfo.DatabaseErrorValue, commonDatabaseErrorCode.GeneralDBError); if (dbMetaData != null && dbMetaData.Rows.Count > 0) { dbInfo.DatabaseTitle = (dbMetaData.Rows[0]["DatabaseTitle"]).ToString(); dbInfo.DatabaseVersion = (int)dbMetaData.Rows[0]["DatabaseVersion"]; // Check that database type is valid. string databaseTypeText = (dbMetaData.Rows[0]["DatabaseType"]).ToString(); DatabaseType databaseType = DatabaseType.NotFound; if (MiscUtilities.ValidateEnumValue <DatabaseType>(databaseTypeText, out databaseType)) { dbInfo.DatabaseType = databaseType; errorInfo.ErrorCode = commonDatabaseErrorCode.Success; } else { dbInfo.DatabaseType = DatabaseType.Invalid; errorInfo.ErrorCode = commonDatabaseErrorCode.InvalidType; } } else { dbInfo.DatabaseType = DatabaseType.NotFound; errorInfo.ErrorCode = commonDatabaseErrorCode.NoInfo; } } catch { errorInfo.ErrorCode = commonDatabaseErrorCode.GeneralError; } } oErrorInfo = errorInfo; return(dbInfo); }
/// <summary> /// Sets the value of an entry in the Dictionary. /// </summary> /// <typeparam name="T">The data type of the value to set.</typeparam> /// <param name="key">Key of entry to set.</param> /// <param name="interfaceCode">Code that uniquely identifies which Service Desk system /// is connected to this interface (eg National Help Desk, NZ Post). For dictionary /// entries that differ from one system to another (eg email alert recipients). If set to /// null the value will be saved in the default dictionary.</param> /// <param name="value">The value to set the dictionary entry to. For value types, this /// is the nullable version of the value type.</param> /// <param name="databaseManager">DatabaseManager that handles the connection with a /// database containing a dictionary table.</param> /// <typeparam name="T">The type of the dictionary entry.</typeparam> /// <returns>ErrorInfo object indicating whether the value of the dictionary entry was /// successfully set or not.</returns> private ErrorInfo <TErrorCodeEnum> SetDictionaryValue <T>(string key, string interfaceCode, T value, DatabaseManager2 databaseManager) { ErrorInfo <TErrorCodeEnum> errorInfo = new ErrorInfo <TErrorCodeEnum>(_commonDatabaseErrorCode.GeneralError, string.Empty, _commonDatabaseErrorCode.SuccessValue); try { string parameterName = null; if (typeof(T) == typeof(int) || typeof(T) == typeof(decimal)) { parameterName = "@NumericValue"; } else if (typeof(T) == typeof(DateTime)) { parameterName = "@DateTimeValue"; } else if (typeof(T) == typeof(byte[])) { parameterName = "@BinaryValue"; } else { parameterName = "@TextValue"; } SqlParameter prmKey = new SqlParameter("@Key", key); SqlParameter prmInterface = new SqlParameter("@InterfaceCode", interfaceCode); SqlParameter prmValue = new SqlParameter(parameterName, value); SqlParameter[] prms = { prmKey, prmInterface, prmValue }; databaseManager.ExecStoredProc("p_SetDictionaryEntry", prms, out errorInfo.DatabaseErrorValue, out errorInfo.Message); // Check for unrecognised error code from the database. errorInfo.ErrorCode = BBError.ValidateDBErrorValue(errorInfo.DatabaseErrorValue, _commonDatabaseErrorCode.GeneralDBError); } catch (Exception ex) { errorInfo.ErrorCode = _commonDatabaseErrorCode.GeneralError; errorInfo.Message = ex.Message; } return(errorInfo); }
/// <summary> /// Gets the description of a specified error. /// </summary> /// <typeparam name="TErrorCodeEnum">The type of the enum that lists all the valid error /// codes.</typeparam> /// <param name="codeToLookup">Error code whose description is to be returned.</param> /// <param name="systemSettings">Settings read from a custom section of the config file /// that contains connection information for the database.</param> /// <returns>The description for the specified error code. If the specified error code is /// not found, returns an empty string.</returns> public static string GetErrorDescription <TErrorCodeEnum>(TErrorCodeEnum codeToLookup, SystemCoreSettings systemSettings) where TErrorCodeEnum : IComparable, IFormattable, IConvertible { DatabaseManager2 databaseManager = null; lock (_lockGetErrorDescription_1) { databaseManager = BBDatabase.GetDatabaseManager(systemSettings); if (databaseManager == null) { return(string.Empty); } } return(GetErrorDescription <TErrorCodeEnum>(codeToLookup, databaseManager)); }
/// <summary> /// Gets the database manager used for connecting to a database. /// </summary> /// <param name="systemSettings">Settings read from a custom section of the config file /// that contains connection information for the database.</param> /// <returns>Database manager object, configured to connect to the database, or null if /// unable to read the connection information from the config file.</returns> public static DatabaseManager2 GetDatabaseManager(SystemCoreSettings systemSettings) { lock (_lockGetDatabaseManager) { if (systemSettings == null) { return(null); } DatabaseCoreSettings databaseSettings = systemSettings.Database; string server = databaseSettings.DatabaseServer; string database = databaseSettings.Database; string login = databaseSettings.DatabaseLogin; string password = databaseSettings.DatabasePassword; DatabaseManager2 databaseManager = new DatabaseManager2(server, database, login, password); return(databaseManager); } }
/// <summary> /// Returns the error message in an ErrorInfo object. If the error message is blank and /// an error has occurred then returns the description of the error. /// </summary> /// <typeparam name="T">The type of the enum that lists all the valid error codes.</typeparam> /// <param name="errorInfo">ErrorInfo object containing information about an error.</param> /// <param name="successErrorCode">The error code that represents success.</param> /// <param name="defaultErrorCode">The default error that the ErrorInfo object may be set /// to.</param> /// <param name="systemSettings">Settings read from a custom section of the config file /// that contains connection information for the database.</param> /// <returns>An error message.</returns> public static string GetErrorMessage <TErrorCodeEnum>(ErrorInfo <TErrorCodeEnum> errorInfo, TErrorCodeEnum successErrorCode, TErrorCodeEnum defaultErrorCode, SystemCoreSettings systemSettings) where TErrorCodeEnum : IComparable, IFormattable, IConvertible { DatabaseManager2 databaseManager = null; lock (_lockGetErrorMessage_1) { databaseManager = BBDatabase.GetDatabaseManager(systemSettings); if (databaseManager == null) { return(string.Empty); } } return(BBError.GetErrorMessage <TErrorCodeEnum>(errorInfo, successErrorCode, defaultErrorCode, databaseManager)); }
/// <summary> /// Returns the error message in an ErrorInfo object. If the error message is blank and /// an error has occurred then returns the description of the error. /// </summary> /// <typeparam name="TErrorCodeEnum">The type of the enum that lists all the valid error /// codes.</typeparam> /// <param name="errorInfo">ErrorInfo object containing information about an error.</param> /// <param name="successErrorCode">The error code that represents success.</param> /// <param name="defaultErrorCode">The default error that the ErrorInfo object may be set /// to.</param> /// <param name="systemSettings">Settings read from a custom section of the config file /// that contains connection information for the database.</param> /// <returns>An error message.</returns> public static string GetErrorMessage <TErrorCodeEnum>(ErrorInfo <TErrorCodeEnum> errorInfo, TErrorCodeEnum successErrorCode, TErrorCodeEnum defaultErrorCode, DatabaseManager2 databaseManager) where TErrorCodeEnum : IComparable, IFormattable, IConvertible { lock (_lockGetErrorMessage_2) { string errorMessage = (errorInfo.Message ?? string.Empty).Trim(); if (errorInfo.Message.Length == 0 && errorInfo.ErrorCode.Equals(successErrorCode) && errorInfo.ErrorCode.Equals(defaultErrorCode)) { errorMessage = BBError.GetErrorDescription <TErrorCodeEnum>(errorInfo.ErrorCode, databaseManager); } return(errorMessage); } }
/// <summary> /// Sets up the TraceListener used to write log messages to the database. /// </summary> /// <param name="traceSource">A TraceSource that raises log messages.</param> /// <param name="databaseListenerName">The name of the TraceListener the TraceSource uses /// to write to the database.</param> /// <param name="configSettingsSectionName">The name of the section in the config file /// that contains connection information for the database.</param> /// <remarks>Only need to call this once, for one of the trace sources, as the /// TraceListener is shared between all the trace sources.</remarks> public static void SetupDatabaseLogging(TraceSource traceSource, string databaseListenerName, SystemCoreSettings systemSettings) { lock (_lockSetupDatabaseLogging) { DatabaseManager2 databaseManager = BBDatabase.GetDatabaseManager(systemSettings); DatabaseTraceListener databaseLogger = (DatabaseTraceListener)(traceSource.Listeners[databaseListenerName]); databaseLogger.DatabaseManager = databaseManager; Assembly callingAssembly = Assembly.GetCallingAssembly(); AssemblyTitleAttribute assemblyTitle = ReflectionHelper.GetAssemblyAttribute <AssemblyTitleAttribute>(callingAssembly); databaseLogger.ApplicationName = assemblyTitle.Title; databaseLogger.TraceOutputOptions |= TraceOptions.ProcessId; databaseLogger.TraceOutputOptions |= TraceOptions.ThreadId; // Skip the following helper methods when determining the name of the method that // raised the log message. databaseLogger.MethodNameMethodsToIgnore.Add(LogMethodsToIgnore.LogException); databaseLogger.MethodNameMethodsToIgnore.Add( LogMethodsToIgnore.LogStoredProcResults); } }
/// <summary> /// Gets the value of an entry in the Dictionary. /// </summary> /// <typeparam name="T">The data type of the value retrieved from the dictionary.</typeparam> /// <param name="key">Key of entry to look up.</param> /// <param name="interfaceCode">Code that uniquely identifies which Service Desk system /// is connected to this interface (eg National Help Desk, NZ Post). For dictionary /// entries that differ from one system to another (eg email alert recipients).</param> /// <param name="databaseManager">DatabaseManager that handles the connection with a /// database containing a dictionary table.</param> /// <param name="oUnitOfMeasure">Unit of measure of the dictionary entry (eg min, sec).</param> /// <param name="oErrorInfo">Error information indicating whether the value was /// successfully retrieved from the dictionary or not.</param> /// <returns>Value of dictionary entry. For value types, this is the nullable version of /// the value type.</returns> private T GetDictionaryValue <T>(string key, string interfaceCode, DatabaseManager2 databaseManager, out bool oIsNull, out UnitOfMeasure oUnitOfMeasure, out ErrorInfo <TErrorCodeEnum> oErrorInfo) { object objReturned = null; UnitOfMeasure uom = new UnitOfMeasure(); ErrorInfo <TErrorCodeEnum> errorInfo = new ErrorInfo <TErrorCodeEnum>(_commonDatabaseErrorCode.GeneralError, string.Empty, _commonDatabaseErrorCode.SuccessValue); try { SqlParameter prmKey = new SqlParameter("@Key", key); SqlParameter prmInterface = new SqlParameter("@InterfaceCode", interfaceCode); SqlParameter oprmUoM = new SqlParameter("@oUnitOfMeasure", SqlDbType.NVarChar, 10); oprmUoM.Direction = ParameterDirection.Output; SqlParameter oprmTextValue = new SqlParameter("@oTextValue", SqlDbType.NVarChar, 256); oprmTextValue.Direction = ParameterDirection.Output; SqlParameter oprmNumericValue = new SqlParameter("@oNumericValue", SqlDbType.Decimal); oprmNumericValue.Direction = ParameterDirection.Output; SqlParameter oprmDateValue = new SqlParameter("@oDateTimeValue", SqlDbType.DateTime); oprmDateValue.Direction = ParameterDirection.Output; // Must set size of binary value output parameter else .NET sets it to 0 and get // an exception. Set to arbitrary large value. SqlParameter oprmBinaryValue = new SqlParameter("@oBinaryValue", SqlDbType.VarBinary, 1000000); oprmBinaryValue.Direction = ParameterDirection.Output; SqlParameter[] prms = { prmKey, prmInterface, oprmUoM, oprmTextValue, oprmNumericValue, oprmDateValue, oprmBinaryValue }; databaseManager.ExecStoredProc("p_GetDictionaryEntry", prms, out errorInfo.DatabaseErrorValue, out errorInfo.Message); // Check for unrecognised error code from the database. errorInfo.ErrorCode = BBError.ValidateDBErrorValue <TErrorCodeEnum>(errorInfo.DatabaseErrorValue, _commonDatabaseErrorCode.GeneralDBError); // TODO: Modify to allow this method to return binary values from the database. if (errorInfo.ErrorCode.Equals(_commonDatabaseErrorCode.Success)) { if (oprmUoM.Value != DBNull.Value) { uom = ConvertUnitsOfMeasure((string)oprmUoM.Value); } if (typeof(T) == typeof(string)) { objReturned = oprmTextValue.Value; } else if (typeof(T) == typeof(decimal)) { objReturned = oprmNumericValue.Value; } else if (typeof(T) == typeof(DateTime)) { objReturned = oprmDateValue.Value; } } } catch (Exception) { objReturned = null; errorInfo.ErrorCode = _commonDatabaseErrorCode.GeneralError; } oUnitOfMeasure = uom; oErrorInfo = errorInfo; if (objReturned == DBNull.Value || objReturned == null) { oIsNull = true; return(default(T)); } oIsNull = false; return((T)objReturned); }
/// <summary> /// Initializes a new instance of the ServiceDeskObject class. /// </summary> /// <param name="databaseManager">DatabaseManager that handles the connection with a /// database containing a dictionary table.</param> /// <param name="commonDatabaseErrorCode">Structure to hold the enum values for common /// database error codes.</param> public BBApplicationDictionary(DatabaseManager2 databaseManager, CommonDatabaseErrorCode <TErrorCodeEnum> commonDatabaseErrorCode) { _databaseManager = databaseManager; _commonDatabaseErrorCode = commonDatabaseErrorCode; }
/// <summary> /// Initializes a new instance of the DatabaseTraceListener class. /// </summary> /// <param name="appName">The name of the application to trace.</param> /// <param name="dbManager">The <see cref="DatabaseManager2"/> to use.</param> public DatabaseTraceListener(string appName, DatabaseManager2 dbManager) : base(appName) { _databaseManager = dbManager; }
/// <summary> /// Initializes a new instance of the DatabaseTraceListener class. /// </summary> /// <param name="dbManager">the <see cref="DatabaseManager2"/> to use.</param> public DatabaseTraceListener(DatabaseManager2 dbManager) : this(null, dbManager) { }
/// <summary> /// Initializes a new instance of the DatabaseUpdater class with a DatabaseManager. /// </summary> /// <param name="databaseManager">DatabaseManager that handles the connection to the /// database.</param> public DatabaseUpdater(DatabaseManager2 databaseManager) : this(databaseManager.Connection) { }