init
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace DatabaseImport
|
||||
{
|
||||
public class GlobalSetting : Setting
|
||||
{
|
||||
private const string SYSTEM = "SYSTEM";
|
||||
public GlobalSetting(string id, string defaultPropertyValue)
|
||||
: base(id, PropertyTypes.Global, defaultPropertyValue, SYSTEM)
|
||||
{
|
||||
}
|
||||
protected override void GetPropertyValue(string defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
//if (_allGlobalSetting == null)
|
||||
//{
|
||||
// _allGlobalSetting = GetAllGlobalSetting();
|
||||
//}
|
||||
//foreach (var gs in _allGlobalSetting)
|
||||
//{
|
||||
// if (gs.PropertyId != PropertyId) continue;
|
||||
// _propertyValue = gs._propertyValue;
|
||||
// return;
|
||||
//}
|
||||
|
||||
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 });
|
||||
|
||||
using (var ds = DbOperations.Connection.QueryDataSet(cmd))
|
||||
{
|
||||
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
_propertyValue =
|
||||
Convert.ToString(
|
||||
ds.Tables[0].Rows[0][
|
||||
DbOperations.Settings.UserFields.PropertyValue.ToString()]);
|
||||
}
|
||||
else
|
||||
{
|
||||
_propertyValue = defaultValue;
|
||||
StoreInDB();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally { cmd.Connection.Dispose(); }
|
||||
}
|
||||
}
|
||||
catch (Exception) //TODO: handle exception properly
|
||||
{
|
||||
_propertyValue = defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace DatabaseImport
|
||||
{
|
||||
/// <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;
|
||||
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;
|
||||
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) {/* APILogger.LogException(ex);*/ }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DatabaseImport
|
||||
{
|
||||
/// <summary>
|
||||
/// handles storing/retrieving settings from a db
|
||||
/// assumes DB connection is already established or connection information has already been set for db
|
||||
/// </summary>
|
||||
public class SettingsDB
|
||||
{
|
||||
private static SettingsDB _instance;
|
||||
private static readonly object LOCK_OBJECT = new object();
|
||||
|
||||
private static SettingsDB Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
return _instance ?? (_instance = new SettingsDB());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, Setting> _settingsLookup = new Dictionary<string, Setting>();
|
||||
/// <summary>
|
||||
/// retrieves a global value, creates using default value if property doesn't exist yet
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetGlobalValue(string id, string defaultValue)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id))
|
||||
{
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, defaultValue);
|
||||
}
|
||||
return Instance._settingsLookup[id].PropertyValue;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// returns the global value for given property, or the default value if no value currently exists
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool GetGlobalValueBool(string id, bool defaultValue)
|
||||
{
|
||||
var sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
var b = defaultValue;
|
||||
return !bool.TryParse(sValue, out b) ? b : b;
|
||||
}
|
||||
/// <summary>
|
||||
/// stores an application global property in the database
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetGlobalValue(string id, string value)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id))
|
||||
{
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
Instance._settingsLookup[id].SetValue(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// stores an application global property value in the database
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetGlobalValueBoolean(string id, bool value)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id))
|
||||
{
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
else
|
||||
{
|
||||
Instance._settingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user