Files
DP44/Common/DTS.Common.SettingsDB/Setting.cs
2026-04-17 14:55:32 -04:00

111 lines
4.0 KiB
C#

using System;
using System.Data;
using System.Data.SqlClient;
using DTS.Common.Storage;
using DTS.Common.Utilities.Logging;
namespace DTS.Common.Settings
{
/// <summary>
/// generic base class for settings
/// </summary>
public abstract class Setting
{
protected string _propertyId;
/// <summary>
/// string id of property
/// </summary>
public string PropertyId => _propertyId;
protected int _propertyType;
/// <summary>
/// type of property (global or user specific)
/// </summary>
public PropertyTypes PropertyType => (PropertyTypes)_propertyType;
protected string _propertyValue;
/// <summary>
/// value of property
/// does not currently do length checking and there is a max length
/// </summary>
public string PropertyValue => _propertyValue;
protected string _userId;
/// <summary>
/// User for user specific settings
/// </summary>
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); }
}
}
}