From 278356cebbffd1b52edc094c36586cbd0fc13b75 Mon Sep 17 00:00:00 2001 From: Gabriel Santos Date: Tue, 16 Dec 2025 13:17:12 -0500 Subject: [PATCH 1/4] Migrated functions to search db to private function so that we may use them elswhere later SC-319 --- .../openXDA.Model/SystemCenter/ValueList.cs | 123 ++++++++++-------- 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs b/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs index 3335be1e1b..467de28d44 100644 --- a/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs +++ b/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs @@ -48,7 +48,6 @@ public class ValueList public string Value { get; set; } public string AltValue { get; set; } public int SortOrder { get; set; } - } public class ValueListController : ModelController @@ -57,59 +56,14 @@ public class ValueListController : ModelController [HttpGet, Route("Group/{groupName}")] public IHttpActionResult GetValueListForGroup(string groupName) { - using (AdoDataConnection connection = new AdoDataConnection(Connection)) - { - TableOperations groupTable = new TableOperations(connection); - TableOperations valueTable = new TableOperations(connection); - List groupIds = groupTable.QueryRecordsWhere("Name = {0}", groupName).Select(group => group.ID).ToList(); - if (groupIds.Count() == 0) - { - RestrictedValueList restriction = RestrictedValueList.List.Find((g) => g.Name == groupName); - if (!(restriction is null)) - { - groupTable.AddNewRecord( - new ValueListGroup() - { - Description = "", - Name = restriction.Name - }); - groupIds.Add(connection.ExecuteScalar("SELECT @@IDENTITY")); + if (!GetAuthCheck()) + return Unauthorized(); - int sortOrder = 1; - foreach (object item in restriction.DefaultItems) + using (AdoDataConnection connection = ConnectionFactory()) { - string value; - string altValue; - if (item.GetType() == typeof(Tuple)) - { - value = ((Tuple)item).Item1; - altValue = ((Tuple)item).Item2; + return Ok(GetGroup(groupName, connection)); } - else if (item.GetType() == typeof(string)) - { - value = (string)item; - altValue = (string)item; } - else - throw new InvalidCastException($"Could not convert object in DefaultItems of value list {restriction.Name} to either tuple or string."); - valueTable.AddNewRecord( - new ValueList() - { - GroupID = groupIds[0], - Value = value, - AltValue = altValue, - SortOrder = sortOrder - }); - sortOrder++; - } - } - else - return Ok(new List()); - } - IEnumerable records = valueTable.QueryRecordsWhere("GroupID in ({0})", string.Join(", ", groupIds)).OrderBy(v => v.SortOrder); - return Ok(records); - } - } public override IHttpActionResult Patch([FromBody] ValueList newRecord) { @@ -192,9 +146,66 @@ public override IHttpActionResult Delete(ValueList record) [Route("Count/{groupName}/{value}"), HttpGet] public IHttpActionResult GetCount(string groupName, string value) { - if (!PatchAuthCheck()) + if (!GetAuthCheck()) return Unauthorized(); - using (AdoDataConnection connection = new AdoDataConnection(Connection)) + + using (AdoDataConnection connection = ConnectionFactory()) + return Ok(GetCount(groupName, value, connection)); + } + + private IEnumerable GetGroup(string groupName, AdoDataConnection connection) + { + TableOperations groupTable = new TableOperations(connection); + TableOperations valueTable = new TableOperations(connection); + List groupIds = groupTable.QueryRecordsWhere("Name = {0}", groupName).Select(group => group.ID).ToList(); + if (groupIds.Count() == 0) + { + RestrictedValueList restriction = RestrictedValueList.List.Find((g) => g.Name == groupName); + if (!(restriction is null)) + { + groupTable.AddNewRecord( + new ValueListGroup() + { + Description = "", + Name = restriction.Name + }); + groupIds.Add(connection.ExecuteScalar("SELECT @@IDENTITY")); + + int sortOrder = 1; + foreach (object item in restriction.DefaultItems) + { + string value; + string altValue; + if (item.GetType() == typeof(Tuple)) + { + value = ((Tuple)item).Item1; + altValue = ((Tuple)item).Item2; + } + else if (item.GetType() == typeof(string)) + { + value = (string)item; + altValue = (string)item; + } + else + throw new InvalidCastException($"Could not convert object in DefaultItems of value list {restriction.Name} to either tuple or string."); + valueTable.AddNewRecord( + new ValueList() + { + GroupID = groupIds[0], + Value = value, + AltValue = altValue, + SortOrder = sortOrder + }); + sortOrder++; + } + } + else + return new List(); + } + return valueTable.QueryRecordsWhere("GroupID in ({0})", string.Join(", ", groupIds)).OrderBy(v => v.SortOrder); + } + + private int GetCount(string groupName, string value, AdoDataConnection connection) { int nAddlFields = connection.ExecuteScalar(@"SELECT COUNT(AFV.ID) FROM AdditionalFieldValue AFV WHERE [Value] = {0} AND (SELECT TOP 1 AF.ID FROM AdditionalField AF WHERE Type = {1}) = AFV.AdditionalFieldID @@ -202,12 +213,10 @@ public IHttpActionResult GetCount(string groupName, string value) RestrictedValueList restriction = RestrictedValueList.List.Find((g) => g.Name == groupName); int count = 0; if (!(restriction?.CountSQL is null)) - { count = connection.ExecuteScalar(restriction.CountSQL, value); + + return nAddlFields + count; } - return Ok(nAddlFields + count); } - } - } -} \ No newline at end of file + } \ No newline at end of file From c95d313c6bd8a25e8ed96a7fb6f68574d9f3e2c4 Mon Sep 17 00:00:00 2001 From: Gabriel Santos Date: Tue, 16 Dec 2025 13:17:34 -0500 Subject: [PATCH 2/4] cleanup deprecated SC-319 --- Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs b/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs index 467de28d44..9c845125cd 100644 --- a/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs +++ b/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs @@ -36,10 +36,9 @@ namespace SystemCenter.Model [PatchRoles("Administrator, Transmission SME")] [GetRoles("Administrator, Transmission SME")] [AllowSearch] - [ TableName("ValueList"), UseEscapedName, PrimaryLabel("Text"), SettingsCategory("systemSettings")] + [TableName("ValueList"), UseEscapedName, PrimaryLabel("Text"), SettingsCategory("systemSettings")] public class ValueList { - [PrimaryKey(true)] public int ID { get; set; } @@ -76,7 +75,7 @@ public override IHttpActionResult Patch([FromBody] ValueList newRecord) bool changeVal = false; ValueList oldRecord; - using (AdoDataConnection connection = new AdoDataConnection(Connection)) + using (AdoDataConnection connection = ConnectionFactory()) { oldRecord = new TableOperations(connection).QueryRecordWhere("ID = {0}", newRecord.ID); changeVal = !(newRecord.Value == oldRecord.Value); @@ -85,7 +84,7 @@ public override IHttpActionResult Patch([FromBody] ValueList newRecord) if (changeVal) { ValueListGroup group; - using (AdoDataConnection connection = new AdoDataConnection(Connection)) + using (AdoDataConnection connection = ConnectionFactory()) { group = new TableOperations(connection).QueryRecordWhere("ID = {0}", newRecord.GroupID); // Wrapping is needed here, since C# tries to use the wrong method signature otherwise @@ -122,7 +121,7 @@ public override IHttpActionResult Delete(ValueList record) } ValueListGroup group; - using (AdoDataConnection connection = new AdoDataConnection(Connection)) + using (AdoDataConnection connection = ConnectionFactory()) { group = new TableOperations(connection).QueryRecordWhere("ID = {0}", record.GroupID); RestrictedValueList restriction = RestrictedValueList.List.Find((g) => g.Name == group.Name); From c0287fb4da5320cedf419f231821b25b7577003c Mon Sep 17 00:00:00 2001 From: Gabriel Santos Date: Tue, 16 Dec 2025 13:18:58 -0500 Subject: [PATCH 3/4] function to get counts for entire list, cannot go through sql due to C# only SQL definitions SC-319 --- .../openXDA.Model/SystemCenter/ValueList.cs | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs b/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs index 9c845125cd..9df01707f6 100644 --- a/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs +++ b/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs @@ -21,10 +21,10 @@ // //****************************************************************************************************** +using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; -using System; using GSF.ComponentModel.DataAnnotations; using GSF.Data; using GSF.Data.Model; @@ -59,10 +59,24 @@ public IHttpActionResult GetValueListForGroup(string groupName) return Unauthorized(); using (AdoDataConnection connection = ConnectionFactory()) - { + { return Ok(GetGroup(groupName, connection)); - } - } + } + } + + [Route("Count/{groupName}"), HttpGet] + public IHttpActionResult GetValueListForGroupWithCounts(string groupName) + { + if (!PatchAuthCheck()) + return Unauthorized(); + + using (AdoDataConnection connection = ConnectionFactory()) + { + IEnumerable group = GetGroup(groupName, connection); + group.Select(item => new { item.ID, Count = GetCount(groupName, item.Value) }); + return Ok(group.ToList()); + } + } public override IHttpActionResult Patch([FromBody] ValueList newRecord) { @@ -205,17 +219,17 @@ private IEnumerable GetGroup(string groupName, AdoDataConnection conn } private int GetCount(string groupName, string value, AdoDataConnection connection) - { - int nAddlFields = connection.ExecuteScalar(@"SELECT COUNT(AFV.ID) FROM AdditionalFieldValue AFV WHERE + { + int nAddlFields = connection.ExecuteScalar(@"SELECT COUNT(AFV.ID) FROM AdditionalFieldValue AFV WHERE [Value] = {0} AND (SELECT TOP 1 AF.ID FROM AdditionalField AF WHERE Type = {1}) = AFV.AdditionalFieldID ", value, groupName); - RestrictedValueList restriction = RestrictedValueList.List.Find((g) => g.Name == groupName); - int count = 0; - if (!(restriction?.CountSQL is null)) - count = connection.ExecuteScalar(restriction.CountSQL, value); + RestrictedValueList restriction = RestrictedValueList.List.Find((g) => g.Name == groupName); + int count = 0; + if (!(restriction?.CountSQL is null)) + count = connection.ExecuteScalar(restriction.CountSQL, value); return nAddlFields + count; - } - } + } + } - } \ No newline at end of file +} \ No newline at end of file From d16c780e8fd9fc91e284d94df28b02a2ef0757ad Mon Sep 17 00:00:00 2001 From: Gabriel Santos Date: Tue, 16 Dec 2025 15:59:31 -0500 Subject: [PATCH 4/4] Changed endpoint to be a dictionary since that is easier to work with frontend SC-319 --- .../Libraries/openXDA.Model/SystemCenter/ValueList.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs b/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs index 9df01707f6..3384c69b9e 100644 --- a/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs +++ b/Source/Libraries/openXDA.Model/SystemCenter/ValueList.cs @@ -29,6 +29,7 @@ using GSF.Data; using GSF.Data.Model; using GSF.Web.Model; +using Newtonsoft.Json.Linq; namespace SystemCenter.Model { @@ -65,16 +66,17 @@ public IHttpActionResult GetValueListForGroup(string groupName) } [Route("Count/{groupName}"), HttpGet] - public IHttpActionResult GetValueListForGroupWithCounts(string groupName) + public IHttpActionResult GetValueListCountDictionary(string groupName) { if (!PatchAuthCheck()) return Unauthorized(); using (AdoDataConnection connection = ConnectionFactory()) { - IEnumerable group = GetGroup(groupName, connection); - group.Select(item => new { item.ID, Count = GetCount(groupName, item.Value) }); - return Ok(group.ToList()); + JObject dictionary = new JObject(); + foreach(ValueList item in GetGroup(groupName, connection)) + dictionary.Add(item.ID.ToString(), GetCount(groupName, item.Value, connection).ToString()); + return Ok(dictionary); } }