using System; using System.Data; using System.Data.SqlClient; using DTS.Common.Storage; using DTS.Common.Utilities.Logging; namespace DTS.Common.Settings { /// /// generic base class for settings /// public abstract class Setting { protected string _propertyId; /// /// string id of property /// public string PropertyId => _propertyId; protected int _propertyType; /// /// type of property (global or user specific) /// public PropertyTypes PropertyType => (PropertyTypes)_propertyType; protected string _propertyValue; /// /// value of property /// does not currently do length checking and there is a max length /// public string PropertyValue => _propertyValue; protected string _userId; /// /// User for user specific settings /// public string UserId => _userId; public enum PropertyTypes { User = 1 << 0, Global = 1 << 1 } protected Setting(string id, PropertyTypes type, string defaultPropertyValue, string userId) { _userId = userId; _propertyId = id; _propertyType = (int)type; if (!string.IsNullOrWhiteSpace(id)) { GetPropertyValue(defaultPropertyValue); } } protected abstract void GetPropertyValue(string defaultValue); public void SetValue(string value) { _propertyValue = value; StoreInDB(); } protected void StoreInDB() { try { using (var cmd = DbOperations.GetSQLCommand(true)) { try { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_SettingsUpdateInsert.ToString(); #region params cmd.Parameters.Add( new SqlParameter("@PropertyId", SqlDbType.NVarChar, 255) { Value = PropertyId }); cmd.Parameters.Add(new SqlParameter("@PropertyType", SqlDbType.Int) { Value = _propertyType }); cmd.Parameters.Add( new SqlParameter("@PropertyValue", SqlDbType.NVarChar, 255) { Value = PropertyValue }); cmd.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 255) { Value = UserId }); var newIdParam = new SqlParameter("@new_id", SqlDbType.Int) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(newIdParam); var errorNumberParam = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(errorNumberParam); var errorMessageParam = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(errorMessageParam); #endregion params cmd.ExecuteNonQuery(); if (int.Parse(errorNumberParam.Value.ToString()) != 0) { //errorMessageParam.Value } } finally { cmd.Connection.Dispose(); } } } catch (Exception ex) { APILogger.LogException(ex); } } } }