protected AuditSaveRequest GetAuditSaveRequest(ISaveRequestHandler handler)
        {
            var auditFields = new HashSet<Field>();
            var flag = handler.IsCreate ? FieldFlags.Insertable : FieldFlags.Updatable;
            foreach (var field in handler.Row.GetFields())
                if (field.Flags.HasFlag(flag))
                    auditFields.Add(field);

            Field[] array = new Field[auditFields.Count];
            auditFields.CopyTo(array);

            var auditRequest = new AuditSaveRequest(handler.Row.Table, (IIdRow)handler.Old, (IIdRow)handler.Row, array);

            var parentIdRow = handler.Row as IParentIdRow;
            if (parentIdRow != null)
            {
                var parentIdField = (Field)parentIdRow.ParentIdField;

                if (!parentIdField.ForeignTable.IsTrimmedEmpty())
                {
                    auditRequest.ParentTypeId = parentIdField.ForeignTable;
                    auditRequest.OldParentId = handler.IsCreate ? null : parentIdRow.ParentIdField[handler.Old];
                    auditRequest.NewParentId = parentIdRow.ParentIdField[handler.Row];
                }
            }

            return auditRequest;
        }
        protected AuditSaveRequest GetAuditSaveRequest(ISaveRequestHandler handler)
        {
            var auditFields = new HashSet <Field>();
            var flag        = handler.IsCreate ? FieldFlags.Insertable : FieldFlags.Updatable;

            foreach (var field in handler.Row.GetFields())
            {
                if (field.Flags.HasFlag(flag))
                {
                    auditFields.Add(field);
                }
            }

            Field[] array = new Field[auditFields.Count];
            auditFields.CopyTo(array);

            var auditRequest = new AuditSaveRequest(handler.Row.Table, (IIdRow)handler.Old, (IIdRow)handler.Row, array);

            var parentIdRow = handler.Row as IParentIdRow;

            if (parentIdRow != null)
            {
                var parentIdField = (Field)parentIdRow.ParentIdField;

                if (!parentIdField.ForeignTable.IsTrimmedEmpty())
                {
                    auditRequest.ParentTypeId = parentIdField.ForeignTable;
                    auditRequest.OldParentId  = handler.IsCreate ? null : parentIdRow.ParentIdField[handler.Old];
                    auditRequest.NewParentId  = parentIdRow.ParentIdField[handler.Row];
                }
            }

            return(auditRequest);
        }
Exemple #3
0
        public static void AuditInsert(IDbConnection connection, string schema, AuditSaveRequest request)
        {
            var fld = Dependency.Resolve<IAuditLogRow>(schema);

            var srcRow = (Row)request.NewEntity;
            var dstRow = srcRow.CreateNew();
            dstRow.TrackAssignments = true;

            foreach (var field in request.AuditFields)
                if (srcRow.IsAssigned(field) &&
                    !field.IsNull(srcRow))
                    field.Copy(srcRow, dstRow);

            if (request.FileFieldInfos != null)
            {
                if (request.FilesToDelete == null)
                    throw new ArgumentNullException("FilesToDelete");

                foreach (var file in request.FileFieldInfos)
                    if (dstRow.IsAssigned(file.Filename))
                        AuditFile(dstRow, file, request.FileSubFolder, request.FilesToDelete);
            }

            string auditData;
            if (dstRow.IsAnyFieldAssigned)
                auditData = dstRow.ToJson();
            else
                auditData = null;

            var audit = ((Row)fld).CreateNew();
            audit.TrackAssignments = true;
            fld.EntityTypeIdField[audit] = request.EntityType;
            fld.EntityIdField[audit] = request.NewEntity.IdField[srcRow];
            fld.ParentTypeIdField[audit] = request.ParentTypeId;
            fld.NewParentIdField[audit] = request.NewParentId;
            fld.DateField[audit] = DateTime.UtcNow;
            fld.AuditTypeIdField[audit] = (Int32?)AuditType.Insert;
            fld.OldAuditDataField[audit] = null;
            fld.NewAuditDataField[audit] = auditData;

            var loggingRow = request.NewEntity as ILoggingRow;
            if (loggingRow != null)
                fld.UserIdField[audit] = loggingRow.InsertUserIdField[(Row)request.NewEntity];
            if (fld.UserIdField[audit] == null)
                fld.UserIdField[audit] = Authorization.UserId.TryParseID().Value;

            connection.Insert(audit);
        }
        protected virtual AuditSaveRequest GetAuditRequest(HashSet <Field> auditFields)
        {
            Field[] array = new Field[auditFields.Count];
            auditFields.CopyTo(array);

            var auditRequest = new AuditSaveRequest(Row.Table, (IIdRow)Old, (IIdRow)Row, array);

            var parentIdRow = Row as IParentIdRow;

            if (parentIdRow != null)
            {
                var parentIdField = (Field)parentIdRow.ParentIdField;

                if (!parentIdField.ForeignTable.IsTrimmedEmpty())
                {
                    auditRequest.ParentTypeId = parentIdField.ForeignTable;
                    auditRequest.OldParentId  = Old == null ? null : parentIdRow.ParentIdField[Old];
                    auditRequest.NewParentId  = parentIdRow.ParentIdField[Row];
                }
            }

            return(auditRequest);
        }
