init
This commit is contained in:
1
Common/DTS.Common.SettingsDB/.svn/entries
Normal file
1
Common/DTS.Common.SettingsDB/.svn/entries
Normal file
@@ -0,0 +1 @@
|
||||
12
|
||||
1
Common/DTS.Common.SettingsDB/.svn/format
Normal file
1
Common/DTS.Common.SettingsDB/.svn/format
Normal file
@@ -0,0 +1 @@
|
||||
12
|
||||
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ClassDiagram MajorVersion="1" MinorVersion="1">
|
||||
<Class Name="DTS.Settings.GlobalSetting" Collapsed="true">
|
||||
<Position X="0.5" Y="1.75" Width="1.5" />
|
||||
<NestedTypes>
|
||||
<Enum Name="DTS.Settings.GlobalSetting.XMLFields" Collapsed="true">
|
||||
<TypeIdentifier>
|
||||
<NewMemberFileName>GlobalSetting.cs</NewMemberFileName>
|
||||
</TypeIdentifier>
|
||||
</Enum>
|
||||
</NestedTypes>
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAABAAAAAAAAAAAAAAAAACAgEAAAAAAAAAAAA=</HashCode>
|
||||
<FileName>GlobalSetting.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="DTS.Settings.Setting" Collapsed="true">
|
||||
<Position X="1.75" Y="0.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAEAAAAACBBAAgAAAAAAAAgAAEAAEAAIAAACAAAAgAA=</HashCode>
|
||||
<FileName>Setting.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="DTS.Settings.SettingsDB" Collapsed="true">
|
||||
<Position X="5.25" Y="0.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AIAEAAQAAAAJgAAAAAAACIAAgAAAAAMUQAARAAAACCA=</HashCode>
|
||||
<FileName>SettingsDB.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="DTS.Settings.UserSetting" Collapsed="true">
|
||||
<Position X="2.75" Y="1.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
|
||||
<FileName>UserSetting.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Font Name="Segoe UI" Size="9" />
|
||||
</ClassDiagram>
|
||||
@@ -0,0 +1,285 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
// ReSharper disable ConditionalTernaryEqualBranch
|
||||
// ReSharper disable RedundantAssignment
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace DTS.Common.Settings
|
||||
{
|
||||
/// <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 const string CSV_IMPORT_CREATE_DYNAMIC_GROUPS = "CSVImportCreateDynamicGroups";
|
||||
private const string IMPORT_CREATE_DYNAMIC_GROUPS = "ImportCreateDynamicGroups";
|
||||
|
||||
private static SettingsDB Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
return _instance ?? (_instance = new SettingsDB());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, Setting> _settingsLookup = new Dictionary<string, Setting>();
|
||||
|
||||
private SettingsDB()
|
||||
{
|
||||
}
|
||||
|
||||
public static void RefreshSettings()
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
Instance._settingsLookup.Clear();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// retrieve property value given user
|
||||
/// creates with default value if doesn't exist
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetUserValue(string id, string defaultValue, string user)
|
||||
{
|
||||
var key = $"{id}_{user}";
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(key))
|
||||
{
|
||||
Instance._settingsLookup[key] = new UserSetting(id, defaultValue, user);
|
||||
}
|
||||
return Instance._settingsLookup[key].PropertyValue;
|
||||
}
|
||||
}
|
||||
/// <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, bool useCache = true)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id) || !useCache)
|
||||
{
|
||||
//If a setting is being added to the db, and is replacing an old
|
||||
//setting, use the value of the old setting
|
||||
if ((id == IMPORT_CREATE_DYNAMIC_GROUPS) && Instance._settingsLookup.ContainsKey(CSV_IMPORT_CREATE_DYNAMIC_GROUPS))
|
||||
{
|
||||
defaultValue = Instance._settingsLookup[CSV_IMPORT_CREATE_DYNAMIC_GROUPS].PropertyValue;
|
||||
}
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, defaultValue);
|
||||
}
|
||||
return Instance._settingsLookup[id].PropertyValue;
|
||||
}
|
||||
}
|
||||
public static void GetAllGlobalValues()
|
||||
{
|
||||
var settings = GlobalSetting.GetAllGlobalSettings();
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
foreach (var setting in settings)
|
||||
{
|
||||
Instance._settingsLookup[setting.PropertyId] = setting;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <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 double GetGlobalValueDouble(string id, double defaultValue)
|
||||
{
|
||||
var sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
var d = defaultValue;
|
||||
return !double.TryParse(sValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? defaultValue : d;
|
||||
}
|
||||
public static int GetGlobalValueInt(string id, int defaultValue)
|
||||
{
|
||||
var sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
var d = defaultValue;
|
||||
return !int.TryParse(sValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? defaultValue : d;
|
||||
}
|
||||
/// <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, bool useCache = true)
|
||||
{
|
||||
var sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture), useCache);
|
||||
var b = defaultValue;
|
||||
return !bool.TryParse(sValue, out b) ? b : b;
|
||||
}
|
||||
|
||||
public static double[] GetGlobalValueDoubleArray(string id, double[] defaultValues)
|
||||
{
|
||||
var key = $"{id}_x_Count";
|
||||
var count = GetGlobalValueInt(key, defaultValues.Length);
|
||||
|
||||
var values = new List<double>();
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var thiskey = $"{id}_x_{i}";
|
||||
values.Add(i >= defaultValues.Length
|
||||
? GetGlobalValueDouble(thiskey, 0D)
|
||||
: GetGlobalValueDouble(thiskey, defaultValues[i]));
|
||||
}
|
||||
return values.ToArray();
|
||||
}
|
||||
|
||||
public static void SetGlobalValueDoubleArray(string id, double[] values)
|
||||
{
|
||||
var key = $"{id}_x_Count";
|
||||
SetGlobalValueInt(key, values.Length);
|
||||
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
var thiskey = $"{id}_x_{i}";
|
||||
SetGlobalValueDouble(thiskey, values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static int[] GetGlobalValueIntArray(string id, int[] defaultValues)
|
||||
{
|
||||
var key = $"{id}_x_Count";
|
||||
var count = GetGlobalValueInt(key, defaultValues.Length);
|
||||
|
||||
var values = new List<int>();
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
var thiskey = $"{id}_x_{i}";
|
||||
values.Add(i >= defaultValues.Length
|
||||
? GetGlobalValueInt(thiskey, 0)
|
||||
: GetGlobalValueInt(thiskey, defaultValues[i]));
|
||||
}
|
||||
catch (Exception) { /* catch (Exception ex) { APILogger.Log(ex); } */ }
|
||||
}
|
||||
return values.ToArray();
|
||||
}
|
||||
|
||||
public static void SetGlobalValueIntArray(string id, int[] values)
|
||||
{
|
||||
var key = $"{id}_x_Count";
|
||||
SetGlobalValueInt(key, values.Length);
|
||||
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
var thiskey = $"{id}_x_{i}";
|
||||
SetGlobalValueInt(thiskey, values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// sets user specific property in database
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="user"></param>
|
||||
public static void SetUserValue(string id, string value, string user)
|
||||
{
|
||||
var key = $"{id}_{user}";
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(key))
|
||||
{
|
||||
Instance._settingsLookup[key] = new UserSetting(id, value, user);
|
||||
}
|
||||
Instance._settingsLookup[key].SetValue(value);
|
||||
}
|
||||
}
|
||||
/// <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);
|
||||
}
|
||||
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 SetGlobalValueDouble(string id, double value)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id))
|
||||
{
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
Instance._settingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetGlobalValueInt(string id, int value)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id))
|
||||
{
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
Instance._settingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
/// <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));
|
||||
}
|
||||
|
||||
if (id == IMPORT_CREATE_DYNAMIC_GROUPS)
|
||||
{
|
||||
// 36831 The ImportCreateDynamicGroups setting has replaced the CSVImportCreateDynamicGroups setting, but
|
||||
// the old setting needs to still be used by pre-Version 93 clients, so set it whenever the new one is set.
|
||||
// When a pre-Version 93 client sets the old setting, the stored procedure will also set the new one.
|
||||
if (!Instance._settingsLookup.ContainsKey(CSV_IMPORT_CREATE_DYNAMIC_GROUPS))
|
||||
{
|
||||
Instance._settingsLookup[CSV_IMPORT_CREATE_DYNAMIC_GROUPS] = new GlobalSetting(CSV_IMPORT_CREATE_DYNAMIC_GROUPS, "True");
|
||||
}
|
||||
|
||||
//This SetValue call will set both the ImportCreateDynamicGroups (new) and CSVImportCreateDynamicGroups (old) settings.
|
||||
Instance._settingsLookup[CSV_IMPORT_CREATE_DYNAMIC_GROUPS].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
Instance._settingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using DTS.Common.Storage;
|
||||
|
||||
|
||||
namespace DTS.Common.Settings
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// user specific setting, this allows users to have specific settings for columns, tables, etc in the future
|
||||
/// </summary>
|
||||
public class UserSetting : Setting
|
||||
{
|
||||
public UserSetting(string id, string defaultValue, string user)
|
||||
: base(id, PropertyTypes.User, defaultValue, user)
|
||||
{
|
||||
|
||||
}
|
||||
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 = UserId });
|
||||
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,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("SettingsDB")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("SettingsDB")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2015")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("577ab42d-e5b3-4ba5-852d-ecadfbb81f9b")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,142 @@
|
||||
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<GlobalSetting>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{61017104-D8EE-41D1-B9CA-DAD863FF78B2}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>SettingsDB</RootNamespace>
|
||||
<AssemblyName>SettingsDB</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
<SccProjectName>
|
||||
</SccProjectName>
|
||||
<SccLocalPath>
|
||||
</SccLocalPath>
|
||||
<SccAuxPath>
|
||||
</SccAuxPath>
|
||||
<SccProvider>
|
||||
</SccProvider>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x86\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
|
||||
<OutputPath>bin\x86\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="GlobalSetting.cs" />
|
||||
<Compile Include="Setting.cs" />
|
||||
<Compile Include="SettingsDB.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="UserSetting.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Common\DTS.Common.Storage\DTS.Common.Storage.csproj">
|
||||
<Project>{e0d1a7d0-dce8-403d-ba85-5a5d66ba1313}</Project>
|
||||
<Name>DTS.Common.Storage</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\Common\DTS.Common.Utilities\DTS.Common.Utilities.csproj">
|
||||
<Project>{03eace47-ea59-44ac-b49d-956e4dc4d618}</Project>
|
||||
<Name>DTS.Common.Utilities</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Design\SettingsDBClassDiagram.cd" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
@@ -0,0 +1,110 @@
|
||||
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); }
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
Common/DTS.Common.SettingsDB/.svn/wc.db
Normal file
BIN
Common/DTS.Common.SettingsDB/.svn/wc.db
Normal file
Binary file not shown.
0
Common/DTS.Common.SettingsDB/.svn/wc.db-journal
Normal file
0
Common/DTS.Common.SettingsDB/.svn/wc.db-journal
Normal file
96
Common/DTS.Common.SettingsDB/DTS.Common.Settings.csproj
Normal file
96
Common/DTS.Common.SettingsDB/DTS.Common.Settings.csproj
Normal file
@@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{61017104-D8EE-41D1-B9CA-DAD863FF78B2}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>SettingsDB</RootNamespace>
|
||||
<AssemblyName>SettingsDB</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
<SccProjectName>
|
||||
</SccProjectName>
|
||||
<SccLocalPath>
|
||||
</SccLocalPath>
|
||||
<SccAuxPath>
|
||||
</SccAuxPath>
|
||||
<SccProvider>
|
||||
</SccProvider>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x86\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
|
||||
<OutputPath>bin\x86\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="GlobalSetting.cs" />
|
||||
<Compile Include="Setting.cs" />
|
||||
<Compile Include="SettingsDB.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="UserSetting.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Common\DTS.Common.Storage\DTS.Common.Storage.csproj">
|
||||
<Project>{e0d1a7d0-dce8-403d-ba85-5a5d66ba1313}</Project>
|
||||
<Name>DTS.Common.Storage</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\Common\DTS.Common.Utilities\DTS.Common.Utilities.csproj">
|
||||
<Project>{03eace47-ea59-44ac-b49d-956e4dc4d618}</Project>
|
||||
<Name>DTS.Common.Utilities</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Design\SettingsDBClassDiagram.cd" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ClassDiagram MajorVersion="1" MinorVersion="1">
|
||||
<Class Name="DTS.Settings.GlobalSetting" Collapsed="true">
|
||||
<Position X="0.5" Y="1.75" Width="1.5" />
|
||||
<NestedTypes>
|
||||
<Enum Name="DTS.Settings.GlobalSetting.XMLFields" Collapsed="true">
|
||||
<TypeIdentifier>
|
||||
<NewMemberFileName>GlobalSetting.cs</NewMemberFileName>
|
||||
</TypeIdentifier>
|
||||
</Enum>
|
||||
</NestedTypes>
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAABAAAAAAAAAAAAAAAAACAgEAAAAAAAAAAAA=</HashCode>
|
||||
<FileName>GlobalSetting.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="DTS.Settings.Setting" Collapsed="true">
|
||||
<Position X="1.75" Y="0.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAEAAAAACBBAAgAAAAAAAAgAAEAAEAAIAAACAAAAgAA=</HashCode>
|
||||
<FileName>Setting.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="DTS.Settings.SettingsDB" Collapsed="true">
|
||||
<Position X="5.25" Y="0.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AIAEAAQAAAAJgAAAAAAACIAAgAAAAAMUQAARAAAACCA=</HashCode>
|
||||
<FileName>SettingsDB.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="DTS.Settings.UserSetting" Collapsed="true">
|
||||
<Position X="2.75" Y="1.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
|
||||
<FileName>UserSetting.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Font Name="Segoe UI" Size="9" />
|
||||
</ClassDiagram>
|
||||
142
Common/DTS.Common.SettingsDB/GlobalSetting.cs
Normal file
142
Common/DTS.Common.SettingsDB/GlobalSetting.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
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<GlobalSetting>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Common/DTS.Common.SettingsDB/Properties/AssemblyInfo.cs
Normal file
36
Common/DTS.Common.SettingsDB/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("SettingsDB")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("SettingsDB")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2015")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("577ab42d-e5b3-4ba5-852d-ecadfbb81f9b")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
110
Common/DTS.Common.SettingsDB/Setting.cs
Normal file
110
Common/DTS.Common.SettingsDB/Setting.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
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); }
|
||||
}
|
||||
}
|
||||
}
|
||||
285
Common/DTS.Common.SettingsDB/SettingsDB.cs
Normal file
285
Common/DTS.Common.SettingsDB/SettingsDB.cs
Normal file
@@ -0,0 +1,285 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
// ReSharper disable ConditionalTernaryEqualBranch
|
||||
// ReSharper disable RedundantAssignment
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace DTS.Common.Settings
|
||||
{
|
||||
/// <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 const string CSV_IMPORT_CREATE_DYNAMIC_GROUPS = "CSVImportCreateDynamicGroups";
|
||||
private const string IMPORT_CREATE_DYNAMIC_GROUPS = "ImportCreateDynamicGroups";
|
||||
|
||||
private static SettingsDB Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
return _instance ?? (_instance = new SettingsDB());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, Setting> _settingsLookup = new Dictionary<string, Setting>();
|
||||
|
||||
private SettingsDB()
|
||||
{
|
||||
}
|
||||
|
||||
public static void RefreshSettings()
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
Instance._settingsLookup.Clear();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// retrieve property value given user
|
||||
/// creates with default value if doesn't exist
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetUserValue(string id, string defaultValue, string user)
|
||||
{
|
||||
var key = $"{id}_{user}";
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(key))
|
||||
{
|
||||
Instance._settingsLookup[key] = new UserSetting(id, defaultValue, user);
|
||||
}
|
||||
return Instance._settingsLookup[key].PropertyValue;
|
||||
}
|
||||
}
|
||||
/// <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, bool useCache = true)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id) || !useCache)
|
||||
{
|
||||
//If a setting is being added to the db, and is replacing an old
|
||||
//setting, use the value of the old setting
|
||||
if ((id == IMPORT_CREATE_DYNAMIC_GROUPS) && Instance._settingsLookup.ContainsKey(CSV_IMPORT_CREATE_DYNAMIC_GROUPS))
|
||||
{
|
||||
defaultValue = Instance._settingsLookup[CSV_IMPORT_CREATE_DYNAMIC_GROUPS].PropertyValue;
|
||||
}
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, defaultValue);
|
||||
}
|
||||
return Instance._settingsLookup[id].PropertyValue;
|
||||
}
|
||||
}
|
||||
public static void GetAllGlobalValues()
|
||||
{
|
||||
var settings = GlobalSetting.GetAllGlobalSettings();
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
foreach (var setting in settings)
|
||||
{
|
||||
Instance._settingsLookup[setting.PropertyId] = setting;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <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 double GetGlobalValueDouble(string id, double defaultValue)
|
||||
{
|
||||
var sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
var d = defaultValue;
|
||||
return !double.TryParse(sValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? defaultValue : d;
|
||||
}
|
||||
public static int GetGlobalValueInt(string id, int defaultValue)
|
||||
{
|
||||
var sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
var d = defaultValue;
|
||||
return !int.TryParse(sValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? defaultValue : d;
|
||||
}
|
||||
/// <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, bool useCache = true)
|
||||
{
|
||||
var sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture), useCache);
|
||||
var b = defaultValue;
|
||||
return !bool.TryParse(sValue, out b) ? b : b;
|
||||
}
|
||||
|
||||
public static double[] GetGlobalValueDoubleArray(string id, double[] defaultValues)
|
||||
{
|
||||
var key = $"{id}_x_Count";
|
||||
var count = GetGlobalValueInt(key, defaultValues.Length);
|
||||
|
||||
var values = new List<double>();
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var thiskey = $"{id}_x_{i}";
|
||||
values.Add(i >= defaultValues.Length
|
||||
? GetGlobalValueDouble(thiskey, 0D)
|
||||
: GetGlobalValueDouble(thiskey, defaultValues[i]));
|
||||
}
|
||||
return values.ToArray();
|
||||
}
|
||||
|
||||
public static void SetGlobalValueDoubleArray(string id, double[] values)
|
||||
{
|
||||
var key = $"{id}_x_Count";
|
||||
SetGlobalValueInt(key, values.Length);
|
||||
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
var thiskey = $"{id}_x_{i}";
|
||||
SetGlobalValueDouble(thiskey, values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static int[] GetGlobalValueIntArray(string id, int[] defaultValues)
|
||||
{
|
||||
var key = $"{id}_x_Count";
|
||||
var count = GetGlobalValueInt(key, defaultValues.Length);
|
||||
|
||||
var values = new List<int>();
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
var thiskey = $"{id}_x_{i}";
|
||||
values.Add(i >= defaultValues.Length
|
||||
? GetGlobalValueInt(thiskey, 0)
|
||||
: GetGlobalValueInt(thiskey, defaultValues[i]));
|
||||
}
|
||||
catch (Exception) { /* catch (Exception ex) { APILogger.Log(ex); } */ }
|
||||
}
|
||||
return values.ToArray();
|
||||
}
|
||||
|
||||
public static void SetGlobalValueIntArray(string id, int[] values)
|
||||
{
|
||||
var key = $"{id}_x_Count";
|
||||
SetGlobalValueInt(key, values.Length);
|
||||
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
var thiskey = $"{id}_x_{i}";
|
||||
SetGlobalValueInt(thiskey, values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// sets user specific property in database
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="user"></param>
|
||||
public static void SetUserValue(string id, string value, string user)
|
||||
{
|
||||
var key = $"{id}_{user}";
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(key))
|
||||
{
|
||||
Instance._settingsLookup[key] = new UserSetting(id, value, user);
|
||||
}
|
||||
Instance._settingsLookup[key].SetValue(value);
|
||||
}
|
||||
}
|
||||
/// <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);
|
||||
}
|
||||
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 SetGlobalValueDouble(string id, double value)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id))
|
||||
{
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
Instance._settingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetGlobalValueInt(string id, int value)
|
||||
{
|
||||
lock (LOCK_OBJECT)
|
||||
{
|
||||
if (!Instance._settingsLookup.ContainsKey(id))
|
||||
{
|
||||
Instance._settingsLookup[id] = new GlobalSetting(id, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
Instance._settingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
/// <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));
|
||||
}
|
||||
|
||||
if (id == IMPORT_CREATE_DYNAMIC_GROUPS)
|
||||
{
|
||||
// 36831 The ImportCreateDynamicGroups setting has replaced the CSVImportCreateDynamicGroups setting, but
|
||||
// the old setting needs to still be used by pre-Version 93 clients, so set it whenever the new one is set.
|
||||
// When a pre-Version 93 client sets the old setting, the stored procedure will also set the new one.
|
||||
if (!Instance._settingsLookup.ContainsKey(CSV_IMPORT_CREATE_DYNAMIC_GROUPS))
|
||||
{
|
||||
Instance._settingsLookup[CSV_IMPORT_CREATE_DYNAMIC_GROUPS] = new GlobalSetting(CSV_IMPORT_CREATE_DYNAMIC_GROUPS, "True");
|
||||
}
|
||||
|
||||
//This SetValue call will set both the ImportCreateDynamicGroups (new) and CSVImportCreateDynamicGroups (old) settings.
|
||||
Instance._settingsLookup[CSV_IMPORT_CREATE_DYNAMIC_GROUPS].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
Instance._settingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
61
Common/DTS.Common.SettingsDB/UserSetting.cs
Normal file
61
Common/DTS.Common.SettingsDB/UserSetting.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using DTS.Common.Storage;
|
||||
|
||||
|
||||
namespace DTS.Common.Settings
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// user specific setting, this allows users to have specific settings for columns, tables, etc in the future
|
||||
/// </summary>
|
||||
public class UserSetting : Setting
|
||||
{
|
||||
public UserSetting(string id, string defaultValue, string user)
|
||||
: base(id, PropertyTypes.User, defaultValue, user)
|
||||
{
|
||||
|
||||
}
|
||||
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 = UserId });
|
||||
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,4 @@
|
||||
// <autogenerated />
|
||||
using System;
|
||||
using System.Reflection;
|
||||
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.8", FrameworkDisplayName = "")]
|
||||
@@ -0,0 +1,4 @@
|
||||
// <autogenerated />
|
||||
using System;
|
||||
using System.Reflection;
|
||||
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user