/// <summary> /// To the model. /// </summary> /// <param name="value">The value.</param> /// <returns></returns> public static Audit ToModel(this AuditDto value) { Audit result = new Audit(); value.CopyToModel(result); return(result); }
/// <summary> /// Saves the entity and returns a list of any entity changes that /// need to be logged /// </summary> /// <param name="PersonId">The id of the person making the change</param> /// <param name="changes">The changes.</param> /// <param name="audits">The audits.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public bool Save(int?PersonId, out List <EntityChange> changes, out List <AuditDto> audits, out List <string> errorMessages) { changes = new List <EntityChange>(); audits = new List <AuditDto>(); errorMessages = new List <string>(); Context.ChangeTracker.DetectChanges(); List <object> addedEntities = new List <object>(); List <object> deletedEntities = new List <object>(); List <object> modifiedEntities = new List <object>(); var contextAdapter = ((IObjectContextAdapter)Context); foreach (ObjectStateEntry entry in contextAdapter.ObjectContext.ObjectStateManager.GetObjectStateEntries( System.Data.EntityState.Added | System.Data.EntityState.Deleted | System.Data.EntityState.Modified | System.Data.EntityState.Unchanged)) { var rockEntity = entry.Entity as Entity <T>; var audit = new Rock.Model.AuditDto(); switch (entry.State) { case System.Data.EntityState.Added: addedEntities.Add(entry.Entity); audit.AuditType = AuditType.Add; break; case System.Data.EntityState.Deleted: deletedEntities.Add(entry.Entity); audit.AuditType = AuditType.Delete; break; case System.Data.EntityState.Modified: if (rockEntity != null) { bool cancel = false; rockEntity.RaiseUpdatingEvent(out cancel, PersonId); if (cancel) { errorMessages.Add(string.Format("Update cancelled by {0} event handler", rockEntity.TypeName)); contextAdapter.ObjectContext.Detach(entry); } else { modifiedEntities.Add(entry.Entity); audit.AuditType = AuditType.Modify; } } break; } if (rockEntity != null) { Type rockEntityType = rockEntity.GetType(); if (rockEntityType.Namespace == "System.Data.Entity.DynamicProxies") { rockEntityType = rockEntityType.BaseType; } if (AuditClass(rockEntityType)) { var dbEntity = Context.Entry(entry.Entity); var modifiedProperties = new List <string>(); PropertyInfo[] properties = rockEntityType.GetProperties(); foreach (PropertyInfo propInfo in properties) { if (AuditProperty(propInfo)) { var dbPropertyEntry = dbEntity.Property(propInfo.Name); if (dbPropertyEntry != null && dbPropertyEntry.IsModified) { modifiedProperties.Add(propInfo.Name); } } } if (modifiedProperties.Count > 0) { audit.DateTime = DateTime.Now; audit.PersonId = PersonId; audit.EntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(rockEntity.TypeName).Id; audit.EntityId = rockEntity.Id; audit.Title = rockEntity.ToString().Ellipsis(195); audit.Properties = modifiedProperties.AsDelimited(";"); audits.Add(audit); } } } } if (errorMessages.Count > 0) { return(false); } Context.SaveChanges(); foreach (object modifiedEntity in addedEntities) { var model = modifiedEntity as Entity <T>; if (model != null) { model.RaiseAddedEvent(PersonId); } } foreach (object deletedEntity in deletedEntities) { var model = deletedEntity as Entity <T>; if (model != null) { model.RaiseDeletedEvent(PersonId); } } foreach (object modifiedEntity in modifiedEntities) { var model = modifiedEntity as Entity <T>; if (model != null) { model.RaiseUpdatedEvent(PersonId); } } return(true); }