using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using Gestor.Model.Attributes; namespace Gestor.Model.Domain.Generic; public abstract class DomainBase : ICloneable { private List _originalValues = new List(); public long Id { get; set; } public void Initialize() { _originalValues = GetProperties(); } public List GetProperties(object obj) { return (from x in obj.GetType().GetProperties() where !HasValidate(x.GetValue(obj)) && GetLogAttribute(x).GetValueOrDefault() select x into key select new FieldInformation { Name = key.Name, Description = GetDescriptionAttribute(key), Value = key.GetValue(obj) }).ToList(); } public List GetProperties() { List dictionary = new List(); GetType().GetProperties().ToList().ForEach(delegate(PropertyInfo x) { if (!(x.Name == "ValidationEvent")) { bool? logAttribute = GetLogAttribute(x); if (!logAttribute.HasValue || logAttribute != false) { object value = x.GetValue(this); if (!HasValidate(value)) { FieldInformation property = GetProperty(x, value, logAttribute); if (property != null) { dictionary.Add(property); } } else { dictionary.AddRange(GetPropertiesChield(value, GetDescriptionAttribute(x), logAttribute, defaultInfo: true)); } } } }); return dictionary; } public FieldInformation GetProperty(PropertyInfo property, dynamic value, bool? log, string description = null) { if (!log.HasValue || log == false) { return null; } return new FieldInformation { Name = property.Name, Description = (description ?? GetDescriptionAttribute(property)), Value = value }; } private List GetPropertiesChield(dynamic obj, string description, bool? log, bool defaultInfo = false) { List list = new List(); if (obj == null) { return list; } dynamic properties = obj.GetType().GetProperties(); foreach (dynamic item in properties) { if ((item.Name == "ValidationEvent") || ((defaultInfo && !GetDefaultAttribute(item)) ? true : false)) { continue; } log = GetLogAttribute(item); if (log.HasValue && log == false) { continue; } dynamic value = item.GetValue(obj); if (value != null && HasValidate(value)) { list.AddRange(GetPropertiesChield(value, GetDescriptionAttribute(item), log, true)); continue; } dynamic property = GetProperty(item, value, log, description); if (property != null) { list.Add(property); } } return list; } private bool? GetLogAttribute(PropertyInfo info) { object[] customAttributes = info.GetCustomAttributes(typeof(LogAttribute), inherit: true); if (customAttributes.Length == 0) { return null; } return ((LogAttribute)customAttributes[0]).Description; } private bool GetDefaultAttribute(PropertyInfo info) { return info.GetCustomAttributes(typeof(NameAttribute), inherit: true).Length != 0; } private string GetDescriptionAttribute(PropertyInfo info) { if (info == null) { return ""; } object[] customAttributes = info.GetCustomAttributes(typeof(DescriptionAttribute), inherit: true); if (customAttributes.Length == 0) { return info.Name.ToUpper(); } return ((DescriptionAttribute)customAttributes[0]).Description; } public bool HasChange() { return !GetProperties().All(_originalValues.Contains); } public bool HasChange(string property) { IEnumerable @object = _originalValues.Where((FieldInformation x) => x.Name == property); return !GetProperties().All(@object.Contains); } public object Clone() { return MemberwiseClone(); } public static void Copy(TU dest, T source) { List list = (from x in typeof(T).GetProperties() where x.CanRead select x).ToList(); List source2 = (from x in typeof(TU).GetProperties() where x.CanWrite select x).ToList(); foreach (PropertyInfo sourceProp in list) { if (source2.Any((PropertyInfo x) => x.Name == sourceProp.Name)) { source2.First((PropertyInfo x) => x.Name == sourceProp.Name).SetValue(dest, sourceProp.GetValue(source, null), null); } } } public List GetValorOriginal() { return (from x in GetProperties() select new ValorOriginal { Campo = x.Name, Descricao = x.Description, ValorAtual = ((!((x.Value != null) ? true : false)) ? null : ((!(x.Value.GetType() is DateTime)) ? x.Value?.ToString() : x.Value?.ToString("G"))) }).ToList(); } public List Compare() { List current = GetProperties(); List difference = new List(); PropertyInfo[] properties = GetType().GetProperties(); _originalValues.ToList().ForEach(delegate(FieldInformation x) { FieldInformation fieldInformation = current.FirstOrDefault((FieldInformation k) => k.Description == x.Description); if (!((fieldInformation?.Value == null && x.Value == null) ? true : false)) { PropertyInfo propertyInfo = properties.FirstOrDefault((PropertyInfo p) => p.Name == x.Name); bool flag = propertyInfo != null && propertyInfo.GetCustomAttributes(typeof(ForceLogAttribute), inherit: true).Length != 0; if (fieldInformation?.Value == null && x.Value != null && !string.IsNullOrWhiteSpace(x.Value.ToString())) { difference.Add(new Diferenca { Campo = x.Name, Descricao = x.Description, ValorAnterior = x.Value.ToString(), ValorAtual = null }); } else if (fieldInformation.Value != null && x.Value == null && !string.IsNullOrWhiteSpace(fieldInformation.Value.ToString())) { difference.Add(new Diferenca { Campo = x.Name, Descricao = x.Description, ValorAnterior = null, ValorAtual = fieldInformation.Value.ToString() }); } else if (!((fieldInformation.Value.ToString() == x.Value.ToString() && !flag) ? true : false)) { difference.Add((x.Value.GetType() is DateTime) ? new Diferenca { Campo = x.Name, Descricao = x.Description, ValorAnterior = x.Value?.ToString("G"), ValorAtual = fieldInformation.Value?.ToString("G") } : new Diferenca { Campo = x.Name, Descricao = x.Description, ValorAnterior = x.Value?.ToString(), ValorAtual = fieldInformation.Value?.ToString() }); } } }); return difference; } public List Compare(List valoresOriginais) { List current = GetProperties(); List difference = new List(); PropertyInfo[] properties = GetType().GetProperties(); valoresOriginais.ForEach(delegate(ValorOriginal x) { FieldInformation fieldInformation = current.First((FieldInformation k) => k.Name == x.Campo); if (!((fieldInformation.Value == null && x.ValorAtual == null) ? true : false)) { PropertyInfo propertyInfo = properties.FirstOrDefault((PropertyInfo p) => p.Name == x.Campo); bool flag = propertyInfo != null && propertyInfo.GetCustomAttributes(typeof(ForceLogAttribute), inherit: true).Length != 0; if (fieldInformation.Value == null && x.ValorAtual != null && !string.IsNullOrWhiteSpace(x.ValorAtual)) { difference.Add(new Diferenca { Campo = x.Campo, Descricao = fieldInformation.Description, ValorAnterior = x.ValorAtual, ValorAtual = null }); } else if (fieldInformation.Value != null && x.ValorAtual == null && !string.IsNullOrWhiteSpace(fieldInformation.Value.ToString())) { difference.Add(new Diferenca { Campo = x.Campo, Descricao = fieldInformation.Description, ValorAnterior = null, ValorAtual = fieldInformation.Value.ToString() }); } else if (!((fieldInformation.Value != null && fieldInformation.Value.ToString() == x.ValorAtual && !flag) ? true : false)) { difference.Add((fieldInformation.Value?.GetType() is DateTime) ? new Diferenca { Campo = x.Campo, Descricao = x.Descricao, ValorAnterior = x.ValorAtual, ValorAtual = fieldInformation.Value?.ToString("G") } : new Diferenca { Campo = x.Campo, Descricao = x.Descricao, ValorAnterior = x.ValorAtual?.ToString(), ValorAtual = fieldInformation.Value?.ToString() }); } } }); return difference; } public bool HasValidate(object objectToCheck) { if (objectToCheck == null) { return false; } return objectToCheck.GetType().GetProperty("ValidationEvent") != null; } public bool HasProperty(object objectToCheck, string propertyName) { if (objectToCheck == null) { return false; } return objectToCheck.GetType().GetProperty(propertyName) != null; } }