Beispiel #1
0
        private void PrepareChanges(IAuditContext ac)
        {
            foreach (var item in Changes.Where(x => x.State == EntityState.Added))
            {
                Type type = item.Entity.GetType();
                List<string> keyValues = new List<string>();
                foreach (var k in type.GetEntityProperties(true))
                {
                    var key = k.GetValue(item.Entity);

                    var pv = item.Values.FirstOrDefault(x => x.Name == k.Name);
                    if (pv == null)
                    {
                        pv = new CFieldValue { Name = k.Name };
                        item.Values.Add(pv);
                    }
                    pv.NewValue = key;

                    keyValues.Add(key.ToString());
                }

                item.Key = string.Join(",", keyValues);
            }

            List<CLinkValue> links = new List<CLinkValue>();
            foreach (var change in Changes)
            {
                foreach (var item in change.Values)
                {
                    foreach (var fk in item.Property.FKProperties)
                    {
                        CLinkValue v = new CLinkValue();
                        v.ObjectName = fk.EntityName;
                        var v1 = item.NewValue ?? item.OldValue;
                        if (v1 == null)
                            continue;
                        v.Key = v1.ToString();
                        if (string.IsNullOrWhiteSpace(v.Key))
                            continue;
                        v.ChildObject = item.Property.Property.DeclaringType.Name;
                        v.ChildKey = change.Key;
                        v.Operation = change.State.ToString();
                        links.Add(v);
                    }
                }
            }

            foreach (var item in links.GroupBy(x => x.ObjectName))
            {
                string name = item.Key;
                foreach (var k in item.GroupBy(x => x.Key).ToList())
                {
                    string key = k.Key;
                    Change c = Changes.FirstOrDefault(x => x.ObjectName == name && x.Key == key);
                    if (c != null)
                    {
                        c.Links.AddRange(k);
                    }
                    else
                    {
                        c = new Change(EntityState.Modified);
                        c.ObjectName = name;
                        c.Key = key;
                        c.Links.AddRange(k);
                        Changes.Add(c);
                    }
                }
            }


            JavaScriptSerializer sr = new JavaScriptSerializer();

            foreach (var change in Changes)
            {
                if (change.Entity is IAuditIgnore)
                    continue;
                IAuditItem item = ac.CreateNew();
                item.Action = change.State.ToString();
                item.AuditTime = DateTime.UtcNow;
                //item.IPAddress = ac.GetIPAddress();
                //item.Username = ac.GetUsername();
                item.TableKey = change.Key;
                item.TableName = change.ObjectName;
                item.Fields = sr.Serialize(change.Values);
                item.Links = sr.Serialize(change.Links);
                //ac.AddAudit(item);
            }
        }
Beispiel #2
0
 public void BeginAudit()
 {
     Changes = new List<Change>();
     // add everything what was added first...
     foreach (var item in Added)
     {
         var c = new Change(item.Entity, item.State,null);
         Changes.Add(c);
     }
     foreach (var item in Modified)
     {
         var c = new Change(item.Entity, item.State, item.OriginalValues);
         Changes.Add(c);
     }
     foreach (var item in Deleted)
     {
         var c = new Change(item.Entity, item.State,null);
         Changes.Add(c);
     }
 }