diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-12-18 15:52:25 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-12-18 15:52:25 +0200 |
| commit | 41b950b3f3f7fa0384cccd13ed4ef38119bbcbcf (patch) | |
| tree | f6c535ae38fcf71842f0af4622c806044224974d /Software/Visual_Studio/Tango.BL/ActionLogs | |
| parent | 615bc666c8d0618d93bc4401a74928535c2cc7f6 (diff) | |
| download | Tango-41b950b3f3f7fa0384cccd13ed4ef38119bbcbcf.tar.gz Tango-41b950b3f3f7fa0384cccd13ed4ef38119bbcbcf.zip | |
Fixed ContinueThreadLoadingResponse.
Refactored ActionLog Difference data structure to node tree.
Diffstat (limited to 'Software/Visual_Studio/Tango.BL/ActionLogs')
4 files changed, 140 insertions, 94 deletions
diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/BasicActionLogComparer.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/BasicActionLogComparer.cs index 61fc373a8..af52bf394 100644 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/BasicActionLogComparer.cs +++ b/Software/Visual_Studio/Tango.BL/ActionLogs/BasicActionLogComparer.cs @@ -11,19 +11,36 @@ namespace Tango.BL.ActionLogs { public class BasicActionLogComparer { - public Task<List<ActionLogDifference>> Compare(IActionLogComparable before, IActionLogComparable after) + public Task<ActionLogDifference> Compare(IActionLogComparable before, IActionLogComparable after) { - return Task.Factory.StartNew<List<ActionLogDifference>>(() => + return Task.Factory.StartNew<ActionLogDifference>(() => { - List<ActionLogDifference> diffs = new List<ActionLogDifference>(); + ActionLogDifference diff = new ActionLogDifference(); + diff.Name = GetComponentName(before, after); - Compare(diffs, before, after, null); + Compare(diff, before, after, null); + RemoveNoDifferences(diff); - return diffs; + return diff; }); } - private void Compare(List<ActionLogDifference> diffs, Object before, Object after, HashSet<Object> scannedObjects) + private void RemoveNoDifferences(ActionLogDifference diff) + { + foreach (var child in diff.Children.ToList()) + { + if (!child.HasDifference) + { + diff.Children.Remove(child); + } + else + { + RemoveNoDifferences(child); + } + } + } + + private void Compare(ActionLogDifference diff, Object before, Object after, HashSet<Object> scannedObjects) { if (scannedObjects == null) { @@ -46,7 +63,7 @@ namespace Tango.BL.ActionLogs String component = GetComponentName(before, after); - foreach (var prop in GetProperties(before, after)) + foreach (var prop in GetProperties(before, after).OrderByDescending(x => x.PropertyType.IsPrimitive || x.PropertyType.IsValueType || x.PropertyType == typeof(String))) { if (GetShouldIgnore(prop, before, after)) continue; @@ -65,14 +82,14 @@ namespace Tango.BL.ActionLogs afterValue = prop.GetValue(after); } - if (afterValue == null && beforeValue != null) AddDiff(diffs, component, prop.Name, beforeValue, afterValue); - if (afterValue != null && beforeValue == null) AddDiff(diffs, component, prop.Name, beforeValue, afterValue); + if (afterValue == null && beforeValue != null) AddValueDiff(diff, prop.Name, beforeValue, afterValue); + if (afterValue != null && beforeValue == null) AddValueDiff(diff, prop.Name, beforeValue, afterValue); if (afterValue != null && beforeValue != null) { if (!afterValue.Equals(beforeValue)) { - AddDiff(diffs, component, prop.Name, beforeValue, afterValue); + AddValueDiff(diff, prop.Name, beforeValue, afterValue); } } } @@ -91,7 +108,7 @@ namespace Tango.BL.ActionLogs afterPropInstance = prop.GetValue(after); } - Compare(diffs, beforePropInstance, afterPropInstance, scannedObjects); + Compare(AddChildDiff(diff, prop.Name), beforePropInstance, afterPropInstance, scannedObjects); } else { @@ -108,18 +125,18 @@ namespace Tango.BL.ActionLogs afterCollection = prop.GetValue(after) as IList; } - if (beforeCollection != null && beforeCollection == null) + if (beforeCollection != null && afterCollection == null) { for (int i = 0; i < beforeCollection.Count; i++) { - Compare(diffs, beforeCollection[i], null, scannedObjects); + Compare(AddChildDiff(diff, GetActionLogName(beforeCollection[i],prop.Name)), beforeCollection[i], null, scannedObjects); } } else if (beforeCollection == null && afterCollection != null) { for (int i = 0; i < afterCollection.Count; i++) { - Compare(diffs, null, afterCollection[i], scannedObjects); + Compare(AddChildDiff(diff, GetActionLogName(afterCollection[i], prop.Name)), null, afterCollection[i], scannedObjects); } } @@ -129,27 +146,52 @@ namespace Tango.BL.ActionLogs { var beforeItem = i < beforeCollection.Count ? beforeCollection[i] : null; var afterItem = i < afterCollection.Count ? afterCollection[i] : null; - Compare(diffs, beforeItem, afterItem, scannedObjects); + Compare(AddChildDiff(diff, GetActionLogName(beforeItem, prop.Name)), beforeItem, afterItem, scannedObjects); } } } } } - private void AddDiff(List<ActionLogDifference> diffs, String component, String property, object before, object after) + private void AddValueDiff(ActionLogDifference diff, String property, object before, object after) { - diffs.Add(new ActionLogDifference() + diff.Children.Add(new ActionLogDifferenceValue() { - Component = component, - Property = property, + Name = property, Before = before, After = after }); } + private ActionLogDifference AddChildDiff(ActionLogDifference diff, String property) + { + ActionLogDifference childDiff = new ActionLogDifference(); + childDiff.Name = property; + diff.Children.Add(childDiff); + return childDiff; + } + private String GetComponentName(Object before, Object after) { - return after != null ? after.GetType().Name : before.GetType().Name; + var afterCast = after as IActionLogComparable; + var beforeCast = before as IActionLogComparable; + + var name = afterCast != null ? afterCast.GetActionName() : beforeCast.GetActionName(); + + return name; + } + + private String GetActionLogName(Object obj, String defaultName) + { + var objCast = obj as IActionLogComparable; + if (objCast != null) + { + return objCast.GetActionName(); + } + else + { + return defaultName; + } } private PropertyInfo GetProperty(String name, Object before, Object after) diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogManager.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogManager.cs index 23562bee0..9d632bccc 100644 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogManager.cs +++ b/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogManager.cs @@ -14,109 +14,113 @@ namespace Tango.BL.ActionLogs { public class DefaultActionLogManager : ExtendedObject, IActionLogManager { - public bool ThrowExceptions { get; set; } - - public DefaultActionLogManager() + public void InsertLog(ActionLogType type, string userGuid, string relatedObjectName, string relatedObjectGuid, ActionLogDifference difference, string message = null) { - ThrowExceptions = true; - } - - public async Task InsertLog(ActionLogType type, string userGuid, string relatedObjectName, string relatedObjectGuid, List<ActionLogDifference> differences, string message = null) - { - using (ObservablesContext db = ObservablesContext.CreateDefault()) + Task.Factory.StartNew(() => { - try + using (ObservablesContext db = ObservablesContext.CreateDefault()) { - ActionLog log = new ActionLog(); - log.ActionType = type; - log.LastUpdated = DateTime.UtcNow; - log.UserGuid = userGuid; - log.RelatedObjectName = relatedObjectName; - log.RelatedObjectGuid = relatedObjectGuid; - log.Message = message; - log.Differences = differences; + try + { + ActionLog log = new ActionLog(); + log.ActionType = type; + log.LastUpdated = DateTime.UtcNow; + log.UserGuid = userGuid; + log.RelatedObjectName = relatedObjectName; + log.RelatedObjectGuid = relatedObjectGuid; + log.Message = message; + log.DifferenceObject = difference; - db.ActionLogs.Add(log); + db.ActionLogs.Add(log); - await db.SaveChangesAsync(); + db.SaveChanges(); - LogManager.Log($"Action log '{type}' inserted."); + LogManager.Log($"Action log '{type}' inserted."); - if (differences != null && differences.Count > 0) - { - Debug.WriteLine($"Action log differences:\n{differences.ToJsonString()}"); + if (difference != null && difference.Children.Count > 0) + { + Debug.WriteLine($"Action log differences:\n{difference.ToJsonString()}"); + } } - } - catch (Exception ex) - { - LogManager.Log(ex, $"Error inserting action log '{type}'."); - - if (ThrowExceptions) + catch (Exception ex) { - throw ex; + LogManager.Log(ex, $"Error inserting action log '{type}'."); } } - } + }); } - public async Task InsertLog(ActionLogType type, string userGuid, string relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, string message = null) + public void InsertLog(ActionLogType type, string userGuid, string relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, string message = null) { - List<ActionLogDifference> diffs = null; - - try + Task.Factory.StartNew(() => { - if (relatedObjectAfter != null) + ActionLogDifference diff = null; + + try { - diffs = await relatedObjectAfter.CompareTo(relatedObjectBefore); + diff = new BasicActionLogComparer().Compare(relatedObjectBefore, relatedObjectAfter).Result; } - else + catch (Exception ex) { - diffs = await relatedObjectBefore.CompareTo(relatedObjectAfter); + LogManager.Log(ex, $"Error while comparing for action log '{type}'."); } - } - catch (Exception ex) - { - LogManager.Log(ex, $"Error while comparing for action log '{type}'."); - if (ThrowExceptions) + if (diff.Children.Count > 0) { - throw ex; + InsertLog(type, userGuid, relatedObjectName, GetRelatedObjectGuid(relatedObjectBefore, relatedObjectAfter), diff, message); } - } + else + { + LogManager.Log($"Action log '{type}' was about to be inserted but skipped due to no differences."); + } + }); + } + + public void InsertLog(ActionLogType type, User user, string relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, string message = null) + { + InsertLog(type, user.Guid, relatedObjectName, relatedObjectBefore, relatedObjectAfter, message); + } + + public void InsertLog(ActionLogType type, string userGuid, string relatedObjectName, string relatedObjectGuid, string message = null) + { + InsertLog(type, userGuid, relatedObjectName, relatedObjectGuid, null, message); + } - if (diffs.Count > 0) + public void InsertLog(ActionLogType type, User user, string relatedObjectName, IActionLogComparable relatedObject, string message = null, bool serializeRelatedObject = false) + { + if (serializeRelatedObject) { - await InsertLog(type, userGuid, relatedObjectName, relatedObjectAfter.Guid, diffs, message); + InsertLog(type, user.Guid, relatedObjectName, relatedObject, null, message); } else { - LogManager.Log($"Action log '{type}' was about to be inserted but skipped due to no differences."); + InsertLog(type, user.Guid, relatedObjectName, relatedObject?.Guid, message); } } - public Task InsertLog(ActionLogType type, User user, string relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, string message = null) + public void InsertLog(ActionLogType type, string userGuid, string message = null) { - return InsertLog(type, user.Guid, relatedObjectName, relatedObjectBefore, relatedObjectAfter, message); + InsertLog(type, userGuid, null, null, message); } - public Task InsertLog(ActionLogType type, string userGuid, string relatedObjectName, string relatedObjectGuid, string message = null) + public void InsertLog(ActionLogType type, User user, string message = null) { - return InsertLog(type, userGuid, relatedObjectName, relatedObjectGuid, null, message); + InsertLog(type, user.Guid, message); } - public Task InsertLog(ActionLogType type, User user, string relatedObjectName, IObservableEntity relatedObject, string message = null) + private String GetRelatedObjectGuid(IActionLogComparable before, IActionLogComparable after) { - return InsertLog(type, user.Guid, relatedObjectName, relatedObject?.Guid, message); - } + if (before != null) + { + return before.Guid; + } - public Task InsertLog(ActionLogType type, string userGuid, string message = null) - { - return InsertLog(type, userGuid, null, null, message); - } + if (after != null) + { + return after.Guid; + } - public Task InsertLog(ActionLogType type, User user, string message = null) - { - return InsertLog(type, user.Guid, message); + return null; } } } diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparable.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparable.cs index de534141b..d98dd9122 100644 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparable.cs +++ b/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparable.cs @@ -10,12 +10,13 @@ namespace Tango.BL.ActionLogs public interface IActionLogComparable { String Guid { get; } - Task<List<ActionLogDifference>> CompareTo(IActionLogComparable before); + Task<ActionLogDifference> CompareTo(IActionLogComparable before); bool ShouldActionLogIgnore(String propName); + String GetActionName(); } public interface IActionLogComparable<T> : IActionLogComparable where T : class { - Task<List<ActionLogDifference>> CompareTo(T before); + Task<ActionLogDifference> CompareTo(T before); } } diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogManager.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogManager.cs index fd903bbba..36fb62ed4 100644 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogManager.cs +++ b/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogManager.cs @@ -11,13 +11,12 @@ namespace Tango.BL.ActionLogs { public interface IActionLogManager { - bool ThrowExceptions { get; set; } - Task InsertLog(ActionLogType type, String userGuid, String relatedObjectName, String relatedObjectGuid, List<ActionLogDifference> differences, String message = null); - Task InsertLog(ActionLogType type, String userGuid, String relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, String message = null); - Task InsertLog(ActionLogType type, User user, String relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, String message = null); - Task InsertLog(ActionLogType type, String userGuid, String relatedObjectName, String relatedObjectGuid, String message = null); - Task InsertLog(ActionLogType type, User user, String relatedObjectName, IObservableEntity relatedObject, String message = null); - Task InsertLog(ActionLogType type, String userGuid, String message = null); - Task InsertLog(ActionLogType type, User user, String message = null); + void InsertLog(ActionLogType type, String userGuid, String relatedObjectName, String relatedObjectGuid, ActionLogDifference diffference, String message = null); + void InsertLog(ActionLogType type, String userGuid, String relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, String message = null); + void InsertLog(ActionLogType type, User user, String relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, String message = null); + void InsertLog(ActionLogType type, String userGuid, String relatedObjectName, String relatedObjectGuid, String message = null); + void InsertLog(ActionLogType type, User user, String relatedObjectName, IActionLogComparable relatedObject, String message = null, bool serializeRelatedObject = false); + void InsertLog(ActionLogType type, String userGuid, String message = null); + void InsertLog(ActionLogType type, User user, String message = null); } } |
