Пример #1
0
        private static void AddAuditEntities(DataContext dataContext, AuditLog auditLog, AuditAction action, IEnumerable <object> entities)
        {
            foreach (var entity in entities)
            {
                if (!HasAuditAttribute(entity))
                {
                    continue;
                }

                AuditEntity auditEntity = CreateAuditEntity(dataContext, entity, action);
                auditLog.Entities.Add(auditEntity);
            }
        }
Пример #2
0
 private static void MergeAuditEntity(AuditEntity source, AuditEntity destination)
 {
     //destination.Action = source.Action;  // merge action?
     foreach (var property in source.Properties)
     {
         if (destination.Properties.Contains(property.Name))
         {
             // only update the current property, keep the original
             destination.Properties[property.Name].Current = property.Current;
         }
         else
         {
             destination.Properties.Add(property);
         }
     }
 }
Пример #3
0
        private static void AddAuditKeys(MetaType metaType, object entity, AuditEntity auditEntity)
        {
            foreach (var dataMember in metaType.IdentityMembers)
            {
                var auditProperty = new AuditKey();
                try
                {
                    auditProperty.MetaDataMember = dataMember;
                    auditProperty.Name           = dataMember.Member.Name;
                    auditProperty.Type           = dataMember.Type.FullName;

                    object value = GetKeyValue(dataMember, entity);

                    auditProperty.Value = value;
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.Message);
                    auditProperty.Value = "{error}";
                }
                auditEntity.Keys.Add(auditProperty);
            }
        }
Пример #4
0
        private static AuditEntity CreateAuditEntity(DataContext dataContext, object entity, AuditAction action)
        {
            var metaType = dataContext.Mapping.GetMetaType(entity.GetType());
            var rootType = metaType.InheritanceRoot == null
                       ? metaType.Type
                       : metaType.InheritanceRoot.Type;

            var table = dataContext.GetTable(rootType);

            var auditEntity = new AuditEntity();

            auditEntity.Action  = action;
            auditEntity.Type    = metaType.Type.FullName;
            auditEntity.Current = entity;
            if (action != AuditAction.Insert)
            {
                auditEntity.Original = table.GetOriginalEntityState(entity);
            }

            AddAuditKeys(metaType, entity, auditEntity);
            AddAuditProperties(metaType, table, entity, auditEntity);

            return(auditEntity);
        }
Пример #5
0
        private static void AddAssociationProperties(MetaType metaType, ITable table, object entity, AuditEntity auditEntity)
        {
            foreach (var association in metaType.Associations)
            {
                if (association.IsMany || HasNotAuditedAttribute(association.ThisMember.Member))
                {
                    continue;
                }

                // keys can contain multiple columns.
                // if none of them are found in the change list, skip.
                bool foundMember = false;

                foreach (var keyMember in association.ThisKey)
                {
                    if (!auditEntity.Properties.Contains(keyMember.Name))
                    {
                        continue;
                    }

                    var property = auditEntity.Properties[keyMember.Name];
                    property.IsForeignKey = association.IsForeignKey;
                    foundMember           = true;
                }

                if (!foundMember)
                {
                    continue;
                }

                var auditProperty = new AuditProperty();

                try
                {
                    var thisMember = association.ThisMember;
                    auditProperty.Name          = thisMember.Name;
                    auditProperty.Type          = thisMember.Type.FullName;
                    auditProperty.IsAssociation = true;
                    auditProperty.ForeignKey    = string.Join(",", association.ThisKey.Select(k => k.Name).ToArray());

                    var displayMember = GetDisplayMember(association.OtherType);

                    //this will get the fkey entity with out causing a load
                    object currentChildEntity = thisMember.DeferredValueAccessor.GetBoxedValue(entity);
                    object currentValue       = GetAssociationValue(association, displayMember, currentChildEntity, table, entity);

                    //if there is nothing set for the fkey on insert and delete, skip
                    if (auditEntity.Action != AuditAction.Update && currentValue == null)
                    {
                        continue;
                    }

                    if (auditEntity.Action == AuditAction.Delete)
                    {
                        auditProperty.Original = currentValue;
                    }
                    else
                    {
                        auditProperty.Current = currentValue ?? _nullText;
                    }

                    if (auditEntity.Action == AuditAction.Update && table != null)
                    {
                        // get original value for updates
                        object original = table.GetOriginalEntityState(entity);
                        if (original != null)
                        {
                            //this will get the fkey entity with out causing a load
                            object originalChildEntity = thisMember.DeferredValueAccessor.GetBoxedValue(original);
                            auditProperty.Original = GetAssociationValue(association, displayMember, originalChildEntity, table, original) ?? _nullText;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.Message);
                    if (auditEntity.Action == AuditAction.Delete)
                    {
                        auditProperty.Original = "{error}";
                    }
                    else
                    {
                        auditProperty.Current = "{error}";
                    }
                }

                auditEntity.Properties.Add(auditProperty);
            } // foreach
        }
Пример #6
0
        private static AuditProperty CreateAuditProperty(MetaDataMember dataMember, ModifiedMemberInfo modifiedMemberInfo, object entity, AuditEntity auditEntity)
        {
            var auditProperty = new AuditProperty();

            try
            {
                auditProperty.MetaDataMember = dataMember;
                auditProperty.Name           = dataMember.Member.Name;

                Type underlyingType = GetUnderlyingType(dataMember.Type);
                auditProperty.Type = underlyingType.FullName;

                if (auditProperty.Type == _binaryType || dataMember.IsDeferred)
                {
                    return(auditProperty);
                }

                if (auditEntity.Action == AuditAction.Update && modifiedMemberInfo.Member != null)
                {
                    auditProperty.Current  = GetValue(modifiedMemberInfo.Member, underlyingType, modifiedMemberInfo.CurrentValue, entity);
                    auditProperty.Original = GetValue(modifiedMemberInfo.Member, underlyingType, modifiedMemberInfo.OriginalValue, entity);
                    return(auditProperty);
                }

                var value = GetValue(dataMember.Member, underlyingType, dataMember.MemberAccessor.GetBoxedValue(entity), entity);
                if (value == null)
                {
                    return(null); // ignore null properties?
                }
                if (auditEntity.Action == AuditAction.Delete)
                {
                    auditProperty.Original = value;
                }
                else
                {
                    auditProperty.Current = value;
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                if (auditEntity.Action == AuditAction.Delete)
                {
                    auditProperty.Original = "{error}";
                }
                else
                {
                    auditProperty.Current = "{error}";
                }
            }

            return(auditProperty);
        }
Пример #7
0
        private static void AddAuditProperties(MetaType metaType, ITable table, object entity, AuditEntity auditEntity)
        {
            var modifiedMembers = table.GetModifiedMembers(entity);

            foreach (var dataMember in metaType.PersistentDataMembers)
            {
                if (dataMember.IsVersion || dataMember.IsAssociation || HasNotAuditedAttribute(dataMember.Member))
                {
                    continue;
                }

                var memberInfo         = dataMember.Member;
                var modifiedMemberInfo = modifiedMembers.FirstOrDefault(m => m.Member == memberInfo);

                if (auditEntity.Action == AuditAction.Update && modifiedMemberInfo.Member == null && !HasAlwaysAuditAttribute(dataMember.Member))
                {
                    continue; // this means the property was not changed, skip it
                }
                var auditProperty = CreateAuditProperty(dataMember, modifiedMemberInfo, entity, auditEntity);
                if (auditProperty != null)
                {
                    auditEntity.Properties.Add(auditProperty);
                }
            }

            AddAssociationProperties(metaType, table, entity, auditEntity);
        }