Skip to content

Commit

Permalink
Export/Import Properties of CorrelationFilter (#812)
Browse files Browse the repository at this point in the history
* Export/Import Properties of CorrelationFilter

---------

Co-authored-by: Sean Feldman <[email protected]>
  • Loading branch information
robionekenobi and SeanFeldman authored Nov 20, 2024
1 parent 2d46b03 commit 286be53
Showing 1 changed file with 66 additions and 6 deletions.
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 tRule = ruleFilter as CorrelationFilter;
var tProps = propertyValue[CorrelationFilterProperties] as List<Property>;
foreach (var tProp in tProps)
{
tRule.Properties.Add(tProp.Name, tProp.Value);
}

}
}
if (ruleFilter != null)
{

// As we already created the Properties, we must remove it here before calling SetPropertyValue, as Properties Property is ReadOnly
var tPropVal = propertyValue.Where(p => p.Key != CorrelationFilterProperties);
var tPropVal2 = tPropVal.ToDictionary(p => p.Key, p => p.Value);
SetPropertyValue(propertyDictionary,
propertyValue,
tPropVal2,
ruleFilter);
}
return ruleFilter;
Expand Down

0 comments on commit 286be53

Please sign in to comment.