/// <summary> /// <p>Audits the this HermesScheduleItemStatus against another HermesScheduleItemStatus. It makes a /// field-by-field comparison testing what has changed.</p> <p>This method will fill the TextValue# or /// NumericValue#, and ID fields of each HermesAuditRecord it generates. It will be up to the service using this /// entity to fill the other fields.</p> <p><strong>Implementation Notes</strong></p> <ol type="disc"> <li>If /// the passed entity is null, jus create one HermesAuditRecord</li> <li>Iterate over the Values field. For each /// changed property: <ol type="disc"> <li>Create new HermesAuditRecord.</li> <li>If it is numeric, set /// hermesAuditRecord's NumericValue2 field to this entity's property value, and the NumericValue1 field to the /// passed entity's property value</li> <li>If it is text, set hermesAuditRecord's TextValue2 field to this /// entity's property value, and the TextValue1 field to the passed entity's property value</li> </ol> </li> /// <li>Return the HermesAuditRecords as an IList of HermesAuditRecord</li> </ol> /// </summary> /// <exception cref="IllegalAuditItemException"> /// IllegalAuditItemException If old HermesScheduleItemStatus is the same object as this /// </exception> /// <param name="old">HermesScheduleItemStatus to compary this to</param> /// <returns>IList of HermesAuditRecords detailing any changes</returns> public IList <HermesAuditRecord> Audit(HermesScheduleItemStatus old) { IList <HermesAuditRecord> ret = new List <HermesAuditRecord>(); if (old == null) { ret.Add(new HermesAuditRecord()); return(ret); } if (object.ReferenceEquals(old, this)) { throw new IllegalAuditItemException("Cannot audit an item against itself."); } foreach (string key in old.Values.Keys) { object newVal = Values[key].Value; object oldVal = old.Values[key].Value; if (!oldVal.Equals(newVal)) { HermesAuditRecord auditRec = new HermesAuditRecord(); if (Helper.IsNumeric(oldVal.GetType())) { //TODO set NumericValue } else if (oldVal.GetType().Equals(typeof(string))) { //TODO set TextValue } //TODO other type of fields ret.Add(auditRec); } } return(ret); }
/// <summary> /// Gets a list of records signifying the properties that are different between the old and new entity. /// </summary> /// <typeparam name="TEntity">The type of the entity</typeparam> /// <param name="newEntity">The new entity. This can never be null.</param> /// <param name="oldEntity">The old entity. This may be null.</param> /// <param name="newEntityId">The id of the new entity. This may be null.</param> /// <returns> /// A list of records signifying the properties that are different between the old and new entity. /// </returns> internal static IList <HermesAuditRecord> GetAuditRecords <TEntity>( ISearchable <TEntity> newEntity, ISearchable <TEntity> oldEntity, string newEntityId) where TEntity : ISearchable <TEntity> { IList <HermesAuditRecord> ret = new List <HermesAuditRecord>(); //If it is a new entity, return just 1 record if (oldEntity == null) { HermesAuditRecord auditRecord = new HermesAuditRecord(); auditRecord.Message = typeof(TEntity).Name + ".CreatedAudit"; auditRecord.EntityId = newEntityId; ret.Add(auditRecord); return(ret); } //Check each property foreach (string key in newEntity.Values.Keys) { object newVal = newEntity.Values[key].Value; object oldVal = oldEntity.Values[key].Value; if (newVal == null && oldVal == null) { continue; } //Get the type of the property Type propertyType = oldVal == null?newVal.GetType() : oldVal.GetType(); //Property of type GenericNote, we compare the description if (propertyType.Equals(typeof(HermesGenericNote))) { newVal = newVal == null ? null : (newVal as HermesGenericNote).Description; oldVal = oldVal == null ? null : (oldVal as HermesGenericNote).Description; propertyType = typeof(string); } //Property of type HermesActivity, we compare the name else if (propertyType.Equals(typeof(HermesActivity))) { newVal = newVal == null ? null : (newVal as HermesActivity).Name; oldVal = oldVal == null ? null : (oldVal as HermesActivity).Name; propertyType = typeof(string); } //Property of type HermesActivity, we compare the name else if (propertyType.Equals(typeof(HermesActivityType))) { newVal = newVal == null ? null : (newVal as HermesActivityType).Name; oldVal = oldVal == null ? null : (oldVal as HermesActivityType).Name; propertyType = typeof(string); } //Property of type HermesActivity, we compare the name else if (propertyType.Equals(typeof(HermesActivityGroup))) { newVal = newVal == null ? null : (newVal as HermesActivityGroup).Name; oldVal = oldVal == null ? null : (oldVal as HermesActivityGroup).Name; propertyType = typeof(string); } //Property of type HermesScheduleItemRequestStatus, we compare the Description else if (propertyType.Equals(typeof(HermesScheduleItemRequestStatus))) { newVal = newVal == null ? null : (newVal as HermesScheduleItemRequestStatus).Description; oldVal = oldVal == null ? null : (oldVal as HermesScheduleItemRequestStatus).Description; propertyType = typeof(string); } //Property of type HermesScheduleItemRequestStatus, we compare the name else if (propertyType.Equals(typeof(HermesScheduleItemStatus))) { newVal = newVal == null ? null : (newVal as HermesScheduleItemStatus).Description; oldVal = oldVal == null ? null : (oldVal as HermesScheduleItemStatus).Description; propertyType = typeof(string); } //Create record if the properties are not equal. if ((propertyType.IsValueType || propertyType.Equals(typeof(string))) && !object.Equals(oldVal, newVal)) { //Create the HermesAuditRecord and set EntityId HermesAuditRecord auditRec = new HermesAuditRecord(); auditRec.EntityId = newEntityId; auditRec.Message = typeof(TEntity).Name + "." + ToCamelCase(key) + "Audit"; //int, decimal, double etc. if (IsNumeric(propertyType)) { auditRec.NumericValue1 = Decimal.Parse(oldVal.ToString()); auditRec.NumericValue2 = Decimal.Parse(newVal.ToString()); } //string else if (propertyType.Equals(typeof(string))) { auditRec.TextValue1 = (string)oldVal; auditRec.TextValue2 = (string)newVal; } //DateTime else if (propertyType.Equals(typeof(DateTime))) { auditRec.NumericValue1 = new Decimal(((DateTime)oldVal).ToBinary()); auditRec.NumericValue2 = new Decimal(((DateTime)newVal).ToBinary()); } //bool, char else if (propertyType.IsValueType) { auditRec.TextValue1 = oldVal.ToString(); auditRec.TextValue2 = newVal.ToString(); } ret.Add(auditRec); } } return(ret); }