/// <summary> /// Find a single change that matches the filters provided. /// </summary> /// <param name="filter">Find a single changes with the given filter parameters.</param> /// <returns></returns> /// <remarks></remarks> public static ChangeLogItem Load(ChangeLogItemFilter filter) { List <ChangeLogItem> lResults = ChangeLogItem.LoadCollection(filter); return(lResults.Count == 1 ? lResults[0] : null); }
/// <summary> /// Get a list of property changes from an object. /// </summary> /// <param name="prevObj">The previous object.</param> /// <param name="newObj">The new object.</param> /// <returns></returns> public List <ChangeLogItem> GetChangeLogItems(Object prevObj, Object newObj) { List <ChangeLogItem> changes = new List <ChangeLogItem>(); if (this.IsEqualType(prevObj, newObj)) { //Initialize the type for each Object, which should be the same. Type typeOld = prevObj.GetType(); Type typeNew = newObj.GetType(); //Initialize property list to process. List <PropertyInfo> properties = new List <PropertyInfo>(typeNew.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)); //Find Object state property ("ObjectState" or "State"). PropertyInfo objState = typeNew.GetProperty("ObjectState", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); objState = objState == null?typeNew.GetProperty("State", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) : objState; foreach (PropertyInfo pi in properties) { if (objState != null) { //Get Object state value. ObjectState objStateValue = (ObjectState)objState.GetValue(newObj, null); //Ignore the Object state property if (!Object.ReferenceEquals(pi.PropertyType, typeof(ObjectState))) { switch (objStateValue) { case ObjectState.ToBeInserted: Object insertValue = pi.GetValue(newObj, null); //Log inserts as previous values being empty/default. ChangeLogItem insertItem = GetChangeLogItem(null, insertValue, objStateValue, typeNew, pi); if (insertItem != null) { changes.Add(insertItem); } break; case ObjectState.ToBeUpdated: //Get the current property from the previous Object. PropertyInfo oldProperty = typeOld.GetProperty(pi.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); //Get values from their respective properties' list. Object prevValue = oldProperty.GetValue(prevObj, null); Object newValue = pi.GetValue(newObj, null); //Log updates as differences between previous and new values. ChangeLogItem updateItem = GetChangeLogItem(prevValue, newValue, objStateValue, typeNew, pi); if (updateItem != null) { changes.Add(updateItem); } break; case ObjectState.ToBeDeleted: Object currentValue = pi.GetValue(newObj, null); //Log deletes as new values being empty/default. ChangeLogItem deleteItem = GetChangeLogItem(currentValue, null, objStateValue, typeNew, pi); if (deleteItem != null) { changes.Add(deleteItem); } break; } } } } } return(changes); }
/// <summary> /// Get any differences between the old and new property values. /// </summary> /// <param name="prevValue">Prevous property value.</param> /// <param name="newValue">New property value.</param> /// <param name="ObjectState">The state of the object.</param> /// <param name="ObjectType">The type of the object.</param> /// <param name="propertyInfo">The property being evaluated.</param> /// <returns></returns> private ChangeLogItem GetChangeLogItem(Object prevValue, Object newValue, ObjectState ObjectState, Type ObjectType, PropertyInfo propertyInfo) { ChangeLogItem lItem = new ChangeLogItem(); lItem.OwnerID = this.OwnerID; lItem.OwnerName = ObjectType.Name; lItem.PropertyName = propertyInfo.Name; lItem.Type = ObjectState.ToString().Replace("ToBe", String.Empty); // Get rid of future tense from state name. if (Object.ReferenceEquals(propertyInfo.PropertyType, typeof(String))) { // COMPARE STRINGS String logPrevious = HString.SafeTrim(prevValue); String logNew = HString.SafeTrim(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious; lItem.NewValue = logNew; return(lItem); } } else if (Object.ReferenceEquals(propertyInfo.PropertyType, typeof(Int32))) { // COMPARE INTEGERS Int32 logPrevious = HNumeric.GetSafeInteger(prevValue); Int32 logNew = HNumeric.GetSafeInteger(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious.ToString(); lItem.NewValue = logNew.ToString(); return(lItem); } } else if (Object.ReferenceEquals(propertyInfo.PropertyType, typeof(Decimal))) { // COMPARE DECIMALS Decimal logPrevious = HNumeric.GetSafeDecimal(prevValue); Decimal logNew = HNumeric.GetSafeDecimal(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious.ToString(); lItem.NewValue = logNew.ToString(); return(lItem); } } else if (Object.ReferenceEquals(propertyInfo.PropertyType, typeof(DateTime))) { // COMPARE DATETIMES DateTime logPrevious = HDateTime.GetDateTime(prevValue); DateTime logNew = HDateTime.GetDateTime(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious.ToString(); lItem.NewValue = logNew.ToString(); return(lItem); } } else if (propertyInfo.PropertyType.IsEnum) { // COMPARE ENUMS Int32 logPrevious = Convert.ToInt32(prevValue); Int32 logNew = Convert.ToInt32(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious.ToString(); lItem.NewValue = logNew.ToString(); return(lItem); } } return(null); }