Exemple #5
0
        public static void AuditUpdate(IDbConnection connection, string schema, AuditSaveRequest request)
        {
            var audit = PrepareAuditUpdate(schema, request);

            new SqlInsert(audit).Execute(connection);
        }
Exemple #6
0
        public static Row PrepareAuditUpdate(string schema, AuditSaveRequest request)
        {
            var fld = IoC.Resolve <IAuditLogRow>(schema);

            var data = new AuditUpdateData <Row>();

            var oldRow = (Row)request.OldEntity;
            var newRow = (Row)request.NewEntity;

            data.Old = oldRow.CreateNew();
            data.Old.TrackAssignments = true;
            data.New = oldRow.CreateNew();
            data.New.TrackAssignments = true;

            foreach (var field in request.AuditFields)
            {
                if (newRow.IsAssigned(field) &&
                    field.IndexCompare(oldRow, newRow) != 0)
                {
                    var strField = field as StringField;
                    if (strField == null ||
                        !strField[oldRow].IsTrimmedSame(strField[newRow]))
                    {
                        if (!field.IsNull(oldRow))
                        {
                            field.Copy(oldRow, data.Old);
                        }
                        if (!field.IsNull(newRow))
                        {
                            field.Copy(newRow, data.New);
                        }
                    }
                }
            }

            if (request.FileFieldInfos != null)
            {
                if (request.FilesToDelete == null)
                {
                    throw new ArgumentNullException("FilesToDelete");
                }

                foreach (var file in request.FileFieldInfos)
                {
                    if (file.Filename[oldRow].IsTrimmedSame(file.Filename[newRow]))
                    {
                        continue;
                    }

                    if (!file.Filename[oldRow].IsTrimmedEmpty())
                    {
                        file.OriginalName.Copy(oldRow, data.Old);
                        file.Size.Copy(oldRow, data.Old);
                        AuditFile(data.Old, file, request.FileSubFolder, request.FilesToDelete);
                    }

                    if (!file.Filename[newRow].IsTrimmedEmpty())
                    {
                        file.OriginalName.Copy(newRow, data.New);
                        file.Size.Copy(newRow, data.New);
                        AuditFile(data.New, file, request.FileSubFolder, request.FilesToDelete);
                    }
                }
            }

            if (!data.Old.IsAnyFieldAssigned)
            {
                data.Old = null;
            }

            if (!data.New.IsAnyFieldAssigned)
            {
                data.New = null;
            }

            string oldAuditData, newAuditData;

            if (data.Old == null)
            {
                oldAuditData = null;
            }
            else
            {
                oldAuditData = data.Old.ToJson();
            }

            if (data.New == null)
            {
                newAuditData = null;
            }
            else
            {
                newAuditData = data.New.ToJson();
            }

            var audit = ((Row)fld).CreateNew();

            audit.TrackAssignments       = true;
            fld.EntityTypeIdField[audit] = request.EntityType;
            fld.EntityIdField[audit]     = request.OldEntity.IdField[oldRow];
            fld.ParentTypeIdField[audit] = request.ParentTypeId;
            fld.OldParentIdField[audit]  = request.OldParentId;
            fld.NewParentIdField[audit]  = request.NewParentId;
            fld.DateField[audit]         = DateTime.UtcNow;
            fld.AuditTypeIdField[audit]  = (Int32)AuditType.Update;
            fld.OldAuditDataField[audit] = oldAuditData;
            fld.NewAuditDataField[audit] = newAuditData;

            var loggingRow = request.NewEntity as ILoggingRow;

            if (loggingRow != null)
            {
                fld.UserIdField[audit] = loggingRow.UpdateUserIdField[(Row)request.NewEntity];
            }
            if (fld.UserIdField[audit] == null)
            {
                fld.UserIdField[audit] = (int)SecurityHelper.CurrentUserId;
            }

            return(audit);
        }
