コード例 #1
0
ファイル: Auditor.cs プロジェクト: miseeger/NBean
        private void AuditChanges(string action, Bean bean)
        {
            var api = bean.Api;

            if (!api.DirtyTracking || _auditBlacklist.Contains(bean.GetKind().ToUpper()))
            {
                return;
            }

            var dirtyBackup = bean.GetDirtyBackup();
            var changes     = GetChanges(bean);

            if (action != "DELETE" && !changes.Any())
            {
                return;
            }

            var kind    = bean.GetKind();
            var keyName = api.GetKeyName(kind);

            if ("INSERT|UPDATE".Contains(action))
            {
                foreach (var change in changes)
                {
                    var audit = api.Dispense("AUDIT");

                    audit
                    .Put("AuditDate", DateTime.Now)
                    .Put("Action", action)
                    .Put("User", api.CurrentUser ?? string.Empty)
                    .Put("Object", bean.GetKind())
                    .Put("ObjectId", bean[keyName])
                    .Put("Property", change.Key)
                    .Put("PropertyType", api.GetDbTypeFromValue(change.Value))
                    .Put("OldValue", action == "UPDATE" ? dirtyBackup[change.Key].FormatValueToString() : string.Empty)
                    .Put("NewValue", bean[change.Key].FormatValueToString())
                    .Put("Notes", string.Empty)
                    .Store();
                }
            }

            else if (action == "DELETE")
            {
                var audit = api.Dispense("AUDIT");

                audit
                .Put("AuditDate", DateTime.Now)
                .Put("Action", action)
                .Put("User", api.CurrentUser ?? string.Empty)
                .Put("Object", bean.GetKind())
                .Put("ObjectId", bean[keyName])
                .Put("Property", string.Empty)
                .Put("PropertyType", string.Empty)
                .Put("OldValue", string.Empty)
                .Put("NewValue", string.Empty)
                .Put("Notes", bean.ToJson());

                api.Store(audit);
            }
        }