public static void SendLogReport(ref Logging log, string identifier, Item userSyncItem)
        {
            var recipient = userSyncItem["Mail Recipients"];
            if (String.IsNullOrEmpty(recipient))
            {
                log.Log("Error", "The 'Mail Recipients' field must be defined. Please provide a recipient address for the mail.");
            }
            else
            {
                var replyTo = userSyncItem["Mail Reply To"];
                if (String.IsNullOrEmpty(replyTo))
                {
                    replyTo = "*****@*****.**";
                    log.Log("Error", "The 'Mail Reply To' field must be defined. Please provide a replyTo address for the mail.");
                }
                var subject = userSyncItem["Mail Subject"];
                if (String.IsNullOrEmpty(subject))
                {
                    subject = "Status report mail from UserSync task - {0} - Result: {1}.";
                }

                var logString = log.LogBuilder.ToString();
                var result = string.Empty;
                if (String.IsNullOrEmpty(logString))
                {
                    result = Success;
                    logString += "The import completed successfully.\r\n\r\nStatus:\r\n" + log.GetStatusText();
                }
                else
                {
                    logString = "The import failed.\r\n\r\nStatus:\r\n" + log.GetStatusText() + "\r\n\r\n" + logString;
                    result = Failure;
                }
                try
                {
                    subject = String.Format(subject, identifier, result);
                }
                catch (Exception exception)
                {
                    log.Log("Error", "SendLogReport had an exception trying to format the subject of the mail." + exception.Message);
                }
                var doNotSendMailOnSuccess = userSyncItem["Do Not Send Mail On Success"] == "1";
                if((doNotSendMailOnSuccess && result == Failure) || !doNotSendMailOnSuccess)
                {
                    try{
                        if (SendMail.SendMailWithoutAttachment(recipient, replyTo, subject, logString) == "Failure")
                        {
                            log.Log("Error", "The SendMailWithoutAttachment failed.");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Log("Error", "The SendMailWithoutAttachment failed with an exception: " + ex.Message + " " + ex.StackTrace);
                    }
                }
            }
        }
        public Logging RunUserSyncJob(Item userSyncItem, ref Logging LogBuilder)
        {
            string errorMessage = String.Empty;

            var map = InstantiateDataMap(userSyncItem, ref errorMessage);
            if (!String.IsNullOrEmpty(errorMessage))
            {
                LogBuilder.Log("Error", errorMessage);
            }
            if (map != null)
            {
                map.Process();
            }
            return LogBuilder;
        }
        protected BaseDataMap(Database db, Item importItem, Logging logging)
        {
            ImportItem = importItem;
            LogBuilder = logging;

            //setup import details
            SitecoreDB = db;
            // Defines the data and datasource for the import
            DataSourceString = importItem["Data Source"];
            Query = importItem["Query"];

            Int32.TryParse(importItem["Minimum Number Of Rows Required To Run The Import"],
                           out _MinimumNumberOfRowsRequiredToRunTheImport);
            if (MinimumNumberOfRowsRequiredToRunTheImport < 0)
            {
                MinimumNumberOfRowsRequiredToRunTheImport = 0;
            }
            IsValidateImportDataBeforeProcessing = importItem["Validate Import Data Before Processing"] == "1";
            IsInValidationAllowDuplicatesInKey = importItem["In Validation Allow Duplicates In Key"] == "1";

            int limitNumberOfImportRowsToWhatNumber;
            if (Int32.TryParse(importItem["Limit Number Of ImportRows To What Number"],
                               out limitNumberOfImportRowsToWhatNumber))
            {
                LimitNumberOfImportRowsToWhatNumber = limitNumberOfImportRowsToWhatNumber;
            }
            else
            {
                LimitNumberOfImportRowsToWhatNumber = 0;
            }
            if (LimitNumberOfImportRowsToWhatNumber < 0)
            {
                LimitNumberOfImportRowsToWhatNumber = 0;
            }

            int startOnWhatImportRowIndex;
            if (Int32.TryParse(importItem["Start On What ImportRow Index"],
                               out startOnWhatImportRowIndex))
            {
                StartOnWhatImportRowIndex = startOnWhatImportRowIndex;
            }
            else
            {
                StartOnWhatImportRowIndex = 0;
            }
            if (StartOnWhatImportRowIndex < 0)
            {
                StartOnWhatImportRowIndex = 0;
            }

            //more properties
            GetUsernameFromWhatField = importItem.Fields["Get Username From What Field"].Value;
            if (String.IsNullOrEmpty(GetUsernameFromWhatField))
            {
                LogBuilder.Log("Error",
                               String.Format(
                                   "The field '{0}' must contain a value that indicates which field to retrieve the username from.",
                                   "Get Username From What Field"));
            }

            InitializeIdentifyUserUniqueByFieldDefinition(importItem);

            InitializeUserKeyHandler();

            var createUserInWhatSecurityDomainString = importItem.Fields[FieldCreateUserInWhatSecurityDomain].Value;
            if (!String.IsNullOrEmpty(createUserInWhatSecurityDomainString))
            {
                if (DomainManager.DomainExists(createUserInWhatSecurityDomainString))
                {
                    var createUserInWhatSecurityDomain = DomainManager.GetDomain(createUserInWhatSecurityDomainString);
                    if (createUserInWhatSecurityDomain != null)
                    {
                        CreateUserInWhatSecurityDomain = createUserInWhatSecurityDomain;
                    }
                    else
                    {
                        LogBuilder.Log("Error",
                                       String.Format(
                                           "The initialization of the property CreateUserInWhatSecurityDomain failed. The security domain created was null. The field: '{0}' contains the value  '{1}'.",
                                           FieldCreateUserInWhatSecurityDomain, createUserInWhatSecurityDomainString));
                    }
                }
                else
                {
                    LogBuilder.Log("Error",
                                   String.Format(
                                       "The initialization of the property CreateUserInWhatSecurityDomain failed. The security domain provided does not exist. The field: '{0}' contains the value '{1}'.",
                                       FieldCreateUserInWhatSecurityDomain, createUserInWhatSecurityDomainString));
                }
            }
            else
            {
                LogBuilder.Log("Error",
                               String.Format(
                                   "The initialization of the property CreateUserInWhatSecurityDomain failed. The value provided in the field: '{0}' was null or empty. Value: '{1}'.",
                                   FieldCreateUserInWhatSecurityDomain, createUserInWhatSecurityDomainString));
            }

            string addUserToWhatStandardRoles = importItem.Fields[FieldAddUserToWhatStandardRoles].Value;
            string errorMessage;
            AddUserToWhatStandardRoles = InitializeRolesListFromPipeseperatedString(FieldAddUserToWhatStandardRoles,
                                                                                    addUserToWhatStandardRoles,
                                                                                    out errorMessage);
            if (!String.IsNullOrEmpty(errorMessage))
            {
                LogBuilder.Log("Error", errorMessage);
            }

            GetPasswordFromWhatField = importItem.Fields["Get Password From What Field"].Value;
            AutogeneratePasswordWithLength = 7;
            var autogeneratePasswordWithLength = importItem.Fields["Autogenerate Password With Length"].Value;
            if (!String.IsNullOrEmpty(autogeneratePasswordWithLength))
            {
                int length;
                if (!Int32.TryParse(autogeneratePasswordWithLength, out length))
                {
                    LogBuilder.Log("Error",
                                   String.Format(
                                       "The field '{0}' must contain a integer that states the length of the password. Value: {1}.",
                                       "Autogenerate Password With Length", autogeneratePasswordWithLength));
                }
                else
                {
                    AutogeneratePasswordWithLength = length;
                }
            }

            // Users Present In Import
            string onPresentInImportAddUserToRoles = importItem.Fields[FieldOnPresentInImportAddToRoles].Value;
            OnPresentInImportAddToRoles = InitializeRolesListFromPipeseperatedString(FieldOnPresentInImportAddToRoles,
                                                                                     onPresentInImportAddUserToRoles,
                                                                                     out errorMessage, true);
            if (!String.IsNullOrEmpty(errorMessage))
            {
                LogBuilder.Log("Error", errorMessage);
            }

            string onPresentInImportRemoveUserFromRoles = importItem.Fields[FieldOnPresentInImportRemoveFromRoles].Value;
            OnPresentInImportRemoveFromRoles =
                InitializeRolesListFromPipeseperatedString(FieldOnPresentInImportRemoveFromRoles,
                                                           onPresentInImportRemoveUserFromRoles, out errorMessage);
            if (!String.IsNullOrEmpty(errorMessage))
            {
                LogBuilder.Log("Error", errorMessage);
            }

            // Users Not Present In Import
            IsProcessUsersNotPresentInImport = importItem["Process Users Not Present In Import"] == "1";

            string onNotPresentInImportAddToRoles = importItem.Fields[FieldOnNotPresentInImportAddToRoles].Value;
            OnNotPresentInImportAddToRoles =
                InitializeRolesListFromPipeseperatedString(FieldOnNotPresentInImportAddToRoles,
                                                           onNotPresentInImportAddToRoles, out errorMessage);
            if (!String.IsNullOrEmpty(errorMessage))
            {
                LogBuilder.Log("Error", errorMessage);
            }

            string onNotPresentInImportRemoveFromRoles =
                importItem.Fields[FieldOnNotPresentInImportRemoveFromRoles].Value;
            OnNotPresentInImportRemoveFromRoles =
                InitializeRolesListFromPipeseperatedString(FieldOnNotPresentInImportRemoveFromRoles,
                                                           onNotPresentInImportRemoveFromRoles, out errorMessage);
            if (!String.IsNullOrEmpty(errorMessage))
            {
                LogBuilder.Log("Error", errorMessage);
            }

            IsDoNotLogProgressStatusMessagesInSitecoreLog =
                importItem["Do Not Log Progress Status Messages In Sitecore Log"] == "1";

            SetProfileItemOnUser = importItem["Set ProfileItem On User"] == "1";
            UseWhatProfileItemId = importItem.Fields["Use What ProfileItemId"].Value;
            CheckThatCustomPropertyExistOnUserProfile = importItem["Check That Property Exist On User Profile"] == "1";

            IsDeleteUsersWithMembershipInStandardRolesOnly =
                importItem["Delete Users With Membership In Standard Roles Only"] == "1";

            //start handling fields
            Item fieldsRootItem = GetItemByTemplate(importItem, Utility.Constants.FieldsFolderID);
            if (fieldsRootItem.IsNotNull())
            {
                ChildList c = fieldsRootItem.GetChildren();
                if (c.Any())
                {
                    foreach (Item child in c)
                    {
                        var fieldDefinition = CreateFieldDefinition(child);
                        if (fieldDefinition != null)
                        {
                            FieldDefinitions.Add(fieldDefinition);
                        }
                    }
                }
                else
                {
                    LogBuilder.Log("Warn", "There are no fields to import");
                }
            }
            else
            {
                LogBuilder.Log("Warn", "There is no 'Fields' folder");
            }
        }