public void CopyUpdatableValues(DatabaseTable newData)
{
if (newData == null) return;
ReadOnlyCollection<DBField> fieldList = DBField.GetFieldList(newData.GetType());
foreach (DBField currField in fieldList) {
object newValue = currField.GetValue(newData);
object oldValue = currField.GetValue(this);
if (currField.AutoUpdate) {
if (newValue == null) {
currField.SetValue(this, newValue);
continue;
}
// if the updated value is just the default, don't update.
// something is better than nothing
if (newValue.Equals(currField.Default))
continue;
// if we have a string try to compare trimmed strings
if (newValue.GetType() == typeof(string) && ((string)newValue ?? string.Empty).Trim() == ((string)oldValue ?? string.Empty).Trim())
continue;
// if the value just hasn't changed, dont update
if (newValue.Equals(oldValue))
continue;
// check if the old value is the default value
bool oldValueIsDefault = false;
if (oldValue is string && currField.Default is string) {
if (((string)oldValue ?? string.Empty).Trim().Equals(((string)currField.Default).Trim()))
oldValueIsDefault = true;
}
else if (oldValue == null || oldValue.Equals(currField.Default))
oldValueIsDefault = true;
// if we are protecting non-default values continue
if (protectExistingValuesFromCopy && !oldValueIsDefault)
continue;
currField.SetValue(this, newValue);
}
}
// reset the value protection to true again
protectExistingValuesFromCopy = true;
}