/// <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> /// 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> /// 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); }