From a17b163d177c54a8324e033183340c03ef4bb649 Mon Sep 17 00:00:00 2001 From: Raghid Kawash Date: Thu, 28 May 2020 14:59:10 +0200 Subject: [PATCH] Added FindAndReplace RuleType --- .../DataAnonymizationPage.xaml.cs | 47 ++++++++++++++++++- .../CosmosCloneCommon/Model/ScrubRule.cs | 3 +- .../Utility/ObjectScrubber.cs | 23 +++++---- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/CosmosClone/CosmicCloneUI/DataAnonymizationPage.xaml.cs b/CosmosClone/CosmicCloneUI/DataAnonymizationPage.xaml.cs index b57a9ec..626460f 100644 --- a/CosmosClone/CosmicCloneUI/DataAnonymizationPage.xaml.cs +++ b/CosmosClone/CosmicCloneUI/DataAnonymizationPage.xaml.cs @@ -129,6 +129,10 @@ void AddRuleWithData(WrapPanel parentStackPanel, int ruleIndex, ScrubRule scrubR scrubTypeSP.Orientation = Orientation.Horizontal; scrubTypeSP.Margin = new Thickness(0, 5, 0, 2); + StackPanel findValueSP = new StackPanel(); + findValueSP.Orientation = Orientation.Horizontal; + findValueSP.Margin = new Thickness(0, 5, 0, 2); + StackPanel scrubValueSP = new StackPanel(); scrubValueSP.Orientation = Orientation.Horizontal; scrubValueSP.Margin = new Thickness(0, 5, 0, 2); @@ -197,6 +201,19 @@ void AddRuleWithData(WrapPanel parentStackPanel, int ruleIndex, ScrubRule scrubR ScrubTypeCB.SelectionChanged += new SelectionChangedEventHandler(scrubTypeComboBox_SelectedIndexChanged); + TextBlock FindValueLabel = new TextBlock(); + FindValueLabel.Text = "Find"; + FindValueLabel.FontSize = 15; + FindValueLabel.VerticalAlignment = VerticalAlignment.Center; + FindValueLabel.Margin = new Thickness(10, 0, 0, 5); + + TextBox FindValueTB = new TextBox(); + FindValueTB.Name = "FindValue" + ruleIndex; + FindValueTB.Width = 150; + FindValueTB.HorizontalContentAlignment = HorizontalAlignment.Left; + FindValueTB.VerticalAlignment = VerticalAlignment.Center; + FindValueTB.Margin = new Thickness(20, 0, 0, 0); + TextBlock ScrubValueLabel = new TextBlock(); ScrubValueLabel.Text = "Replace with"; ScrubValueLabel.FontSize = 15; @@ -217,6 +234,8 @@ void AddRuleWithData(WrapPanel parentStackPanel, int ruleIndex, ScrubRule scrubR ScrubTypeLabel.Width = 120; ScrubTypeCB.Width = 150; ScrubValueLabel.Width = 120; + FindValueTB.Width = 150; + FindValueLabel.Width = 120; filterSP.Children.Add(FilterLabel); filterSP.Children.Add(FilterTB); @@ -227,6 +246,10 @@ void AddRuleWithData(WrapPanel parentStackPanel, int ruleIndex, ScrubRule scrubR scrubTypeSP.Children.Add(ScrubTypeLabel); scrubTypeSP.Children.Add(ScrubTypeCB); + findValueSP.Children.Add(FindValueLabel); + findValueSP.Children.Add(FindValueTB); + findValueSP.Visibility = Visibility.Collapsed; + scrubValueSP.Children.Add(ScrubValueLabel); scrubValueSP.Children.Add(ScrubValueTB); scrubValueSP.Children.Add(RuleIdLabel); @@ -235,6 +258,7 @@ void AddRuleWithData(WrapPanel parentStackPanel, int ruleIndex, ScrubRule scrubR sp.Children.Add(attributeSP); sp.Children.Add(filterSP); sp.Children.Add(scrubTypeSP); + sp.Children.Add(findValueSP); sp.Children.Add(scrubValueSP); exp.Content = sp; @@ -245,6 +269,7 @@ void AddRuleWithData(WrapPanel parentStackPanel, int ruleIndex, ScrubRule scrubR AttributeScrubTB.Text = scrubRule.PropertyName; if(scrubRule.Type!=null) ScrubTypeCB.SelectedIndex = (int)scrubRule.Type; ScrubValueTB.Text = scrubRule.UpdateValue; + FindValueTB.Text = scrubRule.FindValue; } parentStackPanel.Children.Add(exp); if(!SaveRuleButton.IsEnabled) @@ -263,13 +288,23 @@ private void scrubTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { var parentPanel = (StackPanel)cbox.Parent; var gpPanel = (StackPanel)parentPanel.Parent; + gpPanel.Children[3].Visibility = Visibility.Collapsed; + gpPanel.Children[4].Visibility = Visibility.Visible; + } + else if (scrubType == RuleType.FindAndReplace.ToString()) + { + var parentPanel = (StackPanel)cbox.Parent; + var gpPanel = (StackPanel)parentPanel.Parent; + gpPanel.Children[3].Visibility = Visibility.Visible; + gpPanel.Children[4].Visibility = Visibility.Visible; } else { var parentPanel = (StackPanel)cbox.Parent; var gpPanel = (StackPanel)parentPanel.Parent; - gpPanel.Children[3].Visibility = Visibility.Hidden; + gpPanel.Children[3].Visibility = Visibility.Collapsed; + gpPanel.Children[4].Visibility = Visibility.Hidden; } } @@ -353,6 +388,10 @@ public List getScrubRules() { sr.UpdateValue = tb.Text.Trim(); } + else if (tb.Name.StartsWith("FindValue")) + { + sr.FindValue = tb.Text.Trim(); + } } if (uiElement.GetType().Name == "ComboBox") @@ -414,7 +453,11 @@ public bool validateInput() { validationMessages.Add($"Rule:{rule.RuleId} - Filter condition starts improperly. Sample c.EntityType=\"document\" "); } - } + } + if (rule.Type == RuleType.FindAndReplace && string.IsNullOrEmpty(rule.FindValue)) + { + validationMessages.Add($"Rule:{rule.RuleId} - Find is empty"); + } } if(validationMessages.Count > 0) { diff --git a/CosmosClone/CosmosCloneCommon/Model/ScrubRule.cs b/CosmosClone/CosmosCloneCommon/Model/ScrubRule.cs index 7cd528e..1e00374 100644 --- a/CosmosClone/CosmosCloneCommon/Model/ScrubRule.cs +++ b/CosmosClone/CosmosCloneCommon/Model/ScrubRule.cs @@ -15,6 +15,7 @@ public class ScrubRule public int RuleId { get; set; } public string FilterCondition { get; set; } public string PropertyName { get; set; } + public string FindValue { get; set; } public string UpdateValue { get; set; } public RuleType? Type { get; set; } @@ -38,5 +39,5 @@ public ScrubRule(string filterCondition, string propertyName, RuleType type, str } - public enum RuleType { SingleValue, NullValue, Shuffle, PartialMaskFromLeft, PartialMaskFromRight };//Can add random rule type later if required. + public enum RuleType { SingleValue, NullValue, Shuffle, PartialMaskFromLeft, PartialMaskFromRight, FindAndReplace };//Can add random rule type later if required. } \ No newline at end of file diff --git a/CosmosClone/CosmosCloneCommon/Utility/ObjectScrubber.cs b/CosmosClone/CosmosCloneCommon/Utility/ObjectScrubber.cs index 096b721..dc33007 100644 --- a/CosmosClone/CosmosCloneCommon/Utility/ObjectScrubber.cs +++ b/CosmosClone/CosmosCloneCommon/Utility/ObjectScrubber.cs @@ -16,13 +16,13 @@ public List ScrubObjectList(List srcList, ScrubRule scrubRule) //var scrubbedObjects = new List(); var scrubbedObjects = new List(); var propNames = scrubRule.PropertyName.Split('.').ToList(); - if(scrubRule.Type == RuleType.NullValue || scrubRule.Type == RuleType.SingleValue || scrubRule.Type == RuleType.PartialMaskFromLeft || scrubRule.Type == RuleType.PartialMaskFromRight) + if(scrubRule.Type == RuleType.NullValue || scrubRule.Type == RuleType.SingleValue || scrubRule.Type == RuleType.PartialMaskFromLeft || scrubRule.Type == RuleType.PartialMaskFromRight || scrubRule.Type == RuleType.FindAndReplace) { foreach (var strObj in srcList) { try { - JToken jToken = GetUpdatedJsonArrayValue((JToken)JObject.Parse(strObj), propNames, scrubRule.UpdateValue, scrubRule.Type); + JToken jToken = GetUpdatedJsonArrayValue((JToken)JObject.Parse(strObj), propNames, scrubRule.FindValue, scrubRule.UpdateValue, scrubRule.Type); scrubbedObjects.Add(jToken); } catch(Exception ex) @@ -205,7 +205,7 @@ public JToken GetDocumentShuffledToken(JToken token, List propNames, ref return jTokenResult; } - public JToken GetUpdatedJsonArrayValue(JToken token, List propNames, string overwritevalue, RuleType? ruleType) + public JToken GetUpdatedJsonArrayValue(JToken token, List propNames, string findvalue, string overwritevalue, RuleType? ruleType) { if (token == null || token.Type == JTokenType.Null) return null; @@ -227,7 +227,7 @@ public JToken GetUpdatedJsonArrayValue(JToken token, List propNames, str { if (jArray[k][currentProperty] != null && jArray[k][currentProperty].Type != JTokenType.Null) { - jArray[k][currentProperty] = ScrubTokenValue(ruleType, jArray[k][currentProperty], overwritevalue); + jArray[k][currentProperty] = ScrubTokenValue(ruleType, jArray[k][currentProperty], findvalue, overwritevalue); } continue; } @@ -235,7 +235,7 @@ public JToken GetUpdatedJsonArrayValue(JToken token, List propNames, str { if (jArray[k] != null && jArray[k][currentProperty].Type != JTokenType.Null) { - jArray[k] = GetUpdatedJsonArrayValue(jArray[k], propNames.GetRange(1, propNames.Count - 1), overwritevalue, ruleType); + jArray[k] = GetUpdatedJsonArrayValue(jArray[k], propNames.GetRange(1, propNames.Count - 1), findvalue, overwritevalue, ruleType); continue; } //else return null; @@ -251,14 +251,14 @@ public JToken GetUpdatedJsonArrayValue(JToken token, List propNames, str { if (jObj[currentProperty] != null && jObj[currentProperty].Type != JTokenType.Null) { - jObj[currentProperty] = ScrubTokenValue(ruleType, jObj[currentProperty], overwritevalue); + jObj[currentProperty] = ScrubTokenValue(ruleType, jObj[currentProperty], findvalue, overwritevalue); } } else { if (jObj[currentProperty] != null && jObj[currentProperty].Type != JTokenType.Null) { - jObj[currentProperty] = GetUpdatedJsonArrayValue((JToken)jObj[currentProperty], propNames.GetRange(1, propNames.Count - 1), overwritevalue, ruleType); + jObj[currentProperty] = GetUpdatedJsonArrayValue((JToken)jObj[currentProperty], propNames.GetRange(1, propNames.Count - 1), findvalue, overwritevalue, ruleType); } //else return null; } @@ -274,7 +274,7 @@ public JToken GetUpdatedJsonArrayValue(JToken token, List propNames, str return jTokenResult; } - private JToken ScrubTokenValue(RuleType? ruleType, JToken tokenToBeScrubbed, string overwriteValue) + private JToken ScrubTokenValue(RuleType? ruleType, JToken tokenToBeScrubbed, string findValue, string overwriteValue) { if (ruleType.HasValue) { @@ -301,6 +301,13 @@ private JToken ScrubTokenValue(RuleType? ruleType, JToken tokenToBeScrubbed, str tokenToBeScrubbed = string.Concat(oldValue.Remove(oldValue.Length - overwriteValue.Length, overwriteValue.Length), overwriteValue); } } + else if (ruleType == RuleType.FindAndReplace) + { + if (oldValue.Contains(findValue)) + { + tokenToBeScrubbed = oldValue.Replace(findValue, overwriteValue); + } + } else { tokenToBeScrubbed = overwriteValue;