/// <summary> /// Builds a compare difference /// </summary> /// <param name="name">Name</param> /// <param name="newValue">New Value</param> /// <param name="oldValue">Old Value</param> /// <param name="labelKey">Text key used for the label</param> /// <param name="textKey">Text key used for the text</param> /// <returns>Compare Difference</returns> private CompareDifference BuildCompareDifference(CompareDifferenceValue name, CompareDifferenceValue newValue, CompareDifferenceValue oldValue, string labelKey, string textKey) { CompareDifference difference = new CompareDifference(); difference.Name = name; difference.NewValue = newValue; difference.OldValue = oldValue; difference.LabelKey = labelKey; difference.TextKey = textKey; return(difference); }
/// <summary> /// Compares the export snippets of an object /// </summary> /// <param name="objectId">Id of the object</param> /// <param name="result">Result to fill</param> /// <returns>Task</returns> private async Task CompareExportSnippets(string objectId, CompareResult result) { List <ObjectExportSnippet> newSnippets = await _objectExportSnippetDbAccess.GetExportSnippets(objectId); List <ObjectExportSnippet> oldSnippets = await _objectExportSnippetSnapshotDbAccess.GetExportSnippetSnapshots(objectId); CompareDifference difference = BuildCompareDifference("ExportSnippetsChanged", null, null, "ExportSnippetsChanged", string.Empty); difference.SubDifferences = CompareImplementationComparableList(newSnippets, oldSnippets); if (difference.SubDifferences.Count > 0) { result.CompareDifference.Add(difference); } }
/// <summary> /// Compares the values of two objects /// </summary> /// <param name="currentState">Current State of the object</param> /// <param name="oldState">Old State of the object</param> /// <returns>Compare results</returns> private List <CompareDifference> CompareValues(IImplementationComparable currentState, IImplementationComparable oldState) { List <CompareDifference> compareResults = new List <CompareDifference>(); List <PropertyInfo> props = currentState.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(ValueCompareAttribute))).ToList(); foreach (PropertyInfo curProperty in props) { object newValue = curProperty.GetValue(currentState); object oldValue = curProperty.GetValue(oldState); if (newValue == null && oldValue == null) { continue; } ValueCompareAttribute compareAttribute = curProperty.GetCustomAttribute <ValueCompareAttribute>(); if ((newValue != null && oldValue == null) || (newValue == null && oldValue != null)) { compareResults.Add(BuildCompareDifference(curProperty.Name, newValue, oldValue, compareAttribute.LabelKey, compareAttribute.TextKey)); } else if (newValue is IImplementationComparable && oldValue is IImplementationComparable) { // Compare complex subobject List <CompareDifference> differences = CompareValues((IImplementationComparable)newValue, (IImplementationComparable)oldValue); differences.AddRange(CompareLists((IImplementationComparable)newValue, (IImplementationComparable)oldValue)); if (differences.Any()) { CompareDifference compareDifference = BuildCompareDifference(curProperty.Name, null, null, compareAttribute.LabelKey, compareAttribute.TextKey); compareDifference.SubDifferences = differences; compareResults.Add(compareDifference); } } else if (!newValue.Equals(oldValue)) { compareResults.Add(BuildCompareDifference(curProperty.Name, newValue, oldValue, compareAttribute.LabelKey, compareAttribute.TextKey)); } } return(compareResults); }
/// <summary> /// Compares the lists of two objects /// </summary> /// <param name="currentState">Current State of the object</param> /// <param name="oldState">Old State of the object</param> /// <returns>Compare results</returns> private List <CompareDifference> CompareLists(IImplementationComparable currentState, IImplementationComparable oldState) { List <CompareDifference> compareResults = new List <CompareDifference>(); List <PropertyInfo> props = currentState.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(ListCompareAttribute))).ToList(); foreach (PropertyInfo curProperty in props) { object newValue = curProperty.GetValue(currentState); object oldValue = curProperty.GetValue(oldState); if (newValue == null || oldValue == null) { continue; } ListCompareAttribute compareAttribute = curProperty.GetCustomAttribute <ListCompareAttribute>(); if ((newValue != null && oldValue == null) || (newValue == null && oldValue != null)) { compareResults.Add(BuildCompareDifference(curProperty.Name, newValue, oldValue, compareAttribute.LabelKey, string.Empty)); } else { // Compare lists IEnumerable newValueEnumerable = (IEnumerable)newValue; IEnumerable oldValueEnumerable = (IEnumerable)oldValue; if (curProperty.PropertyType.GenericTypeArguments.Any() && typeof(IImplementationListComparable).IsAssignableFrom(curProperty.PropertyType.GenericTypeArguments[0])) { CompareDifference difference = BuildCompareDifference(curProperty.Name, null, null, compareAttribute.LabelKey, string.Empty); difference.SubDifferences.AddRange(CompareImplementationComparableList(newValueEnumerable, oldValueEnumerable)); if (difference.SubDifferences.Count > 0) { compareResults.Add(difference); } } } } return(compareResults); }
/// <summary> /// Compares the state machines of objects /// </summary> /// <param name="relatedObjectId">Id of the related object</param> /// <param name="result">Result to fill</param> /// <returns>Task</returns> private async Task CompareStateMachines(string relatedObjectId, CompareResult result) { StateMachine stateMachine = await _stateMachineDbAccess.GetStateMachineByRelatedObjectId(relatedObjectId); if (stateMachine == null) { return; } CompareResult stateMachineResult = await CompareStateMachine(stateMachine.Id, stateMachine); if (stateMachineResult.CompareDifference.Count > 0 || !stateMachineResult.DoesSnapshotExist) { CompareDifference stateMachineDiff = BuildCompareDifference("StateMachineChanged", null, null, "StateMachineChanged", string.Empty); stateMachineDiff.SubDifferences.AddRange(stateMachineResult.CompareDifference); if (!stateMachineResult.DoesSnapshotExist) { stateMachineDiff.SubDifferences.Add(BuildCompareDifference("StateMachineChangedWasNeverImplemented", null, null, "StateMachineChangedWasNeverImplemented", "EmptyText")); } result.CompareDifference.Add(stateMachineDiff); } }
/// <summary> /// Compares two lists of Implementation comparables /// </summary> /// <param name="newValueEnumerable">New Value List</param> /// <param name="oldValueEnumerable">Old Value List</param> /// <returns>Compare Results</returns> private List <CompareDifference> CompareImplementationComparableList(IEnumerable newValueEnumerable, IEnumerable oldValueEnumerable) { List <CompareDifference> compareResults = new List <CompareDifference>(); List <IImplementationListComparable> newComparables = newValueEnumerable.Cast <IImplementationListComparable>().ToList(); List <IImplementationListComparable> oldComparables = oldValueEnumerable.Cast <IImplementationListComparable>().ToList(); Dictionary <string, IImplementationListComparable> newObjectsDictionary = newComparables.ToDictionary(c => c.ListComparableId); Dictionary <string, IImplementationListComparable> oldObjectsDictionary = oldComparables.ToDictionary(c => c.ListComparableId); // Get deleted Objects List <IImplementationListComparable> deletedObjects = oldComparables.Where(c => !newObjectsDictionary.ContainsKey(c.ListComparableId)).ToList(); compareResults.AddRange(deletedObjects.Select(l => BuildCompareDifference(null, null, l.ListComparableValue, string.Empty, string.Empty))); // Get new Objects List <IImplementationListComparable> newObjects = newComparables.Where(c => !oldObjectsDictionary.ContainsKey(c.ListComparableId)).ToList(); compareResults.AddRange(newObjects.Select(l => BuildCompareDifference(null, l.ListComparableValue, null, string.Empty, string.Empty))); // Compare objects that exist in both List <IImplementationListComparable> sameObjects = newComparables.Where(c => oldObjectsDictionary.ContainsKey(c.ListComparableId)).ToList(); foreach (IImplementationListComparable curNewObject in sameObjects) { IImplementationListComparable curOldObject = oldObjectsDictionary[curNewObject.ListComparableId]; CompareResult subCompareResult = CompareObjects(curNewObject, curOldObject); if (subCompareResult.CompareDifference != null && subCompareResult.CompareDifference.Any()) { CompareDifference compareDifference = BuildCompareDifference(curNewObject.ListComparableValue, null, null, string.Empty, string.Empty); compareDifference.SubDifferences = subCompareResult.CompareDifference; compareResults.Add(compareDifference); } } return(compareResults); }