aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ActionLogs/DefaultActionLogComparer.cs
blob: d93b130c4276df83b5ae77c6fdd292f9f2fd0a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
generated by cgit v1.3.1 (git 2.54.0) at 2026-08-02 01:48:29 +0000
 


8' href='#n208'>208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
    }
}