internal override ResultObject DeleteBlackBerryUserInternal(string primaryEmailAddress)
        {
            ResultObject res = HostedSolutionLog.StartLog <ResultObject>("DeleteBlackBerry5UserInternal");

            string file = Path.Combine(UtilityPath, "besuseradminclient.exe");

            if (!File.Exists(file))
            {
                HostedSolutionLog.EndLog("DeleteBlackBerry5User", res, BlackBerryErrorsCodes.FILE_PATH_IS_INVALID);
                return(res);
            }

            string arguments = string.Format("-username {0} -password {1} -delete -u {2} -b {3} -n {4}",
                                             User,
                                             Password,
                                             primaryEmailAddress,
                                             EnterpriseServer,
                                             EnterpriseServerFQDN);

            try
            {
                string output;
                int    exitCode = Execute(file, arguments, out output);
                if (exitCode == 0)
                {
                    Log.WriteInfo(output);
                }
                else
                {
                    throw new ApplicationException(
                              string.Format("Exit code is not 0. {0}, ExitCode = {1}", output, exitCode));
                }

                for (int i = 0; i < 10; i++)
                {
                    BlackBerryUserDeleteState states = GetBlackBerryUserState(primaryEmailAddress);
                    if (states != BlackBerryUserDeleteState.Pending)
                    {
                        break;
                    }
                    Thread.Sleep(10000);
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("DeleteBlackBerry5UserInternal", res, BlackBerryErrorsCodes.CANNOT_EXECUTE_COMMAND, ex);
                return(res);
            }

            HostedSolutionLog.EndLog("DeleteBlackBerry5UserInternal");
            return(res);
        }
        internal override ResultObject SetEmailActivationPasswordInternal(string primaryEmailAddress)
        {
            ResultObject res = HostedSolutionLog.StartLog <ResultObject>("SetEmailActivationPassword");

            ResultObject removeRes = RemoveEmailActivationPassword(primaryEmailAddress);

            res.ErrorCodes.AddRange(removeRes.ErrorCodes);

            if (!removeRes.IsSuccess)
            {
                HostedSolutionLog.EndLog("SetEmailActivationPassword", res);
                return(res);
            }

            string file = Path.Combine(UtilityPath, "besuseradminclient.exe");

            if (!File.Exists(file))
            {
                HostedSolutionLog.EndLog("SetEmailActivationPassword", res, BlackBerryErrorsCodes.FILE_PATH_IS_INVALID);
                return(res);
            }

            string arguments = string.Format("-username {0} -password {1}  -change -u {2}  -b {3} -wrandom -n {4}",
                                             User,
                                             Password,
                                             primaryEmailAddress,
                                             EnterpriseServer,
                                             EnterpriseServerFQDN);

            try
            {
                string output;
                int    exitCode = Execute(file, arguments, out output);
                if (exitCode == 0)
                {
                    Log.WriteInfo(output);
                }
                else
                {
                    throw new ApplicationException(
                              string.Format("Exit code is not 0. {0}, ExitCode = {1}", output, exitCode));
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("SetEmailActivationPassword", res, BlackBerryErrorsCodes.CANNOT_EXECUTE_COMMAND, ex);
                return(res);
            }

            HostedSolutionLog.EndLog("SetEmailActivationPassword");
            return(res);
        }
        internal override BlackBerryUserStatsResult GetBlackBerryUserStatsInternal(string primaryEmailAddress)
        {
            BlackBerryUserStatsResult res =
                HostedSolutionLog.StartLog <BlackBerryUserStatsResult>("GetBlackBerry5UserStatsInternal");

            string[] keys;
            string[] values;

            ResultObject tempRes = GetBlackBerryUserData(primaryEmailAddress, out keys, out values);

            res.ErrorCodes.AddRange(tempRes.ErrorCodes);
            if (!res.IsSuccess)
            {
                HostedSolutionLog.EndLog("GetBlackBerry5UserStatsInternal", res);
                return(res);
            }

            try
            {
                List <BlackBerryStatsItem> items = new List <BlackBerryStatsItem>();

                int[] inds = new int[] { 3, 4, 5, 7, 8, 10, 11, 12, 13, 14, 15, 25 };

                foreach (int i in inds)
                {
                    if (keys.Length > i && values.Length > i)
                    {
                        items.Add(new BlackBerryStatsItem()
                        {
                            Name = keys[i], Value = values[i]
                        });
                    }
                }


                res.Value = items;
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("GetBlackBerry5UserStatsInternal", res, BlackBerryErrorsCodes.CANNOT_POPULATE_STATS, ex);
                return(res);
            }

            HostedSolutionLog.EndLog("GetBlackBerry5UserStatsInternal");
            return(res);
        }
        internal virtual ResultObject SetActivationPasswordWithExpirationTimeInternal(string primaryEmailAddress, string password, int time)
        {
            ResultObject res = HostedSolutionLog.StartLog <ResultObject>("SetActivationPasswordWithExpirationTimeInternal");

            string file = Path.Combine(UtilityPath, "besuseradminclient.exe");

            if (!File.Exists(file))
            {
                HostedSolutionLog.EndLog("SetActivationPasswordWithExpirationTimeInternal", res, BlackBerryErrorsCodes.FILE_PATH_IS_INVALID);
                return(res);
            }

            string arguments = string.Format("-change  -u {0} -p {1} -b {2} -n {3} -w {4} -wt {5}",
                                             primaryEmailAddress,
                                             Password,
                                             EnterpriseServer,
                                             AdministrationToolService,
                                             password,
                                             time);

            try
            {
                string output;
                int    exitCode = Execute(file, arguments, out output);
                if (exitCode == 0)
                {
                    Log.WriteInfo(output);
                }
                else
                {
                    throw new ApplicationException(
                              string.Format("Exit code is not 0. {0}, ExitCode = {1}", output, exitCode));
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("SetActivationPasswordWithExpirationTimeInternal", res, BlackBerryErrorsCodes.CANNOT_EXECUTE_COMMAND, ex);
                return(res);
            }

            HostedSolutionLog.EndLog("SetActivationPasswordWithExpirationTimeInternal");
            return(res);
        }
        internal override ResultObject CreateBlackBerryUserInternal(string primaryEmailAddress)
        {
            ResultObject res = HostedSolutionLog.StartLog <ResultObject>("CreateBlackBerryUser5Internal");

            string file = Path.Combine(UtilityPath, "besuseradminclient.exe");

            if (!File.Exists(file))
            {
                HostedSolutionLog.EndLog("CreateBlackBerry5UserInternal", res, BlackBerryErrorsCodes.FILE_PATH_IS_INVALID);
                return(res);
            }

            string arguments = string.Format("-username {0} -password {1} -add -u {2} -b {3}",
                                             User,
                                             Password,
                                             primaryEmailAddress,
                                             EnterpriseServer);

            try
            {
                string output;

                int exitCode = Execute(file, arguments, out output);
                if (exitCode == 0)
                {
                    Log.WriteInfo(output);
                }
                else
                {
                    throw new ApplicationException(
                              string.Format("Excit code is not 0. {0}, ExitCode = {1}", output, exitCode));
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("CreateBlackBerry5UserInternal", res, BlackBerryErrorsCodes.CANNOT_EXECUTE_COMMAND, ex);
                return(res);
            }

            HostedSolutionLog.EndLog("CreateBlackBerry5UserInternal");
            return(res);
        }
        protected virtual ResultObject GetBlackBerryUserData(string primaryEmailAddress, out string [] keys, out string [] values)
        {
            BlackBerryUserStatsResult res =
                HostedSolutionLog.StartLog <BlackBerryUserStatsResult>("GetBlackBerryUserData");


            string file = Path.Combine(UtilityPath, "besuseradminclient.exe");

            if (!File.Exists(file))
            {
                HostedSolutionLog.EndLog("GetBlackBerryUserData", res, BlackBerryErrorsCodes.FILE_PATH_IS_INVALID);
                keys   = null;
                values = null;

                return(res);
            }
            string arguments = string.Format("-stats -u {0} -p {1} -b {2} -n {3}",
                                             primaryEmailAddress,
                                             Password,
                                             EnterpriseServer,
                                             AdministrationToolService);

            string output;
            string error;

            try
            {
                int exitCode = Execute(file, arguments, out output, out error);
                if (exitCode == 0)
                {
                    Log.WriteInfo(output);
                }
                else
                {
                    throw new ApplicationException(
                              string.Format("Exit code is not 0. {0}, ExitCode = {1}", error, exitCode));
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("GetBlackBerryUserData", res, BlackBerryErrorsCodes.CANNOT_EXECUTE_COMMAND, ex);
                keys   = null;
                values = null;
                return(res);
            }

            try
            {
                string[] data  = output.Split('\n');
                Regex    regex = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

                keys   = regex.Split(data[0]);
                values = regex.Split(data[1]);
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("GetBlackBerryUserData", res, BlackBerryErrorsCodes.CANNOT_SPLIT_STATS, ex);
                keys   = null;
                values = null;
                return(res);
            }

            HostedSolutionLog.EndLog("GetBlackBerryUserData");
            return(res);
        }
        protected override ResultObject GetBlackBerryUserData(string primaryEmailAddress, out string[] keys, out string[] values)
        {
            BlackBerryUserStatsResult res =
                HostedSolutionLog.StartLog <BlackBerryUserStatsResult>("GetBlackBerry5UserData");


            string file = Path.Combine(UtilityPath, "besuseradminclient.exe");

            if (!File.Exists(file))
            {
                HostedSolutionLog.EndLog("GetBlackBerry5UserData", res, BlackBerryErrorsCodes.FILE_PATH_IS_INVALID);
                keys   = null;
                values = null;

                return(res);
            }
            string arguments = string.Format(" -username {0} -password {1} -stats -u {2} -b {3}",
                                             User,
                                             Password,
                                             primaryEmailAddress,
                                             EnterpriseServer);

            string output;
            string error;

            try
            {
                int exitCode = Execute(file, arguments, out output, out error);
                if (exitCode == 0)
                {
                    Log.WriteInfo(output);
                }
                else
                {
                    throw new ApplicationException(
                              string.Format("Exit code is not 0. {0}, ExitCode = {1}", error, exitCode));
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("GetBlackBerry5UserData", res, BlackBerryErrorsCodes.CANNOT_EXECUTE_COMMAND, ex);
                keys   = null;
                values = null;
                return(res);
            }

            try
            {
                string[] data  = output.Split('\n');
                Regex    regex = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

                int startRow = 0;

                if (data.Length > 0)
                {
                    for (startRow = data.Length - 1; startRow >= 0; startRow--)
                    {
                        if (!string.IsNullOrEmpty(data[startRow]) && !string.IsNullOrEmpty(data[startRow].Trim()))
                        {
                            break;
                        }
                    }
                }


                keys   = regex.Split(data[startRow - 1]);
                values = regex.Split(data[startRow]);
            }
            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("GetBlackBerry5UserData", res, BlackBerryErrorsCodes.CANNOT_SPLIT_STATS, ex);
                keys   = null;
                values = null;
                return(res);
            }

            HostedSolutionLog.EndLog("GetBlackBerry5UserData");
            return(res);
        }
        internal override ResultObject CreateBlackBerryUserInternal(string primaryEmailAddress)
        {
            ResultObject res = HostedSolutionLog.StartLog <ResultObject>("CreateBlackBerryUser5Internal");

            string file  = Path.Combine(UtilityPath, "besuseradminclient.exe");
            string file2 = Path.Combine(HandheldcleanupPath, "handheldcleanup.exe");

            //Add user to Blackberry Server
            if (!File.Exists(file))
            {
                HostedSolutionLog.EndLog("CreateBlackBerry5UserInternal", res, BlackBerryErrorsCodes.FILE_PATH_IS_INVALID);
                return(res);
            }

            string arguments = string.Format("-username {0} -password {1} -add -u {2} -b {3} -n {4}",
                                             User,
                                             Password,
                                             primaryEmailAddress,
                                             EnterpriseServer,
                                             EnterpriseServerFQDN);

            //run besuseradminclient.exe

            try
            {
                string output;

                int exitCode = Execute(file, arguments, out output);
                if (exitCode == 0)
                {
                    Log.WriteInfo(output);
                }
                else
                {
                    throw new ApplicationException(
                              string.Format("Exit code is not 0. {0}, ExitCode = {1}", output, exitCode));
                }
            }

            catch (Exception ex)
            {
                HostedSolutionLog.EndLog("CreateBlackBerry5UserInternal", res, BlackBerryErrorsCodes.CANNOT_EXECUTE_COMMAND, ex);
                return(res);
            }

            //run handheldcleanup.exe
            if (File.Exists(file2))
            {
                string arguments2 = string.Format("-u -p {0}",
                                                  MAPIProfile);
                try
                {
                    string output;

                    int exitCode = Execute2(file2, arguments2, out output);
                    if (exitCode == 0)
                    {
                        Log.WriteInfo(output);
                    }
                    else
                    {
                        throw new ApplicationException(
                                  string.Format("Exit code is not 0. {0}, ExitCode = {1}", arguments2, output, exitCode));
                    }
                }

                catch (Exception ex)
                {
                    HostedSolutionLog.EndLog("CreateBlackBerry5UserInternal", res, BlackBerryErrorsCodes.CANNOT_EXECUTE_COMMAND_HANDHELDCLEANUP, ex);
                    return(res);
                }
            }
            HostedSolutionLog.EndLog("CreateBlackBerry5UserInternal");
            return(res);
        }