Beispiel #1
0
        /// <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>
        /// Method that actually writes the log message to the database.
        /// </summary>
        /// <param name="logEntryFields">Fields to write to the log.</param>
        /// <param name="doWriteLine">Determines whether to perform a WriteLine or a Write to the log.</param>
        /// <returns>True if successful.</returns>
        protected override bool WriteToCustomLog(LogEntryFields logEntryFields, bool doWriteLine)
        //protected override bool WriteToCustomLog(string message, string category, string detailedMessage, int eventID,
        //    string source, string methodThatWroteToLog, DateTime eventDateTime, long eventTimestamp,
        //    string relatedActivityID, int processID, string threadID,
        //    string callStack, string logicalOperationStack, bool doWriteLine)
        {
            bool isOK = false;

            try
            {
                SqlParameter prmMessage         = new SqlParameter("@Message", logEntryFields.Message);
                SqlParameter prmDetailedMessage = new SqlParameter("@DetailedMessage",
                                                                   logEntryFields.DetailedMessage);
                SqlParameter prmCategory      = new SqlParameter("@Category", logEntryFields.Category);
                SqlParameter prmEventID       = new SqlParameter("@EventID", logEntryFields.EventID);
                SqlParameter prmProcedureName = new SqlParameter("@ProcedureName",
                                                                 logEntryFields.MethodThatWroteToLog);
                SqlParameter prmSource      = new SqlParameter("@Source", logEntryFields.Source);
                SqlParameter prmApplication = new SqlParameter("@Application",
                                                               this.ApplicationName);
                SqlParameter prmProcessID = new SqlParameter("@ProcessID",
                                                             logEntryFields.ProcessID);
                SqlParameter prmThreadID      = new SqlParameter("@ThreadID", logEntryFields.ThreadID);
                SqlParameter prmRelActivityID = new SqlParameter("@RelatedActivityID",
                                                                 logEntryFields.RelatedActivityID);
                SqlParameter prmCallStack = new SqlParameter("@CallStack",
                                                             logEntryFields.CallStack);
                SqlParameter prmLogicalOpStack = new SqlParameter("@LogicalOperationStack",
                                                                  logEntryFields.LogicalOperationStack);
                SqlParameter[] prms =
                {
                    prmMessage,
                    prmDetailedMessage,
                    prmCategory,
                    prmEventID,
                    prmProcedureName,
                    prmSource,
                    prmApplication,
                    prmProcessID,
                    prmThreadID,
                    prmRelActivityID,
                    prmCallStack,
                    prmLogicalOpStack
                };
                string sqlErrorMessage;
                int    storedProcReturnVal;
                _databaseManager.ExecStoredProc("p_SaveLogMessage", prms,
                                                out storedProcReturnVal, out sqlErrorMessage);
                isOK = (storedProcReturnVal == 0);
            }
            catch
            {
                isOK = false;
            }
            return(isOK);
        }
        /// <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 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);
        }