Files
DP44/DataPRO/Users/UserSettings/UserHistory.cs
2026-04-17 14:55:32 -04:00

183 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using DTS.Common.Storage;
// ReSharper disable RedundantEmptyDefaultSwitchBranch
// ReSharper disable once CheckNamespace
namespace DTS.Slice.Users.UserSettings
{
/// <summary>
/// user specific properties, stored in db
///
/// these are all the user specific settings we are storing in the db currently as generic settings
/// we have a few user specific settings like last used hardware that are stored in other places, that maybe
/// should be stored here as well
///
/// it's possible to retrieve all the settings at once (more efficient, only one db query), or retrieve settings individually
/// one by one using the static accessors
/// each setting has a default specified both in code and in the db.
/// when retrieving a setting we fall back to the default if a user specific value is not found
/// if the default setting in the db is not found, we can further fall back to the default specified in code
///
/// in the db we have a table that lists all the settings with their fall back default value
/// we have a separate table that stores the user specific values for any of those settings, which may or may not match the fall
/// back default value
///
/// the data should always be stored in invariant form in the db
/// DisplayName, Description attributes translate so the properties can be displayed in a property grid
/// </summary>
public class UserHistory
{
#region properties
[CategoryAttributeEx(PropertyEnums.PropertyCategories.UserHistory)]
[PropertyId(PropertyEnums.PropertyIds.UsersCurrentTestSetup)]
[DescriptionAttributeEx(PropertyEnums.PropertyIds.UsersCurrentTestSetup)]
[DisplayAttributeEx(PropertyEnums.PropertyIds.UsersCurrentTestSetup)]
[DefaultValue("")]
[ReadOnly(true)]
public string UsersCurrentTestSetup { get; set; }
[CategoryAttributeEx(PropertyEnums.PropertyCategories.UserHistory)]
[PropertyId(PropertyEnums.PropertyIds.LastRunTestSetup)]
[DescriptionAttributeEx(PropertyEnums.PropertyIds.LastRunTestSetup)]
[DisplayAttributeEx(PropertyEnums.PropertyIds.LastRunTestSetup)]
[DefaultValue("")]
[ReadOnly(true)]
public string LastRunTestSetup { get; set; }
[CategoryAttributeEx(PropertyEnums.PropertyCategories.UserHistory)]
[PropertyId(PropertyEnums.PropertyIds.LastUsedSampleRate)]
[DescriptionAttributeEx(PropertyEnums.PropertyIds.LastUsedSampleRate)]
[DisplayAttributeEx(PropertyEnums.PropertyIds.LastUsedSampleRate)]
[DefaultValue(20000D)]
[ReadOnly(true)]
public double LastUsedSampleRate { get; set; }
[CategoryAttributeEx(PropertyEnums.PropertyCategories.UserHistory)]
[PropertyId(PropertyEnums.PropertyIds.ApplySensorDataToBlankChannels)]
[DescriptionAttributeEx(PropertyEnums.PropertyIds.ApplySensorDataToBlankChannels)]
[DisplayAttributeEx(PropertyEnums.PropertyIds.ApplySensorDataToBlankChannels)]
[DefaultValue(true)]
[ReadOnly(true)]
public bool ApplySensorDataToBlankChannels { get; set; }
[CategoryAttributeEx(PropertyEnums.PropertyCategories.UserHistory)]
[PropertyId(PropertyEnums.PropertyIds.ApplyHardwareAssignmentInHardwareDisco)]
[DescriptionAttributeEx(PropertyEnums.PropertyIds.ApplyHardwareAssignmentInHardwareDisco)]
[DisplayAttributeEx(PropertyEnums.PropertyIds.ApplyHardwareAssignmentInHardwareDisco)]
[DefaultValue(true)]
[ReadOnly(true)]
[Browsable(false)]
public bool ApplyHardwareAssignmentInHardwareDisco { get; set; }
[CategoryAttributeEx(PropertyEnums.PropertyCategories.UserHistory)]
[PropertyId(PropertyEnums.PropertyIds.ShowOptionsWhenApplyingSensorToBlankChannel)]
[DescriptionAttributeEx(PropertyEnums.PropertyIds.ShowOptionsWhenApplyingSensorToBlankChannel)]
[DisplayAttributeEx(PropertyEnums.PropertyIds.ShowOptionsWhenApplyingSensorToBlankChannel)]
[DefaultValue(true)]
[ReadOnly(true)]
public bool ShowOptionsWhenApplyingSensorToBlankChannel { get; set; }
#endregion properties
#region Static Methods
/// <summary>
/// this function is designed to create any missing property settings if needed
/// this might happen if the database table was just created for instance
/// it will create defaults for all the settings in the db, and also settings for all users
/// currently it's used by DataPRO.App
/// </summary>
public static void CreateAnyMissingUserHistory()
{
//get a list of all known properties
var userHistory = new UserHistory();
var properties = userHistory.GetType().GetProperties();
var needToBeAdded = new Dictionary<int, PropertyInfo>();
foreach (var property in properties)
{
var id = PropertyIdAttribute.GetPropertyId(property);
needToBeAdded[id] = property;
}
//find if the property already exists in the db, if so, no need to add...
using (var sql = DbOperations.GetSQLCommand(true))
{
try
{
sql.CommandText = "SELECT [PropertyId] from [DefaultProperties]";
using (var ds = DbOperations.Connection.QueryDataSet(sql))
{
foreach (DataRow row in ds.Tables[0].Rows)
{
var id = Convert.ToInt32(row["PropertyId"]);
if (needToBeAdded.ContainsKey(id))
{
needToBeAdded.Remove(id);
}
}
}
}
finally
{
sql.Connection.Dispose();
}
}
//if we have nothing to add, we are all done
if (needToBeAdded.Values.Count <= 0) return;
//otherwise add the defaults
using (var sql = DbOperations.GetSQLCommand(true))
{
try
{
var sb = new StringBuilder();
sb.Append(DbOperations.BEGIN_STATEMENT);
using (var needToBeAddedEnumerator = needToBeAdded.GetEnumerator())
{
var iIndex = 0;
while (needToBeAddedEnumerator.MoveNext())
{
var currentProperty = needToBeAddedEnumerator.Current.Value;
sb.AppendFormat(
"INSERT INTO [DefaultProperties] ([PropertyId],[PropertyName],[DefaultValue]) VALUES (@1_{0}, @2_{0}, @3_{0});",
iIndex);
DbOperations.CreateParam(sql, string.Format("@1_{0}", iIndex), SqlDbType.Int,
PropertyIdAttribute.GetPropertyId(currentProperty));
DbOperations.CreateParam(sql, string.Format("@2_{0}", iIndex), SqlDbType.NVarChar,
PropertyIdAttribute.GetPropertyIdEnum(currentProperty).ToString());
DbOperations.CreateParam(sql, string.Format("@3_{0}", iIndex), SqlDbType.NVarChar,
TestSetupDefaults.GetDefaultValueAsString(currentProperty));
iIndex++;
}
}
sb.Append(DbOperations.COMMIT_STATEMENT);
sql.CommandText = sb.ToString();
DbOperations.Connection.ExecuteCommand(sql);
}
finally
{
sql.Connection.Dispose();
}
}
//finally add settings for any users we have currently
TestSetupDefaults.CreateMissingUserSettingProperties(needToBeAdded.Values.ToArray());
}
#endregion
#region methods
#endregion
}
}