public void Submit(string comment)
        {
            AssertOperation(Current, OperationCode.Submit);
            //提交时不进行权限验证,后面可能要加上,某些用户可能不具备提交特定流程的权限
            //AssertPrivilege(Current);

            var action = GetCurrentAction(Current, OperationCode.Submit);

            //判断是否本节点回环
            if (action.Transit.ActivityTemplateId != Current.ActivityTemplate.ActivityTemplateId)
            {
                //非本节点回环,创建下个节点实例
                var nextActivityInstance = NewActivityInstance(action.Transit);

                //标记该Activity已完成
                _originateActivityInstance = Current;
                _originateActivityInstance.MarkFinish();
                //_originateActivityInstance.Tail.MarkFinished();

                //替换当前节点
                Current = nextActivityInstance;
            }

            //相应的创建Action实例
            var actionRecord = new ActionRecord()
            {
                ActivityInstanceId = Current.ActivityInstanceId,
                RequiredRole       = Current.ActivityTemplate.RequiredRole.Id
            };

            Current.AddAction(actionRecord);

            //当前节点替换
            var auditTrail = new AuditTrailEntry()
            {
                IsNew = true
            };

            //添加审核日志
            AuditTrails.Add(auditTrail);

            _isDirty = true;
        }
Exemple #2
0
        /// <summary>
        /// The event that triggers when
        /// the context savechanges method is called
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentObjectContext_SavingChanges(object sender, EventArgs e)
        {
            try
            {
                ChangeTracker.DetectChanges(); // Important!
                System.Data.Entity.Core.Objects.ObjectContext           ctx = ((IObjectContextAdapter)this).ObjectContext;
                List <System.Data.Entity.Core.Objects.ObjectStateEntry> objectStateEntryList =
                    ctx.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added
                                                                 | System.Data.Entity.EntityState.Modified
                                                                 | System.Data.Entity.EntityState.Deleted)
                    .ToList();

                foreach (System.Data.Entity.Core.Objects.ObjectStateEntry entry in objectStateEntryList)
                {
                    object[] list = entry.Entity.GetType().GetCustomAttributes(false);
                    // Only the models that marked with 'Auditable' attribute will be tracked
                    // Inside the model the properties that need to tracked needed to be marked with
                    // Auditable attribute
                    if (list.Count() == 0 || !((Nido.Common.Utilities.Attributes.AuditableAttribute)(list[0])).DoAudit)
                    {
                        continue;
                    }

                    TypeAttributes te    = entry.Entity.GetType().Attributes;
                    AuditTrail     audit = new AuditTrail();
                    audit.RevisionStamp = DateTime.Now;
                    audit.TableName     = entry.EntitySet.Name + entry.EntityKey;
                    audit.UserName      = UserName;
                    audit.SystemName    = SystemName;
                    if (!entry.IsRelationship)
                    {
                        switch (entry.State)
                        {
                        case System.Data.Entity.EntityState.Added:
                            // write log...
                        {
                            audit.NewData = GetEntryValueInString(entry);
                            audit.Actions = AuditActions.I.ToString();
                        }
                        break;

                        case System.Data.Entity.EntityState.Deleted:
                            // write log...
                        {
                            audit.TablePrimaryId = GetKeyValue(entry);
                            audit.OldData        = GetEntryValueInString(entry);
                            audit.Actions        = AuditActions.D.ToString();
                        }
                        break;

                        case System.Data.Entity.EntityState.Modified:
                        {
                            string xmlOld = "<?xml version='1.0' encoding='utf-16'?>  <" + (entry.EntitySet).Name
                                            + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>";
                            string xmlNew = "<?xml version='1.0' encoding='utf-16'?>  <" + (entry.EntitySet).Name
                                            + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>";

                            PropertyInfo[] propList = entry.Entity.GetType().GetProperties();
                            Dictionary <string, object> attrList = new Dictionary <string, object>();
                            foreach (PropertyInfo pInfo in propList)
                            {
                                object[] atts = pInfo.GetCustomAttributes(typeof(AuditableAttribute), false);
                                if (atts.Length > 0)
                                {
                                    attrList.Add(pInfo.Name, atts[0]);
                                }
                            }
                            int      i      = 0;
                            object[] listte = entry.GetUpdatableOriginalValues()[0].GetType().GetCustomAttributes(false);
                            foreach (string propertyName in
                                     entry.GetModifiedProperties())
                            {
                                if (attrList.Keys.Contains(propertyName))
                                {
                                    DbDataRecord original = entry.OriginalValues;

                                    string oldValue = original.GetValue(
                                        original.GetOrdinal(propertyName))
                                                      .ToString();

                                    System.Data.Entity.Core.Objects.CurrentValueRecord current = entry.CurrentValues;
                                    string newValue = current.GetValue(
                                        current.GetOrdinal(propertyName))
                                                      .ToString();

                                    xmlOld += "<" + propertyName + " type='" + original.GetFieldType(i) + "'>" + oldValue + "</" + propertyName + ">";
                                    xmlNew += "<" + propertyName + " type='" + original.GetFieldType(i) + "'>" + newValue + "</" + propertyName + ">";
                                }
                                i++;
                            }

                            xmlOld += "</" + (entry.EntitySet).Name + ">";
                            xmlNew += "</" + (entry.EntitySet).Name + ">";

                            audit.OldData = xmlOld;
                            audit.NewData = xmlNew;

                            audit.TablePrimaryId = GetKeyValue(entry);
                            audit.Actions        = AuditActions.U.ToString();
                            break;
                        }
                        }
                    }
                    AuditTrails.Add(audit);
                }
            }
            catch
            {
                // Keep quite...
            }
        }
        /// <summary>
        /// prepare Audit logs under changes types
        /// </summary>
        /// <param name="dbEntry"></param>
        /// <param name="userId"></param>
        /// <param name="state"></param>
        private void GetAuditRecordsForChange(DbEntityEntry dbEntry, long userId, System.Data.Entity.EntityState state
                                              , IDataAuditingService _dataAuditingService, IStatusesAuditingService _statusesAuditingService)
        {
            DateTime changeTime = DateTime.UtcNow;

            // Get table name by calling the GetTableName()
            string tableName = (dbEntry.Entity as IAuditable).GetTableName();
            // Get primary key value by calling the GetKeyPropertyName()
            string primaryKeyName = (dbEntry.Entity as IAuditable).GetPrimaryKeyPropertyName();
            //Get is active property
            string isActiveProperty = (dbEntry.Entity as IAuditable).GetIsActivePropertName();
            //Get is Deleted property
            string isDeletedProperty = (dbEntry.Entity as IAuditable).GetIsDeletedPropertName();

            #region Data Changes

            //Save the audit from the data properties
            List <AuditEntity> auditDetails = new List <AuditEntity>();
            //in case Add only
            if (state == System.Data.Entity.EntityState.Added)
            {
                auditDetails = _dataAuditingService.AddAuditFromDataProperties(dbEntry, userId, changeTime, tableName,
                                                                               primaryKeyName, isActiveProperty, isDeletedProperty, state);
            }
            //In case modify only
            else if (state == System.Data.Entity.EntityState.Modified)
            {
                auditDetails = _dataAuditingService.ModifiedAuditFromDataProperties(dbEntry, userId, changeTime, tableName,
                                                                                    primaryKeyName, isActiveProperty, isDeletedProperty, state);
            }
            //check Added & modified list
            if (auditDetails != null && auditDetails.Any())
            {
                List <AuditTrail> auditTrail = new List <AuditTrail>();
                foreach (var item in auditDetails)
                {
                    AuditTrails.Add(new AuditTrail
                    {
                        ChangeXml    = item.ChangeXml,
                        EventType    = item.EventType,
                        IsData       = item.IsData,
                        LogDate      = item.LogDate,
                        ObjectTypeId = item.ObjectTypeId,
                        RecordID     = item.RecordID,
                        TableName    = item.TableName,
                        UserId       = item.UserId
                    });
                }
            }

            #endregion

            #region Status Changes

            //if the status id exists , save the audit statuses
            if (!string.IsNullOrEmpty(isActiveProperty))
            {
                var statusesAuditTrails = _statusesAuditingService.SaveAuditFromStatusProperty(dbEntry, userId, changeTime,
                                                                                               tableName, primaryKeyName, isActiveProperty, isDeletedProperty, state);
                if (statusesAuditTrails != null && statusesAuditTrails.Any())
                {
                    List <AuditTrail> auditTrail = new List <AuditTrail>();
                    foreach (var item in statusesAuditTrails)
                    {
                        AuditTrails.Add(new AuditTrail
                        {
                            ChangeXml    = item.ChangeXml,
                            EventType    = item.EventType,
                            IsData       = item.IsData,
                            LogDate      = item.LogDate,
                            ObjectTypeId = item.ObjectTypeId,
                            RecordID     = item.RecordID,
                            TableName    = item.TableName,
                            UserId       = item.UserId
                        });
                    }
                }
            }

            #endregion
        }