public static List<CampaignEmployeeUpdateRequestItem> GenerateFromCampaignForEmployee(Employee employee, ImportTask importTask)
        {
            List<CampaignEmployeeUpdateRequestItem> rc = new List<CampaignEmployeeUpdateRequestItem>();

            foreach (UpdateRequest updateRequest in importTask.UpdateRequestsForEmployee(employee))
            {
                CampaignEmployeeUpdateRequestItem item = new CampaignEmployeeUpdateRequestItem
                {
                    StepUrl = Utilities.Domain() + UrlManager.EmployeeEditWithRequestUpdateOid(updateRequest.Oid),
                    Step = updateRequest.Step,
                    LastSentDate = updateRequest.LastSentDate,
                };
                AddGeneratedValues(item, updateRequest);
                rc.Add(item);
            }

            return rc;
        }
        public static bool EmployeeIsDoneUpdating(this ImportTask importTask, Employee employee)
        {
            var rc = false;
            var lastCompletedRequest = importTask.LastCompletedUpdateRequestForEmployee(employee);

            if (lastCompletedRequest == null)
            {
                rc = false;
            }
            else if (lastCompletedRequest.Step < importTask.ImportTaskConfiguration.Steps())
            {
                rc = false;
            }
            else
            {
                rc = true;
            }

            return rc;
        }
        public static List<string> PropertiesAvailableForSelfUpdating()
        {
            var e = new Employee();

            return new List<string>()
            {
                Utilities.GetPropertyName(() => e.FirstName),
                Utilities.GetPropertyName(() => e.LastName),
                Utilities.GetPropertyName(() => e.Initials),
                Utilities.GetPropertyName(() => e.Title),
                Utilities.GetPropertyName(() => e.Address),
                Utilities.GetPropertyName(() => e.City),
                Utilities.GetPropertyName(() => e.PostCode),
                Utilities.GetPropertyName(() => e.StateProvince),
                Utilities.GetPropertyName(() => e.Country),
                Utilities.GetPropertyName(() => e.PhoneNumber),
                Utilities.GetPropertyName(() => e.AltPhoneNumber),
                Utilities.GetPropertyName(() => e.MobileNumber),
                Utilities.GetPropertyName(() => e.Company),
                Utilities.GetPropertyName(() => e.WebPage),
            };
        }
 public static int UpdateRequestStepCompletedForEmployee(this ImportTask importTask, Employee employee)
 {
     var matches = importTask.UpdateRequests.ToList<UpdateRequest>().Where(r => r.Employee.Oid == employee.Oid &&  r.CompletedDate != default(DateTime));
     return matches.Count() > 0 ? matches.Max(r => r.Step) : 0;
 }
 public static List<UpdateRequest> UpdateRequestsForEmployee(this ImportTask importTask, Employee employee)
 {
     return importTask.UpdateRequests.ToList<UpdateRequest>().Where(x => x.Employee.Oid == employee.Oid).ToList();
 }
 public static UpdateRequest UpdateRequestForEmployeeWithStep(this ImportTask importTask, int step, Employee employee)
 {
     return importTask.UpdateRequests.ToList<UpdateRequest>().Where(e => e.Step == step && e.Employee.Oid == employee.Oid).FirstOrDefault();
 }
        public static int NextRequiredStepForEmployee(this ImportTask importTask, Employee employee)
        {
            int nextStep = 0;
            int maxSteps = importTask.ImportTaskConfiguration.Steps();
            int completedStep = importTask.UpdateRequestStepCompletedForEmployee(employee);

            nextStep = completedStep + 1;

            if (nextStep > maxSteps)
            {
                nextStep = 0;
            }

            return nextStep;
        }
 public static UpdateRequest LastCompletedUpdateRequestForEmployee(this ImportTask importTask, Employee employee)
 {
     return importTask.UpdateRequests.ToList<UpdateRequest>().Where(x => x.Employee.Oid == employee.Oid && x.CompletedDate.HasBeenSet()).OrderByDescending(x => x.Step).FirstOrDefault();
 }
        public static int LastCompletedStepForEmployee(this ImportTask importTask, Employee employee)
        {
            int rc = 0;
            UpdateRequest lastCompletedUpdateRequest = importTask.LastCompletedUpdateRequestForEmployee(employee);

            if (lastCompletedUpdateRequest != null)
            {
                rc = lastCompletedUpdateRequest.Step;
            }

            return rc;
        }
        protected void SetUpdateRequestAsPreview()
        {
            Employee employee = new Employee() { FirstName = "Fred", LastName = "Jones" };

            importTask = new ImportTask()
            {
                ImportTaskConfiguration = ObjectFromQueryString<ImportTaskConfiguration>(session),
                FieldLegalValueConfiguration = ObjectFromQueryString<FieldLegalValueConfiguration>(session)
            };

            foreach (ImportTaskEmail item in importTask.ImportTaskConfiguration.ImportTaskEmails)
            {
                importTask.UpdateRequests.Add(new UpdateRequest
                {
                    Employee = employee,
                    Step = item.Step,
                    ImportTask = importTask
                });
            }

            SaveSession();
        }
        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();
                        }
                    }
                }
            }
        }