示例#1
0
        public void DoMigrate(bool isPreview)
        {
            bgwlist.Clear();
            if (isServer)
            {
                if (CurrentCOSSelection == -1)
                {
                    CurrentCOSSelection = 0;
                }
                UsersViewModel usersViewModel =
                    ((UsersViewModel)ViewModelPtrs[(int)ViewType.USERS]);

                if (ZimbraValues.zimbraValues.AuthToken.Length == 0)
                {
                    MessageBox.Show("You must log on to the Zimbra server", "Zimbra Migration",
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                ConfigViewModelS sourceModel =
                    ((ConfigViewModelS)ViewModelPtrs[(int)ViewType.SVRSRC]);

                if (!sourceModel.IsMailServerInitialized)
                {
                    MessageBox.Show("You must log on to Exchange", "Zimbra Migration",
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                string          domainName  = usersViewModel.ZimbraDomain;
                string          defaultPWD  = DefaultPWD;
                string          tempMessage = "";
                bool            bProvision  = false;
                MessageBoxImage mbi         = MessageBoxImage.Information;

                for (int i = 0; i < SchedList.Count; i++)
                {
                    string userName = (usersViewModel.UsersList[i].MappedName.Length > 0) ?
                                      usersViewModel.UsersList[i].MappedName :
                                      usersViewModel.UsersList[i].Username;
                    string accountName = userName + "@" + domainName;

                    if (!SchedList[i].isProvisioned)
                    {
                        if (!isPreview)
                        {
                            bProvision = true;
                            if (defaultPWD.Length == 0)
                            {
                                MessageBox.Show("Please provide an initial password",
                                                "Zimbra Migration", MessageBoxButton.OK,
                                                MessageBoxImage.Exclamation);
                                return;
                            }

                            string    cosID     = CosList[CurrentCOSSelection].CosID;
                            ZimbraAPI zimbraAPI = new ZimbraAPI(isServer);

                            // FBS bug 71646 -- 3/26/12
                            string displayName = "";
                            string givenName   = "";
                            string sn          = "";
                            string zfp         = "";

                            // FBS bug 73395 -- 4/25/12
                            ObjectPickerInfo opinfo = usersViewModel.GetOPInfo();
                            if (opinfo.DisplayName.Length > 0)
                            {
                                displayName = opinfo.DisplayName;
                            }
                            if (opinfo.GivenName.Length > 0)
                            {
                                givenName = opinfo.GivenName;
                            }
                            if (opinfo.Sn.Length > 0)
                            {
                                sn = opinfo.Sn;
                            }
                            if (opinfo.Zfp.Length > 0)
                            {
                                zfp = opinfo.Zfp;
                            }
                            // end 73395
                            // end 71646

                            string historyfile = Path.GetTempPath() + accountName.Substring(0, accountName.IndexOf('@')) + "history.log";
                            if (File.Exists(historyfile))
                            {
                                try
                                {
                                    File.Delete(historyfile);
                                }
                                catch (Exception e)
                                {
                                    string msg = "exception in deleteing the Histroy file " + e.Message;
                                    System.Console.WriteLine(msg);
                                }
                            }

                            bool mustChangePW = usersViewModel.UsersList[i].MustChangePassword;
                            if (zimbraAPI.CreateAccount(accountName, displayName, givenName, sn, zfp, defaultPWD, mustChangePW, cosID) == 0)
                            {
                                tempMessage += string.Format("{0} Provisioned", userName) + "\n";
                                // MessageBox.Show(string.Format("{0} Provisioned", userName), "Zimbra Migration", MessageBoxButton.OK, MessageBoxImage.Information);
                            }
                            else
                            {
                                // MessageBox.Show(string.Format("Provision unsuccessful for {0}: {1}", userName, zimbraAPI.LastError), "Zimbra Migration", MessageBoxButton.OK, MessageBoxImage.Error);
                                tempMessage += string.Format("Provision unsuccessful for {0}: {1}",
                                                             userName, zimbraAPI.LastError) + "\n";
                                mbi = MessageBoxImage.Error;
                            }
                        }
                    }
                }
                if (bProvision)
                {
                    MessageBox.Show(tempMessage, "Zimbra Migration", MessageBoxButton.OK, mbi);
                }
                if (mbi == MessageBoxImage.Error)
                {
                    return;
                }
                lb.SelectedIndex = 6;
            }
            else
            {
                lb.SelectedIndex = 4;
            }

            AccountResultsViewModel accountResultsViewModel =
                ((AccountResultsViewModel)ViewModelPtrs[(int)ViewType.RESULTS]);

            accountResultsViewModel.AccountResultsList.Clear();
            if (isServer)
            {
                EnableMigrate = false;
                EnablePreview = false;
            }
            else
            {
                ((OptionsViewModel)ViewModelPtrs[(int)ViewType.OPTIONS]).OEnableNext = false;
            }
            accountResultsViewModel.EnableStop = !EnableMigrate;

            int num = 0;

            foreach (SchedUser su in SchedList)
            {
                accountResultsViewModel.AccountResultsList.Add(new AccountResultsViewModel(this,
                                                                                           num++, 0, "", "", su.username, 0, "", 0, 0,
                                                                                           accountResultsViewModel.EnableStop));
            }
            accountResultsViewModel.OpenLogFileEnabled = true;

            // FBS bug 71048 -- 4/16/12 -- use the correct number of threads.
            // If MaxThreadCount not specified, default to 4.  If fewer users than MaxThreadCount, numThreads = numUsers
            OptionsViewModel ovm = ((OptionsViewModel)ViewModelPtrs[(int)ViewType.OPTIONS]);
            int maxThreads       = (ovm.MaxThreadCount > 0) ? ovm.MaxThreadCount : 4;

            maxThreads = Math.Min(maxThreads, 8); // let's make 8 the limit for now
            int numUsers   = SchedList.Count;
            int numThreads = Math.Min(numUsers, maxThreads);

            for (int i = 0; i < numUsers; i++)
            {
                Log.info("Schedule bachground workers with numusers :" + numUsers + " and maxthreads are :" + numThreads);
                if (i < numThreads)
                {
                    UserBW bgw = new UserBW(i);
                    bgw.DoWork          += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
                    bgw.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(
                        worker_ProgressChanged);
                    bgw.WorkerReportsProgress      = true;
                    bgw.WorkerSupportsCancellation = true;
                    bgw.RunWorkerCompleted        += new System.ComponentModel.RunWorkerCompletedEventHandler(
                        worker_RunWorkerCompleted);
                    bgw.usernum = i;
                    bgw.RunWorkerAsync(i);
                    bgwlist.Add(bgw);
                    Log.info("Background worker started number :", bgw.threadnum);
                }
                else
                {
                    Log.info("adding user number to overflow list", i);
                    overflowList.Add(i);
                }
            }
            ;
        }