Ejemplo n.º 1
0
        public void AuditRecord(string sessionToken, out List <int> entityPKMaster, out List <int> entityPKDetail, out string auditingOperation)
        {
            IEnumerable <ObjectStateEntry> changes = _context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified);

            auditDetails = new List <AuditingDetail>();

            string serverName = HttpContext.Current.Server.MachineName;
            string userFK     = "";

            if (HttpContext.Current.User != null && HttpContext.Current.User.Identity != null)
            {
                userFK = SecurityHelper.GetUserPKFromCookie().ToString();
            }

            // default out assignments
            entityPKDetail    = new List <int>();
            entityPKMaster    = new List <int>();
            auditingOperation = AuditingOperation.INSERT.ToString();
            int counter = 0;

            foreach (ObjectStateEntry objectStateEntry in changes)
            {
                if (!objectStateEntry.IsRelationship &&
                    objectStateEntry.Entity != null &&
                    !(objectStateEntry.Entity is AuditingDetail) &&
                    !(objectStateEntry.Entity is AuditingMaster))
                {
                    auditingMaster = new AuditingMaster()
                    {
                        UserFK       = userFK,
                        DBName       = _context.DBName(),
                        TableName    = objectStateEntry.EntitySet.Name,
                        Date         = DateTime.Now,
                        Operation    = getAuditOperation(objectStateEntry),
                        ServerName   = serverName,
                        SessionToken = sessionToken
                    };

                    auditingOperation = getAuditOperation(objectStateEntry);

                    auditingMateus.AuditingMasters.AddObject(auditingMaster);

                    auditDetails = GetAuditEntries(objectStateEntry);

                    if (auditDetails.Count > 0)
                    {
                        auditingMateus.SaveChanges();

                        auditDetails.ForEach(ad => ad.AuditingMasterFK = auditingMaster.AuditingMasterPK);

                        AuditingDetail auditingDetailEntityPK;

                        auditingDetailEntityPK = auditDetails.First();

                        if (objectStateEntry.State == EntityState.Added)
                        {
                            auditingMateus.AuditingDetails.AddObject(auditingDetailEntityPK);
                            auditingMateus.SaveChanges();
                        }

                        int i = 0;
                        foreach (var audit in auditDetails)
                        {
                            if (++i == 1 && objectStateEntry.State == EntityState.Added)
                            {
                                continue;
                            }
                            auditingMateus.AuditingDetails.AddObject(audit);
                        }
                        auditingMateus.SaveChanges();

                        entityPKDetail.Insert(counter, auditingDetailEntityPK.AuditingDetailPK);
                        entityPKMaster.Insert(counter, auditingMaster.AuditingMasterPK);

                        counter++;
                    }
                }
            }

            return;
        }