public void TranslateValuesForEmployee(Employee employee, FieldLegalValueConfiguration fieldLegalValueConfiguration, string[] propertiesForEmployee, out TranslationLogEntry logEntry) { logEntry = null; if (propertiesForEmployee == null) { propertiesForEmployee = Utilities.GetPropertyNames<Employee>(); } // Loop through all fields for type foreach (string fieldName in propertiesForEmployee) { string fieldValue = Convert.ToString(employee.GetMemberValue(fieldName)); IEnumerable<FieldLegalValue> legalValuesForFieldName = fieldLegalValueConfiguration.FieldLegalValuesForFieldName(fieldName); if (String.IsNullOrEmpty(fieldValue) == false) { if (legalValuesForFieldName.Any(v => v.LegalValue == fieldValue) == true || legalValuesForFieldName.Count() == 0) // Check if legal { } else { PropertyInfo propertyInfo = employee.GetType().GetProperty(fieldName); FieldTranslatorChange change = new FieldTranslatorChange(); change.FieldName = fieldName; change.PreviousValue = employee.GetMemberValue(fieldName); bool logChange = true; if (legalValuesForFieldName.Any(v => v.FieldTranslations.Cast<FieldTranslation>().Any(t => t.TranslationValue == fieldValue))) // If not legal - check if it can be autocorrected { object newValue = null; var legalValueMatch = legalValuesForFieldName.Where(v => v.FieldTranslations.Cast<FieldTranslation>().Any(t => t.TranslationValue == fieldValue)).FirstOrDefault(); if (legalValueMatch != null) { newValue = legalValueMatch.LegalValue; } employee.SetMemberValue(fieldName, Convert.ChangeType(newValue, propertyInfo.PropertyType)); change.NewValue = Convert.ChangeType(newValue, propertyInfo.PropertyType); change.Status = FieldTranslatorChangeStatus.Changed; } else // If it cannot be autocorrected { if (legalValuesForFieldName.All(x => x.DisallowOtherValues == false) == true) // Ignore if if it will allow other values than the ones defined as a legal value { logChange = false; } // Else Use the following behaviour else if (Behaviour == FieldTranslatorBehaviour.KeepNonLegalValues) { change.Status = FieldTranslatorChangeStatus.Ignored; } else if (Behaviour == FieldTranslatorBehaviour.RemoveNonLegalValues) { employee.SetMemberValue(fieldName, Convert.ChangeType(null, propertyInfo.PropertyType)); change.Status = FieldTranslatorChangeStatus.Removed; } } if (this.CommitChanges == true) { employee.Save(); } if (logChange) { change.Employee = employee; Changes.Add(change); } if (this.CommitChanges == true && change.Status != FieldTranslatorChangeStatus.Ignored && change.Status != FieldTranslatorChangeStatus.None) { Customer currentCustomer = CustomerUtilities.CurrentCustomerFromSession(employee.Session); logEntry = new TranslationLogEntry(employee.Session) { PreviousValue = Convert.ToString(change.PreviousValue), FieldName = fieldName, NewValue = Convert.ToString(change.NewValue), Employee = employee, ChangeStatus = Convert.ToInt32(change.Status), Customer = currentCustomer }; logEntry.Save(); } } } } }