Ejemplo n.º 1
0
        public object InsertIntoAuditTrail(AuditTrail model)
        {
            try
            {
                var auditTralId = string.Empty;
                if (model.WhatActionId == 1)
                {
                    return(auditTrailRepository.InsertIntoAuditTrail(model).ToString());
                }
                else
                {
                    auditTralId = auditTrailRepository.InsertIntoAuditTrail(model).ToString();
                    AuditTrailDetail auditTrailDetail = null;

                    foreach (var item in model.InputFeildAndValue)
                    {
                        auditTrailDetail = new AuditTrailDetail
                        {
                            WhichFeildName = item.WhichFeildName,
                            WhichValue     = item.WhichValue,
                            WhatValue      = item.WhatValue,
                            AuditTrailId   = auditTralId
                        };
                        auditTrailRepository.InsertIntoAuditTrailDetail(auditTrailDetail);
                    }

                    return(auditTralId);
                }
            }
            catch (Exception ex)
            {
                return(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        public static AuditTrail LoadEntiyProperty(EntityEntry entry, AuditTrail auditTrail)
        {
            var allProps = entry.Entity.GetType().GetTypeInfo().DeclaredProperties;
            var props    = allProps.Where(p => p.PropertyType == typeof(string) ||
                                          !typeof(IEnumerable).IsAssignableFrom(p.PropertyType));

            foreach (var prop in props)
            {
                if (!prop.GetGetMethod().IsVirtual&& !IsReference(entry, prop.Name))
                {
                    var currentValue  = entry.Property(prop.Name).CurrentValue;
                    var originalValue = entry.Property(prop.Name).OriginalValue;

                    currentValue  = currentValue ?? string.Empty;
                    originalValue = originalValue ?? string.Empty;

                    var auditTrailDetail = new AuditTrailDetail();

                    TypeHelper.IsCoreType(prop.GetType());
                    {
                        auditTrailDetail.ColumnName = prop.Name;
                        auditTrailDetail.NewRecord  = currentValue.ToString();

                        if (entry.State == EntityState.Added)
                        {
                            auditTrail.Action = ChangeType.Insert.ToString();
                        }
                        else if (entry.Property(prop.Name).IsModified)
                        {
                            auditTrailDetail.OldRecord = originalValue.ToString();
                            auditTrail.Action          = ChangeType.Update.ToString();
                        }
                        else if (entry.State == EntityState.Deleted)
                        {
                            auditTrailDetail.OldRecord = originalValue.ToString();
                            auditTrail.Action          = ChangeType.Delete.ToString();
                        }

                        auditTrail.AuditTrailDetail.Add(auditTrailDetail);
                    }
                }
            }

            return(auditTrail);
        }
        public object InsertIntoAuditTrailDetail(AuditTrailDetail auditTrailDetail)
        {
            try
            {
                using (var conn = this.GetConnection())
                {
                    var dyParam = new OracleDynamicParameters();
                    dyParam.Add("V_AUDIT_TRAIL_ID", OracleDbType.Varchar2, ParameterDirection.Input, auditTrailDetail.AuditTrailId);
                    dyParam.Add("V_WHICH_FIELD_NAME", OracleDbType.Varchar2, ParameterDirection.Input, auditTrailDetail.WhichFeildName);
                    dyParam.Add("V_WHICH_VALUE", OracleDbType.Varchar2, ParameterDirection.Input, auditTrailDetail.WhichValue);
                    dyParam.Add("V_WHAT_VALUE", OracleDbType.Varchar2, ParameterDirection.Input, auditTrailDetail.WhatValue);
                    dyParam.Add("V_PARTICULAR", OracleDbType.Varchar2, ParameterDirection.Input, auditTrailDetail.Particular);

                    var result = SqlMapper.Query(conn, dbUser + "PROC_AUDIT_TRAIL_DTL", param: dyParam, commandType: CommandType.StoredProcedure);
                    this.CloseConnection(conn);

                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        protected virtual void LogAuditTrail(T entity, string action)
        {
            if (CurrentUser != null)
            {
                action = action.ToUpperInvariant();
                if (_auditTrailRepository == null)
                {
                    _auditTrailRepository = new GenericRepository <AuditTrail>(Repository.Context);
                }
                if (_auditTrailDetailRepository == null)
                {
                    _auditTrailDetailRepository = new GenericRepository <AuditTrailDetail>(Repository.Context);
                }

                try
                {
                    // Logs audit trail
                    AuditTrail auditTrail = new AuditTrail();
                    auditTrail.id          = Guid.NewGuid().ToString();
                    auditTrail.object_id   = entity.GetType().GetProperty("id").GetValue(entity, null).ToString();
                    auditTrail.object_type = entity.GetType().ToString();
                    auditTrail.action      = action;
                    auditTrail.date_time   = DateTime.Now;
                    auditTrail.user_id     = CurrentUser.id;
                    _auditTrailRepository.Insert(auditTrail);

                    switch (entity.EntityState)
                    {
                    case EntityState.Added:
                    {
                        foreach (var property in entity.GetType().GetProperties())
                        {
                            AuditTrailDetail auditTrailDetail = new AuditTrailDetail();
                            auditTrailDetail.id            = Guid.NewGuid().ToString();
                            auditTrailDetail.parent_id     = auditTrail.id;
                            auditTrailDetail.property_name = property.Name;
                            auditTrailDetail.old_value     = string.Empty;
                            auditTrailDetail.new_value     = property.GetValue(entity, null) != null?property.GetValue(entity, null).ToString() : null;

                            _auditTrailDetailRepository.Insert(auditTrailDetail);
                        }
                    }
                    break;

                    case EntityState.Modified:
                    {
                        var stateEntry = Repository.Context.ObjectStateManager.GetObjectStateEntry(entity);
                        foreach (var propertyName in stateEntry.GetModifiedProperties())
                        {
                            AuditTrailDetail auditTrailDetail = new AuditTrailDetail();
                            auditTrailDetail.id            = Guid.NewGuid().ToString();
                            auditTrailDetail.parent_id     = auditTrail.id;
                            auditTrailDetail.property_name = propertyName;
                            auditTrailDetail.old_value     = stateEntry.OriginalValues[propertyName] != null ? stateEntry.OriginalValues[propertyName].ToString() : null;
                            auditTrailDetail.new_value     = stateEntry.CurrentValues[propertyName] != null ? stateEntry.CurrentValues[propertyName].ToString() : null;
                            _auditTrailDetailRepository.Insert(auditTrailDetail);
                        }
                    }
                    break;

                    default: break;
                    }
                }
                catch (Exception ex)
                {
                    AppLogger.logError(this.ToString(), ex);
                    throw;
                }
            }
        }