Exemplo n.º 1
0
        public static ResultObject CreateBlackBerryUser(int itemId, int accountId)
        {
            ResultObject res = TaskManager.StartResultTask <ResultObject>("BLACBERRY", "CREATE_BLACKBERRY_USER");


            bool isBlackBerryUser;

            try
            {
                isBlackBerryUser = DataProvider.CheckBlackBerryUserExists(accountId);
            }
            catch (Exception ex)
            {
                TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.CANNOT_CHECK_IF_BLACKBERRY_USER_EXISTS, ex);
                return(res);
            }

            if (isBlackBerryUser)
            {
                TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.USER_IS_ALREADY_BLAKBERRY_USER);
                return(res);
            }

            OrganizationUser user;

            try
            {
                user = OrganizationController.GetAccount(itemId, accountId);

                if (user == null)
                {
                    throw new ApplicationException(
                              string.Format("User is null. ItemId={0}, AccountId={1}", itemId,
                                            accountId));
                }
            }
            catch (Exception ex)
            {
                TaskManager.CompleteResultTask(res, ErrorCodes.CANNOT_GET_ACCOUNT, ex);
                return(res);
            }


            if (user.AccountType != ExchangeAccountType.Mailbox)
            {
                TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.ACCOUNT_TYPE_IS_NOT_MAILBOX);
                return(res);
            }

            try
            {
                user = OrganizationController.GetUserGeneralSettings(itemId, accountId);
                if (string.IsNullOrEmpty(user.FirstName))
                {
                    TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.USER_FIRST_NAME_IS_NOT_SPECIFIED);
                    return(res);
                }
                if (string.IsNullOrEmpty(user.LastName))
                {
                    TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.USER_LAST_NAME_IS_NOT_SPECIFIED);
                    return(res);
                }
            }
            catch (Exception ex)
            {
                TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.CANNOT_GET_USER_GENERAL_SETTINGS, ex);
                return(res);
            }

            BlackBerry blackBerry;

            try
            {
                blackBerry = GetBlackBerryProxy(itemId);
            }
            catch (Exception ex)
            {
                TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.CANNOT_GET_BLACKBERRY_PROXY, ex);
                return(res);
            }

            try
            {
                bool quota = CheckQuota(itemId);
                if (!quota)
                {
                    TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.USER_QUOTA_HAS_BEEN_REACHED);
                    return(res);
                }
            }
            catch (Exception ex)
            {
                TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.CANNOT_CHECK_QUOTA, ex);
                return(res);
            }

            try
            {
                ResultObject userRes = blackBerry.CreateBlackBerryUser(user.PrimaryEmailAddress);
                res.ErrorCodes.AddRange(userRes.ErrorCodes);
                if (!userRes.IsSuccess)
                {
                    TaskManager.CompleteResultTask(res);
                    return(res);
                }
            }
            catch (Exception ex)
            {
                TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.CANNOT_ADD_BLACKBERRY_USER, ex);
                return(res);
            }

            try
            {
                DataProvider.AddBlackBerryUser(accountId);
            }
            catch (Exception ex)
            {
                TaskManager.CompleteResultTask(res, BlackBerryErrorsCodes.CANNOT_ADD_BLACKBERRY_USER_TO_DATABASE, ex);
                return(res);
            }

            TaskManager.CompleteResultTask();
            return(res);
        }