/// <summary> /// Stores an AuditLog object for the ongoing transaction /// </summary> public static IAuditLog Logg <T>(IDwarf obj, AuditLogTypes auditLogType, params AuditLogEventTrace[] auditUpdateEvents) { var type = DwarfHelper.DeProxyfy(obj); if (type.Implements <IAuditLogless>() || type.Implements <IAuditLog>()) { return(null); } var al = new AuditLog { ClassType = type.Name, AuditLogType = auditLogType, UserName = DwarfContext <T> .GetConfiguration().UserService.CurrentUser != null ? DwarfContext <T> .GetConfiguration().UserService.CurrentUser.UserName : string.Empty, TimeStamp = DateTime.Now, ObjectValue = obj.ToString(), }; if (!type.Implements <ICompositeId>()) { al.ObjectId = obj.Id.ToString(); } else { foreach (var ep in DwarfHelper.GetPKProperties(type)) { al.ObjectId += string.Format("[{0}: {1}]", ep.Name, ep.GetValue(obj)); } } if (auditLogType != AuditLogTypes.Created) { al.AuditDetails = "<?xml version=\"1.0\"?><Properties>"; foreach (var auditUpdateEvent in auditUpdateEvents) { al.AuditDetails += auditUpdateEvent.ToXml().ToString(); } al.AuditDetails += "</Properties>"; } DwarfContext <T> .GetDatabase().Insert <T, AuditLog>(al); return(al); }
/// <summary> /// Creates and returns an AuditLogEventTrace object for every modified property /// </summary> private AuditLogEventTrace[] CreateTraceEventsForProperties() { if (!IsSaved) { return(new AuditLogEventTrace[0]); } if (originalValues == null) { var fromDB = !typeof(T).Implements <ICompositeId>() ? Load(Id.Value) : Load(DwarfHelper.GetPKProperties <T>().Select(x => new WhereCondition <T> { ColumnPi = x.ContainedProperty, Value = x.GetValue(this) }).ToArray()); if (fromDB == null) { return(new AuditLogEventTrace[0]); } originalValues = fromDB.originalValues; } var dbProps = from ep in DwarfHelper.GetDBProperties(GetType()) let oldValue = originalValues[ep.Name] let x = ep.GetValue(this) let newValue = x is IDwarf ? ((IDwarf)ep.GetValue(this)).Id : ep.GetValue(this) where (oldValue != null && !oldValue.Equals(newValue)) || (oldValue == null && newValue != null) select new AuditLogEventTrace { PropertyName = ep.Name, OriginalValue = oldValue, NewValue = newValue }; var collections = from ep in DwarfHelper.GetGemListProperties(GetType()) where IsCollectionInitialized(ep.ContainedProperty) let x = (IGemList)ep.GetValue(this) let newValue = x let oldValue = x.Parse((string)originalValues[ep.Name] ?? string.Empty) where (oldValue != null && !oldValue.ComparisonString.Equals(newValue.ComparisonString)) || (oldValue == null && newValue != null) select new AuditLogEventTrace { PropertyName = ep.Name, OriginalValue = oldValue, NewValue = newValue }; return(dbProps.Concat(collections).ToArray()); }