diff options
| author | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
|---|---|---|
| committer | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
| commit | 00a491d93733d4625ad329b2ba8237f445364b3f (patch) | |
| tree | 4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/Tango.BL/ActionLogs | |
| parent | 124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff) | |
| download | Tango-00a491d9.tar.gz Tango-00a491d9.zip | |
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.BL/ActionLogs')
6 files changed, 0 insertions, 662 deletions
diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/ActionLogIgnoreAttribute.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/ActionLogIgnoreAttribute.cs deleted file mode 100644 index 1a1001942..000000000 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/ActionLogIgnoreAttribute.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.BL.ActionLogs -{ - [AttributeUsage(AttributeTargets.Property)] - public class ActionLogIgnoreAttribute : Attribute - { - } -} diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogComparer.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogComparer.cs deleted file mode 100644 index d93b130c4..000000000 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogComparer.cs +++ /dev/null @@ -1,254 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using Tango.BL.ValueObjects; - -namespace Tango.BL.ActionLogs -{ - /// <summary> - /// Represents the default implementation of <see cref="IActionLogComparer"/>. - /// </summary> - public class DefaultActionLogComparer : IActionLogComparer - { - /// <summary> - /// Compares the specified object before and after changes and returns the difference tree. - /// </summary> - /// <param name="before">The object before the change.</param> - /// <param name="after">The object after the change.</param> - /// <returns></returns> - public Task<ActionLogDifference> Compare(IActionLogComparable before, IActionLogComparable after) - { - return Task.Factory.StartNew<ActionLogDifference>(() => - { - ActionLogDifference diff = new ActionLogDifference(); - diff.Name = GetComponentName(before, after); - - Compare(diff, before, after, null); - RemoveNoDifferences(diff); - - return diff; - }); - } - - #region Helpers - - private void Compare(ActionLogDifference diff, Object before, Object after, HashSet<Object> scannedObjects) - { - if (scannedObjects == null) - { - scannedObjects = new HashSet<object>(); - } - - if (before == null && after == null) return; - - if (before != null) - { - if (scannedObjects.Contains(before)) return; - scannedObjects.Add(before); - } - - if (after != null) - { - if (scannedObjects.Contains(after)) return; - scannedObjects.Add(after); - } - - foreach (var prop in GetProperties(before, after).OrderByDescending(x => x.PropertyType.IsValueTypeOrString())) - { - if (prop.PropertyType == typeof(byte[]) || GetShouldIgnore(prop, before, after)) continue; - - if (prop.PropertyType.IsValueTypeOrString()) - { - object beforeValue = null; - object afterValue = null; - - if (before != null) - { - beforeValue = prop.GetValue(before); - } - - if (after != null) - { - afterValue = prop.GetValue(after); - } - - 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)) - { - AddValueDiff(diff, prop.Name, beforeValue, afterValue); - } - } - } - else if (!prop.PropertyType.IsGenericType) - { - object beforePropInstance = null; - object afterPropInstance = null; - - if (before != null) - { - beforePropInstance = prop.GetValue(before); - } - - if (after != null) - { - afterPropInstance = prop.GetValue(after); - } - - Compare(AddChildDiff(diff, prop.Name), beforePropInstance, afterPropInstance, scannedObjects); - } - else - { - IList beforeCollection = null; - IList afterCollection = null; - ActionLogDifference listDiff = new ActionLogDifference() - { - Name = prop.Name - }; - - if (before != null) - { - beforeCollection = prop.GetValue(before) as IList; - } - - if (after != null) - { - afterCollection = prop.GetValue(after) as IList; - } - - int listCount = 0; - - if (beforeCollection != null && afterCollection == null) - { - for (int i = 0; i < beforeCollection.Count; i++) - { - listCount++; - Compare(AddChildDiff(listDiff, GetActionLogName(beforeCollection[i], prop.Name)), beforeCollection[i], null, scannedObjects); - } - } - else if (beforeCollection == null && afterCollection != null) - { - for (int i = 0; i < afterCollection.Count; i++) - { - listCount++; - Compare(AddChildDiff(listDiff, GetActionLogName(afterCollection[i], prop.Name)), null, afterCollection[i], scannedObjects); - } - } - - if (beforeCollection != null && afterCollection != null) - { - for (int i = 0; i < Math.Max(beforeCollection.Count, afterCollection.Count); i++) - { - var beforeItem = i < beforeCollection.Count ? beforeCollection[i] : null; - var afterItem = i < afterCollection.Count ? afterCollection[i] : null; - - listCount++; - Compare(AddChildDiff(listDiff, GetActionLogName(beforeItem, prop.Name)), beforeItem, afterItem, scannedObjects); - } - } - - if (listCount > 0) - { - diff.Children.Add(listDiff); - } - } - } - } - - private void AddValueDiff(ActionLogDifference diff, String property, object before, object after) - { - diff.Children.Add(new ActionLogDifferenceValue() - { - 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 void RemoveNoDifferences(ActionLogDifference diff) - { - foreach (var child in diff.Children.ToList()) - { - if (!child.HasDifference) - { - diff.Children.Remove(child); - } - else - { - RemoveNoDifferences(child); - } - } - } - - private String GetComponentName(Object before, Object after) - { - var afterCast = after as IActionLogComparable; - var beforeCast = before as IActionLogComparable; - - var name = afterCast != null ? afterCast.GetActionLogName() : beforeCast.GetActionLogName(); - - return name; - } - - private String GetActionLogName(Object obj, String defaultName) - { - var objCast = obj as IActionLogComparable; - if (objCast != null) - { - return objCast.GetActionLogName(); - } - else - { - return defaultName; - } - } - - private PropertyInfo GetProperty(String name, Object before, Object after) - { - return after != null ? after.GetType().GetProperty(name) : before.GetType().GetProperty(name); - } - - private List<PropertyInfo> GetProperties(BindingFlags flags, Object before, Object after) - { - return after != null ? after.GetType().GetProperties(flags).ToList() : before.GetType().GetProperties(flags).ToList(); - } - - private List<PropertyInfo> GetProperties(Object before, Object after) - { - return after != null ? after.GetType().GetProperties().ToList() : before.GetType().GetProperties().ToList(); - } - - private bool GetShouldIgnore(PropertyInfo prop, Object before, Object after) - { - if (prop.GetCustomAttribute<ActionLogIgnoreAttribute>() != null) - { - return true; - } - - var beforeCast = before as IActionLogComparable; - var afterCast = after as IActionLogComparable; - - if (beforeCast != null) return beforeCast.ShouldActionLogIgnore(prop.Name); - if (afterCast != null) return afterCast.ShouldActionLogIgnore(prop.Name); - - return false; - } - - #endregion - } -} diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogManager.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogManager.cs deleted file mode 100644 index 0f5d6bd51..000000000 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogManager.cs +++ /dev/null @@ -1,242 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using Tango.BL.Entities; -using Tango.BL.Enumerations; -using Tango.BL.ValueObjects; -using Tango.Core; -using Tango.Core.ExtensionMethods; - -namespace Tango.BL.ActionLogs -{ - /// <summary> - /// Represents the default <see cref="IActionLogManager"/> implementation. - /// </summary> - /// <seealso cref="Tango.Core.ExtendedObject" /> - /// <seealso cref="Tango.BL.ActionLogs.IActionLogManager" /> - public class DefaultActionLogManager : ExtendedObject, IActionLogManager - { - /// <summary> - /// Gets or sets the action log comparer used to compare related objects. - /// </summary> - public IActionLogComparer ActionLogComparer { get; set; } - - /// <summary> - /// Gets or sets a value indicating whether to perform the logging operations asynchronously. - /// </summary> - public bool IsAsync { get; set; } - - /// <summary> - /// Initializes a new instance of the <see cref="DefaultActionLogManager"/> class. - /// </summary> - public DefaultActionLogManager() - { - IsAsync = true; - ActionLogComparer = new DefaultActionLogComparer(); - } - - /// <summary> - /// Inserts a new action log (main entry point). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="userGuid">The user unique identifier.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObjectGuid">The related object unique identifier.</param> - /// <param name="diffference">The diffference node tree.</param> - /// <param name="message">The message.</param> - public void InsertLog(ActionLogType type, string userGuid, string relatedObjectName, string relatedObjectGuid, ActionLogDifference difference, string message = null) - { - Task task = new Task(() => - { - using (ObservablesContext db = ObservablesContext.CreateDefault()) - { - 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.SaveChanges(); - - LogManager.Log($"Action log '{type}' inserted."); - - 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 (IsAsync) - { - task.Start(); - } - else - { - task.RunSynchronously(); - } - } - - /// <summary> - /// Inserts a new action log (comparison entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="userGuid">The user unique identifier.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObjectBefore">The related object before the change.</param> - /// <param name="relatedObjectAfter">The related object after the change.</param> - /// <param name="message">The message.</param> - public void InsertLog(ActionLogType type, string userGuid, string relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, string message = null) - { - Task task = new Task(() => - { - ActionLogDifference diff = new ActionLogDifference(); - - bool insertWhenNoDifference = false; - - if (ActionLogComparer != null) - { - try - { - diff = ActionLogComparer.Compare(relatedObjectBefore, relatedObjectAfter).Result; - } - catch (Exception ex) - { - insertWhenNoDifference = true; - LogManager.Log(ex, $"Error while comparing for action log '{type}'."); - } - - if (diff.Children.Count > 0 || insertWhenNoDifference) - { - 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."); - } - } - else - { - Debug.WriteLine("No comparer defined for action log."); - InsertLog(type, userGuid, relatedObjectName, GetRelatedObjectGuid(relatedObjectBefore, relatedObjectAfter), diff, message); - } - }); - - if (IsAsync) - { - task.Start(); - } - else - { - task.RunSynchronously(); - } - } - - /// <summary> - /// Inserts a new action log (comparison entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="user">The user.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObjectBefore">The related object before the change.</param> - /// <param name="relatedObjectAfter">The related object after the change.</param> - /// <param name="message">The message.</param> - public void InsertLog(ActionLogType type, User user, string relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, string message = null) - { - InsertLog(type, user.Guid, relatedObjectName, relatedObjectBefore, relatedObjectAfter, message); - } - - /// <summary> - /// Inserts a new action log (deletion/creation entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="user">The user.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObject">The related object.</param> - /// <param name="message">The message.</param> - /// <param name="serializeRelatedObject">if set to <c>true</c> will create a one way difference tree (use when the related object was deleted and needs to be monitored).</param> - public void InsertLog(ActionLogType type, User user, string relatedObjectName, IActionLogComparable relatedObject, string message = null, bool serializeRelatedObject = false) - { - if (serializeRelatedObject) - { - InsertLog(type, user.Guid, relatedObjectName, relatedObject, null, message); - } - else - { - InsertLog(type, user.Guid, relatedObjectName, relatedObject?.Guid, message); - } - } - - /// <summary> - /// Inserts a new action log (creation entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="userGuid">The user unique identifier.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObjectGuid">The related object unique identifier.</param> - /// <param name="message">The message.</param> - public void InsertLog(ActionLogType type, string userGuid, string relatedObjectName, string relatedObjectGuid, string message = null) - { - InsertLog(type, userGuid, relatedObjectName, relatedObjectGuid, null, message); - } - - /// <summary> - /// Inserts a new action log (nutral entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="userGuid">The user unique identifier.</param> - /// <param name="message">The message.</param> - public void InsertLog(ActionLogType type, string userGuid, string message = null) - { - InsertLog(type, userGuid, null, null, message); - } - - /// <summary> - /// Inserts a new action log (nutral entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="user">The user.</param> - /// <param name="message">The message.</param> - public void InsertLog(ActionLogType type, User user, string message = null) - { - InsertLog(type, user.Guid, message); - } - - /// <summary> - /// Gets the related object unique identifier. - /// </summary> - /// <param name="before">The before.</param> - /// <param name="after">The after.</param> - /// <returns></returns> - private String GetRelatedObjectGuid(IActionLogComparable before, IActionLogComparable after) - { - if (before != null) - { - return before.Guid; - } - - if (after != null) - { - return after.Guid; - } - - return null; - } - } -} diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparable.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparable.cs deleted file mode 100644 index 255f5a407..000000000 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparable.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.BL.ValueObjects; - -namespace Tango.BL.ActionLogs -{ - /// <summary> - /// Represents a type which is supported for ActionLog difference comparison. - /// </summary> - public interface IActionLogComparable - { - /// <summary> - /// Gets the unique identifier of the object. - /// </summary> - String Guid { get; } - - /// <summary> - /// Returns true if the specified property should be ignored during ActionLog comparison. - /// </summary> - /// <param name="propName">Name of the property.</param> - /// <returns></returns> - bool ShouldActionLogIgnore(String propName); - - /// <summary> - /// Returns an optional custom name for this instance in the comparison tree. - /// </summary> - /// <returns></returns> - String GetActionLogName(); - } -} diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparer.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparer.cs deleted file mode 100644 index 94db259b0..000000000 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogComparer.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.BL.ValueObjects; - -namespace Tango.BL.ActionLogs -{ - /// <summary> - /// Represents an <see cref="IActionLogComparable"/> comparer. - /// </summary> - public interface IActionLogComparer - { - /// <summary> - /// Compares the specified object before and after changes and returns the difference tree. - /// </summary> - /// <param name="before">The object before the change.</param> - /// <param name="after">The object after the change.</param> - /// <returns></returns> - Task<ActionLogDifference> Compare(IActionLogComparable before, IActionLogComparable after); - } -} diff --git a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogManager.cs b/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogManager.cs deleted file mode 100644 index 5edb409e6..000000000 --- a/Software/Visual_Studio/Tango.BL/ActionLogs/IActionLogManager.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.BL.Entities; -using Tango.BL.Enumerations; -using Tango.BL.ValueObjects; - -namespace Tango.BL.ActionLogs -{ - /// <summary> - /// Represents an ActionLog manager which is responsible for inserting and comparing new action logs to a data store. - /// </summary> - public interface IActionLogManager - { - /// <summary> - /// Gets or sets the action log comparer used to compare related objects. - /// </summary> - IActionLogComparer ActionLogComparer { get; set; } - - /// <summary> - /// Gets or sets a value indicating whether to perform the logging operations asynchronously. - /// </summary> - bool IsAsync { get; set; } - - /// <summary> - /// Inserts a new action log (main entry point). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="userGuid">The user unique identifier.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObjectGuid">The related object unique identifier.</param> - /// <param name="diffference">The diffference node tree.</param> - /// <param name="message">The message.</param> - void InsertLog(ActionLogType type, String userGuid, String relatedObjectName, String relatedObjectGuid, ActionLogDifference diffference, String message = null); - - /// <summary> - /// Inserts a new action log (comparison entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="userGuid">The user unique identifier.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObjectBefore">The related object before the change.</param> - /// <param name="relatedObjectAfter">The related object after the change.</param> - /// <param name="message">The message.</param> - void InsertLog(ActionLogType type, String userGuid, String relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, String message = null); - - /// <summary> - /// Inserts a new action log (comparison entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="user">The user.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObjectBefore">The related object before the change.</param> - /// <param name="relatedObjectAfter">The related object after the change.</param> - /// <param name="message">The message.</param> - void InsertLog(ActionLogType type, User user, String relatedObjectName, IActionLogComparable relatedObjectBefore, IActionLogComparable relatedObjectAfter, String message = null); - - /// <summary> - /// Inserts a new action log (deletion/creation entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="user">The user.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObject">The related object.</param> - /// <param name="message">The message.</param> - /// <param name="serializeRelatedObject">if set to <c>true</c> will create a one way difference tree (use when the related object was deleted and needs to be monitored).</param> - void InsertLog(ActionLogType type, User user, String relatedObjectName, IActionLogComparable relatedObject, String message = null, bool serializeRelatedObject = false); - - /// <summary> - /// Inserts a new action log (creation entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="userGuid">The user unique identifier.</param> - /// <param name="relatedObjectName">Name of the related object.</param> - /// <param name="relatedObjectGuid">The related object unique identifier.</param> - /// <param name="message">The message.</param> - void InsertLog(ActionLogType type, String userGuid, String relatedObjectName, String relatedObjectGuid, String message = null); - - /// <summary> - /// Inserts a new action log (neutral entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="userGuid">The user unique identifier.</param> - /// <param name="message">The message.</param> - void InsertLog(ActionLogType type, String userGuid, String message = null); - - /// <summary> - /// Inserts a new action log (neutral entry). - /// </summary> - /// <param name="type">The type.</param> - /// <param name="user">The user.</param> - /// <param name="message">The message.</param> - void InsertLog(ActionLogType type, User user, String message = null); - } -} |
