private void FormatValues(int entityTypeId, IEntity targetEntity, ChangeRecord changeRecord, RockContext rockContext) { //Get the target Entity if (changeRecord.RelatedEntityId.HasValue && changeRecord.RelatedEntityId.Value != 0 && changeRecord.RelatedEntityTypeId.HasValue) { entityTypeId = changeRecord.RelatedEntityTypeId.Value; switch (changeRecord.Action) { case ChangeRecordAction.Create: targetEntity = ChangeRequest.CreateNewEntity(changeRecord.RelatedEntityTypeId.Value, changeRecord.NewValue, rockContext, false); break; case ChangeRecordAction.Delete: targetEntity = ChangeRequest.CreateNewEntity(changeRecord.RelatedEntityTypeId.Value, changeRecord.OldValue, rockContext, false); break; default: targetEntity = ChangeRequest.GetEntity(changeRecord.RelatedEntityTypeId.Value, changeRecord.RelatedEntityId.Value, rockContext); break; } } //Enums if (changeRecord.Property.IsNotNullOrWhiteSpace()) { PropertyInfo enumProp = targetEntity.GetType().GetProperty(changeRecord.Property, BindingFlags.Public | BindingFlags.Instance); if (enumProp != null && enumProp.PropertyType != null && enumProp.PropertyType.IsEnum) { enumProp.PropertyType.GetEnumUnderlyingType(); changeRecord.NewValue = System.Enum.GetName(enumProp.PropertyType, changeRecord.NewValue.AsInteger()).SplitCase(); changeRecord.OldValue = System.Enum.GetName(enumProp.PropertyType, changeRecord.OldValue.AsInteger()).SplitCase(); } } //Format new value var newObject = changeRecord.NewValue.FromJsonOrNull <BasicEntity>(); if (newObject != null) { if (changeRecord.Property.IsNullOrWhiteSpace()) { changeRecord.NewValue = targetEntity.ToString(); } else { PropertyInfo prop = targetEntity.GetType().GetProperty(changeRecord.Property, BindingFlags.Public | BindingFlags.Instance); if (prop.PropertyType.GetInterfaces().Any(i => i.IsInterface && i.GetInterfaces().Contains(typeof(IEntity)))) { var entityTypeCache = EntityTypeCache.Get(prop.PropertyType); var entityType = entityTypeCache.GetEntityType(); var dyn = changeRecord.NewValue.FromJsonOrNull <Dictionary <string, object> >(); var entity = (( IEntity )Activator.CreateInstance(entityType)); foreach (var key in dyn.Keys) { var eleProp = entity.GetType().GetProperty(key); if (eleProp != null) { ChangeRequest.SetProperty(entity, eleProp, dyn[key].ToStringSafe()); } } if (entity != null) { changeRecord.NewValue = entity.ToString(); } } } } //Format old Value var oldObject = changeRecord.OldValue.FromJsonOrNull <BasicEntity>(); if (oldObject != null) { if (changeRecord.Property.IsNullOrWhiteSpace()) { changeRecord.OldValue = targetEntity.ToString(); } else { PropertyInfo prop = targetEntity.GetType().GetProperty(changeRecord.Property, BindingFlags.Public | BindingFlags.Instance); if (prop.PropertyType.GetInterfaces().Any(i => i.IsInterface && i.GetInterfaces().Contains(typeof(IEntity)))) { var entityTypeCache = EntityTypeCache.Get(prop.PropertyType); var entityType = entityTypeCache.GetEntityType(); var dyn = changeRecord.OldValue.FromJsonOrNull <Dictionary <string, object> >(); var entity = (( IEntity )Activator.CreateInstance(entityType)); foreach (var key in dyn.Keys) { var eleProp = entity.GetType().GetProperty(key); if (eleProp != null) { ChangeRequest.SetProperty(entity, eleProp, dyn[key].ToStringSafe()); } } if (entity != null) { changeRecord.OldValue = entity.ToString(); } } } } //Special Dispensation for the photo. if (changeRecord.Property == "PhotoId") { if (changeRecord.NewValue.AsInteger() != 0) { changeRecord.NewValue = string.Format("<a href='/GetImage.ashx?id={0}' target='_blank'><img src='/GetImage.ashx?id={0}' height=50></a>", changeRecord.NewValue); } if (changeRecord.OldValue.AsInteger() != 0) { changeRecord.OldValue = string.Format("<a href='/GetImage.ashx?id={0}' target='_blank'><img src='/GetImage.ashx?id={0}' height=50></a>", changeRecord.OldValue); } } else { //Format Property Name if (changeRecord.RelatedEntityType != null) { changeRecord.Property = changeRecord.RelatedEntityType.Name.Split('.').Last() + ": " + changeRecord.Property.SplitCase(); } else { changeRecord.Property = changeRecord.Property.SplitCase(); } if (changeRecord.Comment.IsNotNullOrWhiteSpace()) { changeRecord.Property += "<br>(" + changeRecord.Comment + ")"; } } }