From 2d70357526f20c319947bc38cadd7bcae806fee2 Mon Sep 17 00:00:00 2001 From: robionekenobi Date: Tue, 19 Nov 2024 19:22:29 +0100 Subject: [PATCH 1/8] Export/Import Properties of CorrelationFilter --- src/Common/Helpers/ImportExportHelper.cs | 77 ++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/src/Common/Helpers/ImportExportHelper.cs b/src/Common/Helpers/ImportExportHelper.cs index c95d9f2a..f4a9694a 100644 --- a/src/Common/Helpers/ImportExportHelper.cs +++ b/src/Common/Helpers/ImportExportHelper.cs @@ -21,6 +21,11 @@ #region References +using Microsoft.Azure.NotificationHubs; +using Microsoft.ServiceBus; +using Microsoft.ServiceBus.Management; +using Microsoft.ServiceBus.Messaging; +using ServiceBusExplorer.Utilities.Helpers; using System; using System.Collections.Generic; using System.ComponentModel; @@ -33,10 +38,6 @@ using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; -using Microsoft.Azure.NotificationHubs; -using ServiceBusExplorer.Utilities.Helpers; -using Microsoft.ServiceBus; -using Microsoft.ServiceBus.Messaging; #endregion @@ -90,6 +91,8 @@ public class ImportExportHelper private const string NamespaceAttribute = "serviceBusNamespace"; private const string Unknown = "Unknown"; private const string ExtensionData = "ExtensionData"; + private const string CorrelationFilterProperties = "Properties"; + private const string CorrelationFilterProperty = "Property"; private const string EntityFormat = "{0}"; private const string LambdaExpressionFilterFqdn = "Microsoft.ServiceBus.Messaging.Filters.LambdaExpressionFilter"; private const string LambdaExpressionRuleActionFqdn = "Microsoft.ServiceBus.Messaging.Filters.LambdaExpressionRuleAction"; @@ -336,8 +339,13 @@ void GetProperties(Type type, bool canRead, bool canWrite) { return; } + // + // We must add the Properties of the CorrelationFilter. As the Properties are not CanWrite, we must specifically add it by not testing the canWrite parameter + // var propertyDictionary = propertyArray. - Where(p => p.Name == RelayType || (p.CanRead == canRead && p.CanWrite == canWrite && p.Name != ExtensionData && p.PropertyType != typeof(DateTime))). + Where(p => p.Name == RelayType || + (p.CanRead == canRead && p.CanWrite == canWrite && p.Name != ExtensionData && p.PropertyType != typeof(DateTime)) || + (p.CanRead == canRead && p.CanWrite == false && p.Name == CorrelationFilterProperties)). ToDictionary(p => p.Name); propertyCache[fullName] = propertyDictionary; } @@ -423,6 +431,26 @@ private static void GetPropertyValue(Dictionary propertyDi if (property.PropertyType == typeof(string)) { propertyValue[name] = xmlReader.ReadElementContentAsString(); + return; + } + // + // Here we fill in the Property of the Filter CorrelationId Properties by looping through the Property Sibling + // + if (property.MemberType == MemberTypes.Property && property.PropertyType.Name == "IDictionary`2") + { + bool bOk = xmlReader.ReadToDescendant("Property"); + while (bOk) + { + var _read = xmlReader.ReadElementContentAsString(); + var _prop = new Property(); + _prop.Name = _read.Split(':')[0]; + _prop.Value = _read.Split(':')[1]; + if (!propertyValue.ContainsKey(name)) + propertyValue[name] = new List(); + var _props = propertyValue[name] as List; + _props.Add(_prop); + bOk = xmlReader.ReadToNextSibling("Property"); + } } } @@ -533,6 +561,24 @@ async Task SerializeEntity(ServiceBusHelper serviceBusHelper, XmlWriter xmlWrite xmlWriter.WriteEndElement(); continue; } + // + // We check if the CorrelationFilter has Properties. + // If yes we write the xml A + // + if (string.Compare(keyValuePair.Key, + CorrelationFilterProperties, + StringComparison.InvariantCultureIgnoreCase) == 0 && + (entity is CorrelationFilter)) + { + var filt = entity as CorrelationFilter; + xmlWriter.WriteStartElement(CorrelationFilterProperties); + foreach (var prop in filt.Properties) + { + xmlWriter.WriteElementString(CorrelationFilterProperty, string.Format("{0}: {1}", prop.Key, prop.Value)); + } + xmlWriter.WriteEndElement(); + continue; + } xmlWriter.WriteStartElement(keyValuePair.Value.Name); if (value is Filter || value is RuleAction || value is ApnsCredential || value is WnsCredential) { @@ -1349,11 +1395,30 @@ private Filter CreateFilter(XElement filter) if (filter.Name == string.Format(NodeNameFormat, Namespace, CorrelationFilterEntity)) { ruleFilter = new CorrelationFilter(propertyValue[CorrelationId] as string); + // + // We check if there is a Key with Properties + // If Yes, we must create the properties of the CorrelationFilter + // + if (propertyValue.ContainsKey(CorrelationFilterProperties)) + { + var _rule = ruleFilter as CorrelationFilter; + var _props = propertyValue[CorrelationFilterProperties] as List; + foreach (var _prop in _props) + { + _rule.Properties.Add(_prop.Name, _prop.Value); + } + + } } if (ruleFilter != null) { + // + // As we already created the Properties, we must remove it here before calling SetPropertyValue, as Properties Property is ReadOnly + // + var _propVal = propertyValue.Where(p => p.Key != CorrelationFilterProperties); + var _propVal2 = _propVal.ToDictionary(p => p.Key, p => p.Value); SetPropertyValue(propertyDictionary, - propertyValue, + _propVal2, ruleFilter); } return ruleFilter; From ceca6df14891101e9324cc97647bbe964ea3b995 Mon Sep 17 00:00:00 2001 From: "RobiOne (Robert Grange)" Date: Tue, 19 Nov 2024 21:34:12 +0100 Subject: [PATCH 2/8] Update src/Common/Helpers/ImportExportHelper.cs Co-authored-by: Sean Feldman --- src/Common/Helpers/ImportExportHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Common/Helpers/ImportExportHelper.cs b/src/Common/Helpers/ImportExportHelper.cs index f4a9694a..346815e9 100644 --- a/src/Common/Helpers/ImportExportHelper.cs +++ b/src/Common/Helpers/ImportExportHelper.cs @@ -339,9 +339,8 @@ void GetProperties(Type type, bool canRead, bool canWrite) { return; } - // + // We must add the Properties of the CorrelationFilter. As the Properties are not CanWrite, we must specifically add it by not testing the canWrite parameter - // var propertyDictionary = propertyArray. Where(p => p.Name == RelayType || (p.CanRead == canRead && p.CanWrite == canWrite && p.Name != ExtensionData && p.PropertyType != typeof(DateTime)) || From d97ccbf18084f2627c6e144bf94169c79d87ed32 Mon Sep 17 00:00:00 2001 From: "RobiOne (Robert Grange)" Date: Tue, 19 Nov 2024 21:34:19 +0100 Subject: [PATCH 3/8] Update src/Common/Helpers/ImportExportHelper.cs Co-authored-by: Sean Feldman --- src/Common/Helpers/ImportExportHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Common/Helpers/ImportExportHelper.cs b/src/Common/Helpers/ImportExportHelper.cs index 346815e9..4008043b 100644 --- a/src/Common/Helpers/ImportExportHelper.cs +++ b/src/Common/Helpers/ImportExportHelper.cs @@ -432,9 +432,8 @@ private static void GetPropertyValue(Dictionary propertyDi propertyValue[name] = xmlReader.ReadElementContentAsString(); return; } - // + // Here we fill in the Property of the Filter CorrelationId Properties by looping through the Property Sibling - // if (property.MemberType == MemberTypes.Property && property.PropertyType.Name == "IDictionary`2") { bool bOk = xmlReader.ReadToDescendant("Property"); From c2829170db525559f2acd2f9205bacd0f8521b20 Mon Sep 17 00:00:00 2001 From: "RobiOne (Robert Grange)" Date: Tue, 19 Nov 2024 21:34:40 +0100 Subject: [PATCH 4/8] Update src/Common/Helpers/ImportExportHelper.cs Co-authored-by: Sean Feldman --- src/Common/Helpers/ImportExportHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Common/Helpers/ImportExportHelper.cs b/src/Common/Helpers/ImportExportHelper.cs index 4008043b..59c40231 100644 --- a/src/Common/Helpers/ImportExportHelper.cs +++ b/src/Common/Helpers/ImportExportHelper.cs @@ -559,10 +559,9 @@ async Task SerializeEntity(ServiceBusHelper serviceBusHelper, XmlWriter xmlWrite xmlWriter.WriteEndElement(); continue; } - // + // We check if the CorrelationFilter has Properties. // If yes we write the xml A - // if (string.Compare(keyValuePair.Key, CorrelationFilterProperties, StringComparison.InvariantCultureIgnoreCase) == 0 && From db14400a8da89d3864a3f7b01233e6cb2c6dbfae Mon Sep 17 00:00:00 2001 From: "RobiOne (Robert Grange)" Date: Tue, 19 Nov 2024 21:34:48 +0100 Subject: [PATCH 5/8] Update src/Common/Helpers/ImportExportHelper.cs Co-authored-by: Sean Feldman --- src/Common/Helpers/ImportExportHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Common/Helpers/ImportExportHelper.cs b/src/Common/Helpers/ImportExportHelper.cs index 59c40231..1c56e5fe 100644 --- a/src/Common/Helpers/ImportExportHelper.cs +++ b/src/Common/Helpers/ImportExportHelper.cs @@ -1392,10 +1392,9 @@ private Filter CreateFilter(XElement filter) if (filter.Name == string.Format(NodeNameFormat, Namespace, CorrelationFilterEntity)) { ruleFilter = new CorrelationFilter(propertyValue[CorrelationId] as string); - // + // We check if there is a Key with Properties // If Yes, we must create the properties of the CorrelationFilter - // if (propertyValue.ContainsKey(CorrelationFilterProperties)) { var _rule = ruleFilter as CorrelationFilter; From 86ce0ccf2eee2f70b8ba831a0222a1faa19eb671 Mon Sep 17 00:00:00 2001 From: "RobiOne (Robert Grange)" Date: Tue, 19 Nov 2024 21:34:55 +0100 Subject: [PATCH 6/8] Update src/Common/Helpers/ImportExportHelper.cs Co-authored-by: Sean Feldman --- src/Common/Helpers/ImportExportHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Common/Helpers/ImportExportHelper.cs b/src/Common/Helpers/ImportExportHelper.cs index 1c56e5fe..eb59e840 100644 --- a/src/Common/Helpers/ImportExportHelper.cs +++ b/src/Common/Helpers/ImportExportHelper.cs @@ -1408,9 +1408,8 @@ private Filter CreateFilter(XElement filter) } if (ruleFilter != null) { - // + // As we already created the Properties, we must remove it here before calling SetPropertyValue, as Properties Property is ReadOnly - // var _propVal = propertyValue.Where(p => p.Key != CorrelationFilterProperties); var _propVal2 = _propVal.ToDictionary(p => p.Key, p => p.Value); SetPropertyValue(propertyDictionary, From ac5b85a0625a85f47487ce8efdc2a7a71982b0a9 Mon Sep 17 00:00:00 2001 From: robionekenobi Date: Tue, 19 Nov 2024 21:43:42 +0100 Subject: [PATCH 7/8] Corrected as requested to meet the coding requierments --- src/Common/Helpers/ImportExportHelper.cs | 27 ++++++++++-------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Common/Helpers/ImportExportHelper.cs b/src/Common/Helpers/ImportExportHelper.cs index f4a9694a..6c22a036 100644 --- a/src/Common/Helpers/ImportExportHelper.cs +++ b/src/Common/Helpers/ImportExportHelper.cs @@ -339,9 +339,8 @@ void GetProperties(Type type, bool canRead, bool canWrite) { return; } - // + // We must add the Properties of the CorrelationFilter. As the Properties are not CanWrite, we must specifically add it by not testing the canWrite parameter - // var propertyDictionary = propertyArray. Where(p => p.Name == RelayType || (p.CanRead == canRead && p.CanWrite == canWrite && p.Name != ExtensionData && p.PropertyType != typeof(DateTime)) || @@ -433,22 +432,21 @@ private static void GetPropertyValue(Dictionary propertyDi propertyValue[name] = xmlReader.ReadElementContentAsString(); return; } - // + // Here we fill in the Property of the Filter CorrelationId Properties by looping through the Property Sibling - // if (property.MemberType == MemberTypes.Property && property.PropertyType.Name == "IDictionary`2") { bool bOk = xmlReader.ReadToDescendant("Property"); while (bOk) { - var _read = xmlReader.ReadElementContentAsString(); - var _prop = new Property(); - _prop.Name = _read.Split(':')[0]; - _prop.Value = _read.Split(':')[1]; + var tRead = xmlReader.ReadElementContentAsString().Split(':'); + var tProp = new Property(); + tProp.Name = tRead[0]; + tProp.Value = tRead[1]; if (!propertyValue.ContainsKey(name)) propertyValue[name] = new List(); - var _props = propertyValue[name] as List; - _props.Add(_prop); + var tProps = propertyValue[name] as List; + tProps.Add(tProp); bOk = xmlReader.ReadToNextSibling("Property"); } } @@ -561,10 +559,9 @@ async Task SerializeEntity(ServiceBusHelper serviceBusHelper, XmlWriter xmlWrite xmlWriter.WriteEndElement(); continue; } - // + // We check if the CorrelationFilter has Properties. // If yes we write the xml A - // if (string.Compare(keyValuePair.Key, CorrelationFilterProperties, StringComparison.InvariantCultureIgnoreCase) == 0 && @@ -1395,10 +1392,9 @@ private Filter CreateFilter(XElement filter) if (filter.Name == string.Format(NodeNameFormat, Namespace, CorrelationFilterEntity)) { ruleFilter = new CorrelationFilter(propertyValue[CorrelationId] as string); - // + // We check if there is a Key with Properties // If Yes, we must create the properties of the CorrelationFilter - // if (propertyValue.ContainsKey(CorrelationFilterProperties)) { var _rule = ruleFilter as CorrelationFilter; @@ -1412,9 +1408,8 @@ private Filter CreateFilter(XElement filter) } if (ruleFilter != null) { - // + // As we already created the Properties, we must remove it here before calling SetPropertyValue, as Properties Property is ReadOnly - // var _propVal = propertyValue.Where(p => p.Key != CorrelationFilterProperties); var _propVal2 = _propVal.ToDictionary(p => p.Key, p => p.Value); SetPropertyValue(propertyDictionary, From 021f3284a9c680b1f53fb6b8ca5cfe1449263778 Mon Sep 17 00:00:00 2001 From: robionekenobi Date: Tue, 19 Nov 2024 23:46:57 +0100 Subject: [PATCH 8/8] Some more corrections as requested to meet the coding requierments --- src/Common/Helpers/ImportExportHelper.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Common/Helpers/ImportExportHelper.cs b/src/Common/Helpers/ImportExportHelper.cs index 6c22a036..f5a5c155 100644 --- a/src/Common/Helpers/ImportExportHelper.cs +++ b/src/Common/Helpers/ImportExportHelper.cs @@ -1397,11 +1397,11 @@ private Filter CreateFilter(XElement filter) // If Yes, we must create the properties of the CorrelationFilter if (propertyValue.ContainsKey(CorrelationFilterProperties)) { - var _rule = ruleFilter as CorrelationFilter; - var _props = propertyValue[CorrelationFilterProperties] as List; - foreach (var _prop in _props) + var tRule = ruleFilter as CorrelationFilter; + var tProps = propertyValue[CorrelationFilterProperties] as List; + foreach (var tProp in tProps) { - _rule.Properties.Add(_prop.Name, _prop.Value); + tRule.Properties.Add(tProp.Name, tProp.Value); } } @@ -1410,10 +1410,10 @@ private Filter CreateFilter(XElement filter) { // As we already created the Properties, we must remove it here before calling SetPropertyValue, as Properties Property is ReadOnly - var _propVal = propertyValue.Where(p => p.Key != CorrelationFilterProperties); - var _propVal2 = _propVal.ToDictionary(p => p.Key, p => p.Value); + var tPropVal = propertyValue.Where(p => p.Key != CorrelationFilterProperties); + var tPropVal2 = tPropVal.ToDictionary(p => p.Key, p => p.Value); SetPropertyValue(propertyDictionary, - _propVal2, + tPropVal2, ruleFilter); } return ruleFilter;