/// <summary>
 /// AddUserRegistration method implementation
 /// </summary>
 internal static Registration AddUserRegistration(Registration reg, bool resetkey = true, bool canupdate = true, bool email = false)
 {
     EnsureService();
     return(RuntimeRepository.AddUserRegistration(Config, reg, resetkey, canupdate, email));
 }
        /// <summary>
        /// DoImport method implementation
        /// </summary>
        public override bool DoImport()
        {
            if (Config == null)
            {
                throw new ArgumentNullException("Config", "MFA Configuration must be passed to the import process !");
            }
            if (!File.Exists(FileName))
            {
                throw new ArgumentException("Invalid FileName for import process !", "FileName");
            }

            var contents = File.ReadAllText(FileName).Split('\n');
            var csv      = from line in contents select line.Split(';').ToArray();

            var header = csv.First(r => r.Length > 1 && r.Last().Trim().Length > 0);

            int?[] ids = GetCSVHeadersId(header);

            var listen = InitializeTrace(FileName);

            try
            {
                Trace.WriteLine("");
                Trace.WriteLine(string.Format("Importing file : {0}", FileName));
                Trace.Indent();
                foreach (var row in csv.Skip(1).TakeWhile(r => r.Length > 1 && r.Last().Trim().Length > 0))
                {
                    Trace.TraceInformation("Importing record N° {0}", (RecordsCount + 1).ToString());
                    try
                    {
                        Registration reg = new Registration();
                        if ((ids[0].HasValue) && (!string.IsNullOrEmpty(row[ids[0].Value])))
                        {
                            reg.UPN = row[ids[0].Value];
                        }
                        else
                        {
                            throw new InvalidDataException("upn must be provided !");
                        }

                        if ((ids[1].HasValue) && (!string.IsNullOrEmpty(row[ids[1].Value])))
                        {
                            reg.MailAddress = row[ids[1].Value];
                        }
                        else if (Config.MailProvider.Enabled)
                        {
                            throw new InvalidDataException("email must be provided !");
                        }

                        if ((ids[2].HasValue) && (!string.IsNullOrEmpty(row[ids[2].Value])))
                        {
                            reg.PhoneNumber = row[ids[2].Value];
                        }
                        else if (Config.ExternalProvider.Enabled)
                        {
                            throw new InvalidDataException("mobile must be provided !");
                        }

                        if ((ids[3].HasValue) && (!string.IsNullOrEmpty(row[ids[3].Value])))
                        {
                            reg.PreferredMethod = (PreferredMethod)Enum.Parse(typeof(PreferredMethod), row[ids[3].Value]);
                        }
                        else
                        {
                            reg.PreferredMethod = PreferredMethod.Choose;
                        }

                        if (DisableAll)
                        {
                            reg.Enabled = false;
                        }
                        else if ((ids[4].HasValue) && (!string.IsNullOrEmpty(row[ids[4].Value])))
                        {
                            reg.Enabled = bool.Parse(row[ids[4].Value]);
                        }
                        else
                        {
                            reg.Enabled = true;
                        }

                        RuntimeRepository.AddUserRegistration(Config, reg, ForceNewKey, true, SendEmail); // Can also Update
                        Trace.TraceInformation("Record N° {0} imported for user : {1} !", (RecordsCount + 1).ToString(), reg.UPN);
                    }
                    catch (Exception ex)
                    {
                        ErrorsCount++;
                        Trace.TraceError("Error importing Record N° {0} \r\r {1}", (RecordsCount + 1).ToString(), ex.Message);
                    }
                    finally
                    {
                        RecordsCount++;
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                return(false);
            }
            finally
            {
                Trace.Unindent();
                FinalizeTrace(listen);
            }
            return(true);
        }
        /// <summary>
        /// DoImport method implementation
        /// </summary>
        public override bool DoImport()
        {
            if (Config == null)
            {
                throw new ArgumentNullException("Config", "MFA Configuration must be passed to the import process !");
            }
            if (!File.Exists(FileName))
            {
                throw new ArgumentException("Invalid FileName for import process !", "FileName");
            }

            var xml    = XDocument.Load(FileName);
            var listen = InitializeTrace(FileName);

            try
            {
                Trace.WriteLine("");
                Trace.WriteLine(string.Format("Importing file : {0}", FileName));
                Trace.Indent();

                foreach (var row in xml.Root.Descendants("User"))
                {
                    Trace.TraceInformation("Importing record N° {0}", (RecordsCount + 1).ToString());
                    try
                    {
                        Registration reg = new Registration();
                        if (row.Attribute("upn") != null)
                        {
                            reg.UPN = row.Attribute("upn").Value;
                        }
                        else
                        {
                            throw new InvalidDataException("upn must be provided !");
                        }

                        if (row.Attribute("email") != null)
                        {
                            reg.MailAddress = row.Attribute("email").Value;
                        }
                        else if (Config.MailProvider.Enabled)
                        {
                            throw new InvalidDataException("email must be provided !");
                        }

                        if (row.Attribute("mobile") != null)
                        {
                            reg.PhoneNumber = row.Attribute("mobile").Value;
                        }
                        else if (Config.ExternalProvider.Enabled)
                        {
                            throw new InvalidDataException("mobile must be provided !");
                        }

                        if (row.Attribute("method") != null)
                        {
                            reg.PreferredMethod = (PreferredMethod)Enum.Parse(typeof(PreferredMethod), row.Attribute("method").Value);
                        }
                        else
                        {
                            reg.PreferredMethod = PreferredMethod.Choose;
                        }

                        if (DisableAll)
                        {
                            reg.Enabled = false;
                        }
                        else if (row.Attribute("enabled") != null)
                        {
                            reg.Enabled = bool.Parse(row.Attribute("enabled").Value);
                        }
                        else
                        {
                            reg.Enabled = true;
                        }

                        RuntimeRepository.AddUserRegistration(Config, reg, ForceNewKey, true, SendEmail);
                        Trace.TraceInformation("Record N° {0} imported for user : {1} !", (RecordsCount + 1).ToString(), reg.UPN);
                    }
                    catch (Exception ex)
                    {
                        ErrorsCount++;
                        Trace.TraceError("Error importing Record N° {0} \r\r {1}", (RecordsCount + 1).ToString(), ex.Message);
                    }
                    finally
                    {
                        RecordsCount++;
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                return(false);
            }
            finally
            {
                Trace.Unindent();
                FinalizeTrace(listen);
            }
            return(true);
        }
 /// <summary>
 /// AddUserRegistration method implementation
 /// </summary>
 internal static Registration AddUserRegistration(Registration reg, bool addkey = true, bool noemail = false)
 {
     EnsureService();
     return(RuntimeRepository.AddUserRegistration(Config, reg, addkey, noemail));
 }