Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export/Import Properties of CorrelationFilter #812

Merged
merged 9 commits into from
Nov 20, 2024
72 changes: 66 additions & 6 deletions src/Common/Helpers/ImportExportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -336,8 +339,12 @@ 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;
}
Expand Down Expand Up @@ -423,6 +430,25 @@ private static void GetPropertyValue(Dictionary<string, PropertyInfo> 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 tRead = xmlReader.ReadElementContentAsString().Split(':');
var tProp = new Property();
tProp.Name = tRead[0];
tProp.Value = tRead[1];
if (!propertyValue.ContainsKey(name))
propertyValue[name] = new List<Property>();
var tProps = propertyValue[name] as List<Property>;
tProps.Add(tProp);
bOk = xmlReader.ReadToNextSibling("Property");
}
}
}

Expand Down Expand Up @@ -533,6 +559,23 @@ async Task SerializeEntity(ServiceBusHelper serviceBusHelper, XmlWriter xmlWrite
xmlWriter.WriteEndElement();
continue;
}

// We check if the CorrelationFilter has Properties.
// If yes we write the xml <Properties><Property>A</Property></Properties>
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)
{
Expand Down Expand Up @@ -1349,11 +1392,28 @@ 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;
robionekenobi marked this conversation as resolved.
Show resolved Hide resolved
var _props = propertyValue[CorrelationFilterProperties] as List<Property>;
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);
robionekenobi marked this conversation as resolved.
Show resolved Hide resolved
var _propVal2 = _propVal.ToDictionary(p => p.Key, p => p.Value);
SetPropertyValue(propertyDictionary,
propertyValue,
_propVal2,
ruleFilter);
}
return ruleFilter;
Expand Down