private async void BtnCreateGroup_OnClick(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(TxtGroupAlias.Text))
            {
                if (DlGroupMigrationViewModel.Instance.IsAliasValid)
                {
                    var credManager = new CredentialManager();
                    var credentials = credManager.GetUserCredentials();

                    if (credentials != null)
                    {
                        if (DlGroupMigrationViewModel.Instance.CurrentlyActiveDl == null ||
                            DlGroupMigrationViewModel.Instance.CurrentlyActiveDl.Members.Count == 0)
                        {
                            var dialogResult =
                                ModernDialog.ShowMessage("This will just create a group with no members. Proceed?",
                                    "Hummingbird", MessageBoxButton.YesNo);

                            if (dialogResult == MessageBoxResult.Yes)
                            {
                                DlGroupMigrationViewModel.Instance.BackupControlsEnabled = false;
                                DlGroupMigrationViewModel.Instance.RestoreControlsEnabled = false;
                                TxtBackupStatus.Text = "creating group...";

                                var groupCreated = await CreateGroup(credentials, TxtGroupAlias.Text, DlGroupMigrationViewModel.Instance.CurrentlyActiveDl.Owner);

                                if (!string.IsNullOrWhiteSpace(groupCreated))
                                {
                                    ModernDialog.ShowMessage(string.Format("Your group was created successfully. SMTP address: {0}", groupCreated), "Hummingbird",
                                        MessageBoxButton.OK);
                                }

                                DlGroupMigrationViewModel.Instance.BackupControlsEnabled = true;
                                DlGroupMigrationViewModel.Instance.RestoreControlsEnabled = true;
                            }
                        }
                        else
                        {
                            DlGroupMigrationViewModel.Instance.BackupControlsEnabled = false;
                            DlGroupMigrationViewModel.Instance.RestoreControlsEnabled = false;
                            TxtBackupStatus.Text = "creating group...";
                            bool operationFailed = false;
                            List<string> invalidMembers = new List<string>();

                            var groupCreated = await CreateGroup(credentials, TxtGroupAlias.Text, DlGroupMigrationViewModel.Instance.CurrentlyActiveDl.Owner);

                            if (string.IsNullOrEmpty(groupCreated))
                            {
                                operationFailed = true;
                                ModernDialog.ShowMessage(string.Format("The group couldn't be created. Please try migrating again later."), "Hummingbird",
                                                MessageBoxButton.OK);
                            }
                            else
                            {
                                TxtBackupStatus.Text = "adding members...";
                                Collection<string> members = DlGroupMigrationViewModel.Instance.CurrentlyActiveDl.Members;

                                for (int i = 0; i < members.Count; i += DlGroupMigrationMain.MemberAddBatchSize)
                                {
                                    IEnumerable<string> membersToAddInABatch = members.Skip(i).Take(DlGroupMigrationMain.MemberAddBatchSize);
                                    var membersAddedResponse = await AddMembersToGroup(credentials, groupCreated, membersToAddInABatch);

                                    if (membersAddedResponse != null && membersAddedResponse.Body.SetUnifiedGroupMembershipResponseMessage != null)
                                    {
                                        if (membersAddedResponse.Body.SetUnifiedGroupMembershipResponseMessage.ResponseCode.ToLower() == "noerror")
                                        {
                                            continue;
                                        }
                                        else if (membersAddedResponse.Body.SetUnifiedGroupMembershipResponseMessage.ResponseCode.ToLower() == "errorinvalidid" &&
                                            membersAddedResponse.Body.SetUnifiedGroupMembershipResponseMessage.FailedMembers.Count == 0)
                                        {
                                            invalidMembers.AddRange(membersAddedResponse.Body.SetUnifiedGroupMembershipResponseMessage.InvalidMembers);
                                            continue;
                                        }
                                        else
                                        {
                                            operationFailed = true;
                                        }
                                    }
                                    else
                                    {
                                        operationFailed = true;
                                    }

                                    if (operationFailed)
                                    {
                                        ModernDialog.ShowMessage(string.Format(@"The group was created, but not all the members were added. Please go to 'bulk add' to add the missing members using the original backup and the following group address {0}.", groupCreated), "Hummingbird",
                                            MessageBoxButton.OK);
                                        break;
                                    }
                                }
                            }

                            if (!operationFailed)
                            {
                                if (invalidMembers.Count == 0)
                                {
                                    ModernDialog.ShowMessage(string.Format("Your group was created successfully. SMTP address: {0}.", groupCreated), "Hummingbird",
                                                MessageBoxButton.OK);
                                }
                                else
                                {
                                    ModernDialog.ShowMessage("The group was created but invalid members weren't added.", "Hummingbird",
                                                MessageBoxButton.OK);

                                    var fsOperator = new FileSystemOperator();
                                    AddMembersErrorDetails error = new AddMembersErrorDetails();
                                    error.InvalidMembers = invalidMembers;
                                    var filePath = fsOperator.StoreDistributionListFailures(DlGroupMigrationViewModel.Instance.CurrentlyActiveDl, "Create", error);

                                    if (!string.IsNullOrWhiteSpace(filePath))
                                    {
                                        var result =
                                            ModernDialog.ShowMessage(
                                                "The list of invalid members was created. Do you want to open File Explorer to find its location?",
                                                "Hummingbird", MessageBoxButton.YesNo);

                                        if (result == MessageBoxResult.Yes)
                                        {
                                            Process.Start("explorer.exe", string.Concat("/select, ", filePath));
                                        }
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(groupCreated))
                            {
                                var fsOperator = new FileSystemOperator();
                                DlToGroupMapping mapping = new DlToGroupMapping();
                                mapping.GroupSMTPAddress = groupCreated;
                                mapping.DlAlias = this.aliasOfSelectedDl;
                                fsOperator.StoreGroupDetails(mapping);
                            }

                            DlGroupMigrationViewModel.Instance.BackupControlsEnabled = true;
                            DlGroupMigrationViewModel.Instance.RestoreControlsEnabled = true;
                        }
                    }
                    else
                    {
                        ModernDialog.ShowMessage("No credentials were provided. Open Settings and add your credentials.", "Hummingbird",
                            MessageBoxButton.OK);
                    }
                }
                else
                {
                    ModernDialog.ShowMessage("The group couldn't be created from the backup file. The alias may be invalid or there might be a problem connecting to the server. Please try again later.",
                        "Hummingbird",
                         MessageBoxButton.OK);
                }
            }
            else
            {
                ModernDialog.ShowMessage("You must include an alias.", "Hummingbird", MessageBoxButton.OK);
            }

            DlGroupMigrationViewModel.Instance.CurrentlyActiveDl = null;
            DlGroupMigrationViewModel.Instance.IsAliasValid = false;
            TxtPath.Text = string.Empty;
        }
Пример #2
0
        /// <summary>
        /// Stores the DL alias and groups SMTP in a file.
        /// </summary>
        /// <param name="dlName">Name of the DL</param>
        /// <returns>Path of the file.</returns>
        internal string StoreGroupDetails(DlToGroupMapping mapping)
        {
            string path;

            try
            {
                var directory = Directory.CreateDirectory(AppPath);

                var serializer = new XmlSerializer(typeof(DlToGroupMapping));
                path = Path.Combine(directory.FullName, "DlToGroupMapping.xmldl");

                using (var writer = new StreamWriter(path))
                {
                    serializer.Serialize(writer, mapping);
                }

                LoggingViewModel.Instance.Logger.Write(string.Concat("StoreGroupDetails:OK", path,
                    Environment.NewLine,
                    mapping.GroupSMTPAddress));
            }
            catch (Exception exception)
            {
                LoggingViewModel.Instance.Logger.Write(string.Concat("StoreGroupDetails:Error",
                    exception.Message, Environment.NewLine,
                    exception.StackTrace,
                    Environment.NewLine,
                    mapping.GroupSMTPAddress));

                path = string.Empty;
            }

            return path;
        }