Exemple #7
0
        public static void AuditInsert(IDbConnection connection, string schema, AuditSaveRequest request)
        {
            var fld = IoC.Resolve <IAuditLogRow>(schema);

            var srcRow = (Row)request.NewEntity;
            var dstRow = srcRow.CreateNew();

            dstRow.TrackAssignments = true;

            foreach (var field in request.AuditFields)
            {
                if (srcRow.IsAssigned(field) &&
                    !field.IsNull(srcRow))
                {
                    field.Copy(srcRow, dstRow);
                }
            }

            if (request.FileFieldInfos != null)
            {
                if (request.FilesToDelete == null)
                {
                    throw new ArgumentNullException("FilesToDelete");
                }

                foreach (var file in request.FileFieldInfos)
                {
                    if (dstRow.IsAssigned(file.Filename))
                    {
                        AuditFile(dstRow, file, request.FileSubFolder, request.FilesToDelete);
                    }
                }
            }

            string auditData;

            if (dstRow.IsAnyFieldAssigned)
            {
                auditData = dstRow.ToJson();
            }
            else
            {
                auditData = null;
            }

            var audit = ((Row)fld).CreateNew();

            audit.TrackAssignments       = true;
            fld.EntityTypeIdField[audit] = request.EntityType;
            fld.EntityIdField[audit]     = request.NewEntity.IdField[srcRow];
            fld.ParentTypeIdField[audit] = request.ParentTypeId;
            fld.NewParentIdField[audit]  = request.NewParentId;
            fld.DateField[audit]         = DateTime.UtcNow;
            fld.AuditTypeIdField[audit]  = (Int32?)AuditType.Insert;
            fld.OldAuditDataField[audit] = null;
            fld.NewAuditDataField[audit] = auditData;

            var loggingRow = request.NewEntity as ILoggingRow;

            if (loggingRow != null)
            {
                fld.UserIdField[audit] = loggingRow.InsertUserIdField[(Row)request.NewEntity];
            }
            if (fld.UserIdField[audit] == null)
            {
                fld.UserIdField[audit] = (int)SecurityHelper.CurrentUserId;
            }

            new SqlInsert(audit).Execute(connection);
        }
 public static void AuditUpdate(IDbConnection connection, string schema, AuditSaveRequest request)
 {
     var audit = PrepareAuditUpdate(schema, request);
     connection.Insert(audit);
 }
        public static Row PrepareAuditUpdate(string schema, AuditSaveRequest request)
        {
#if COREFX
            var fld = Dependency.Resolve<IAuditLogRow>();
#else
            var fld = Dependency.Resolve<IAuditLogRow>(schema);
#endif

            var data = new AuditUpdateData<Row>();

            var oldRow = (Row)request.OldEntity;
            var newRow = (Row)request.NewEntity;

            data.Old = oldRow.CreateNew();
            data.Old.TrackAssignments = true;
            data.New = oldRow.CreateNew();
            data.New.TrackAssignments = true;

            foreach (var field in request.AuditFields)
            {
                if (newRow.IsAssigned(field) &&
                    field.IndexCompare(oldRow, newRow) != 0)
                {
                    var strField = field as StringField;
                    if (ReferenceEquals(null, strField) ||
                        !strField[oldRow].IsTrimmedSame(strField[newRow]))
                    {
                        if (!field.IsNull(oldRow))
                            field.Copy(oldRow, data.Old);
                        if (!field.IsNull(newRow))
                            field.Copy(newRow, data.New);
                    }
                }
            }

            if (request.FileFieldInfos != null)
            {
                if (request.FilesToDelete == null)
                    throw new ArgumentNullException("FilesToDelete");

                foreach (var file in request.FileFieldInfos)
                {
                    if (file.Filename[oldRow].IsTrimmedSame(file.Filename[newRow]))
                        continue;

                    if (!file.Filename[oldRow].IsTrimmedEmpty())
                    {
                        file.OriginalName.Copy(oldRow, data.Old);
                        file.Size.Copy(oldRow, data.Old);
                        AuditFile(data.Old, file, request.FileSubFolder, request.FilesToDelete);
                    }

                    if (!file.Filename[newRow].IsTrimmedEmpty())
                    {
                        file.OriginalName.Copy(newRow, data.New);
                        file.Size.Copy(newRow, data.New);
                        AuditFile(data.New, file, request.FileSubFolder, request.FilesToDelete);
                    }
                }
            }

            if (!data.Old.IsAnyFieldAssigned)
                data.Old = null;

            if (!data.New.IsAnyFieldAssigned)
                data.New = null;

            string oldAuditData, newAuditData;
            if (data.Old == null)
                oldAuditData = null;
            else
                oldAuditData = data.Old.ToJson();

            if (data.New == null)
                newAuditData = null;
            else
                newAuditData = data.New.ToJson();

            var audit = ((Row)fld).CreateNew();
            audit.TrackAssignments = true;
            fld.EntityTypeIdField[audit] = request.EntityType;
            fld.EntityIdField[audit] = request.OldEntity.IdField[oldRow];
            fld.ParentTypeIdField[audit] = request.ParentTypeId;
            fld.OldParentIdField[audit] = request.OldParentId;
            fld.NewParentIdField[audit] = request.NewParentId;
            fld.DateField[audit] = DateTime.UtcNow;
            fld.AuditTypeIdField[audit] = (Int32)AuditType.Update;
            fld.OldAuditDataField[audit] = oldAuditData;
            fld.NewAuditDataField[audit] = newAuditData;

            var loggingRow = request.NewEntity as ILoggingRow;
            if (loggingRow != null)
                fld.UserIdField[audit] = loggingRow.UpdateUserIdField[(Row)request.NewEntity];
            if (fld.UserIdField[audit] == null)
                fld.UserIdField[audit] = (long)Authorization.UserId.TryParseID();

            return audit;
        }