using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using DTS.Common.Storage; using DTS.Common.Utilities.Logging; // ReSharper disable CheckNamespace // ReSharper disable InconsistentNaming namespace DTS.Common.Settings { public class GlobalSetting : Setting { private const string SYSTEM = "SYSTEM"; protected GlobalSetting() : base("", PropertyTypes.Global, "", SYSTEM) { } public GlobalSetting(string id, string defaultPropertyValue) : base(id, PropertyTypes.Global, defaultPropertyValue, SYSTEM) { } public static GlobalSetting ReadXML(System.Xml.XmlElement root) { var gs = new GlobalSetting(); foreach (var node in root.ChildNodes) { if (node is System.Xml.XmlElement element) { ProcessXMLElement(element, ref gs); } } return gs; } public enum XMLFields { NAME, VALUE }; private static void ProcessXMLElement(System.Xml.XmlElement node, ref GlobalSetting gs) { XMLFields field; if (Enum.TryParse(node.Name, out field)) { switch (field) { case XMLFields.NAME: gs._propertyId = node.InnerText; break; case XMLFields.VALUE: gs._propertyValue = node.InnerText; break; default: // TODO: handle exception properly throw new NotImplementedException("Unsupported xml tag for GlobalSetting: " + field.ToString()); } } } public class NullDefaultValueException : NullReferenceException { } public static GlobalSetting[] GetAllGlobalSettings() { var list = new List(); try { using (var cmd = DbOperations.GetSQLCommand(true)) { try { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_SettingsGet.ToString(); cmd.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar) { Value = SYSTEM }); cmd.Parameters.Add(new SqlParameter("@PropertyId", SqlDbType.NVarChar) { Value = DBNull.Value }); var reader = cmd.ExecuteReader(); while (reader.Read()) { var gs = new GlobalSetting() { _propertyId = Convert.ToString(reader["PropertyId"]), _propertyType = Convert.ToInt32(reader["PropertyType"]), _propertyValue = Convert.ToString(reader["PropertyValue"]) }; list.Add(gs); } } finally { cmd.Connection.Dispose(); } } } catch (Exception ex) { APILogger.Log($"Failed to retrieve global settings", ex); } return list.ToArray(); } protected override void GetPropertyValue(string defaultValue) { try { using (var cmd = DbOperations.GetSQLCommand(true)) { try { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_SettingsGet.ToString(); cmd.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar) { Value = SYSTEM }); cmd.Parameters.Add(new SqlParameter("@PropertyId", SqlDbType.NVarChar) { Value = PropertyId }); var reader = cmd.ExecuteReader(); if (reader.Read()) { _propertyValue = Convert.ToString(reader[DbOperations.Settings.UserFields.PropertyValue.ToString()]); } else { //null being passed in will create an exception when we try to StoreInDB //handle this as an explicit case //26754 Exceptions thrown and eaten when Export is clicked in Export Settings if (null == defaultValue) { throw new NullDefaultValueException(); } _propertyValue = defaultValue; StoreInDB(); } } finally { cmd.Connection.Dispose(); } } } catch (NullDefaultValueException) { //we tried to retrieve a global setting, it didn't exist in the db, we don't have a default value //return this as an explicit case //26754 Exceptions thrown and eaten when Export is clicked in Export Settings throw; } catch (Exception ex) { APILogger.Log($"Failed to retrieve global setting {PropertyId} - {defaultValue}", ex); _propertyValue = defaultValue; } } } }