public void DisableExchange(string companyCode)
        {
            ExchangePowershell powershell = null;
            CPDatabase         database   = null;

            try
            {
                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);

                // Disable all exchange objects
                powershell.DeleteAllMailboxes(companyCode);
                powershell.DeleteAllContacts(companyCode);
                powershell.DeleteAllGroups(companyCode);
                powershell.DeleteAddressBookPolicy(companyCode + " ABP");
                powershell.DeleteOfflineAddressBook(companyCode + " OAL");
                powershell.DeleteAddressList(companyCode + " - All Rooms");
                powershell.DeleteAddressList(companyCode + " - All Contacts");
                powershell.DeleteAddressList(companyCode + " - All Groups");
                powershell.DeleteAddressList(companyCode + " - All Users");
                powershell.DeleteGlobalAddressList(companyCode + " - GAL");

                // Get all accepted domains
                this.logger.Debug("Retrieving list of accepted domains for " + companyCode);

                database = new CPDatabase();
                var domains = from d in database.Domains
                              where d.IsAcceptedDomain
                              where d.CompanyCode == companyCode
                              select d;

                if (domains != null)
                {
                    foreach (Domain d in domains)
                    {
                        powershell.DeleteDomain(d.Domain1);
                    }
                }

                // Now update the database
                int r = database.DisableExchange(companyCode);
                this.logger.Debug("Total count returned when calling DisableExchange stored procedure: " + r.ToString());
            }
            catch (Exception ex)
            {
                this.logger.Error("Error disabling Exchange for company " + companyCode, ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                {
                    database.Dispose();
                }

                if (powershell != null)
                {
                    powershell.Dispose();
                }
            }
        }
        public void DeleteDomain(string domainName, string companyCode)
        {
            CPDatabase           database   = null;
            ADOrganizationalUnit adOrg      = null;
            ExchangePowershell   powershell = null;

            try
            {
                database = new CPDatabase();

                // Make sure no users groups or anything is using this domain
                var usersUsing = (from u in database.Users
                                  where (u.UserPrincipalName.EndsWith("@" + domainName) || u.Email.EndsWith("@" + domainName))
                                  select u).Count();

                if (usersUsing > 0)
                {
                    ThrowEvent(AlertID.FAILED, "The domain is in use " + domainName);
                }
                else
                {
                    // Make sure no groups are using this domain
                    var groupsUsing = (from g in database.DistributionGroups
                                       where g.Email.EndsWith("@" + domainName)
                                       select g).Count();

                    if (groupsUsing > 0)
                    {
                        ThrowEvent(AlertID.FAILED, "The domain is in use " + domainName);
                    }
                    else
                    {
                        // Since users & groups are not using this domain we can continue and remove it

                        // Get company distinguished name
                        var dn = (from d in database.Companies
                                  where !d.IsReseller
                                  where d.CompanyCode == companyCode
                                  select d.DistinguishedName).First();

                        // Delete domain from Active Directory
                        adOrg = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                        adOrg.RemoveDomain(dn, domainName);

                        // Get domain from SQL
                        var domain = (from d in database.Domains
                                      where d.Domain1 == domainName
                                      where d.CompanyCode == companyCode
                                      select d).First();

                        // Check if it was enabled for Exchange
                        if (domain.IsAcceptedDomain)
                        {
                            powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                            powershell.DeleteDomain(domain.Domain1);
                        }

                        database.Domains.Remove(domain);
                        database.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to remove domain " + domainName + " from company " + companyCode, ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (powershell != null)
                {
                    powershell.Dispose();
                }

                if (database != null)
                {
                    database.Dispose();
                }
            }
        }
        public void  UpdateDomain(string domainName, string companyCode, bool isDefault, bool isExchangeEnabled, DomainType domainType)
        {
            CPDatabase         database   = null;
            ExchangePowershell powershell = null;

            try
            {
                // Remove any whitespace characters at the beginning and end
                domainName = domainName.Trim();

                database = new CPDatabase();
                var defaultDomains = from d in database.Domains
                                     where d.CompanyCode == companyCode
                                     select d;

                foreach (Domain d in defaultDomains)
                {
                    if (d.Domain1.Equals(domainName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        // This is the domain we are updating
                        d.IsDefault  = isDefault;
                        d.DomainType = (int)domainType;

                        // Check if it wasn't an Exchange domain and we are making it an Exchange domain
                        if (!d.IsAcceptedDomain && isExchangeEnabled)
                        {
                            // Create accepted domain
                            powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                            powershell.NewDomain(domainName, domainType);
                            d.IsAcceptedDomain = true;
                        }
                        else if (d.IsAcceptedDomain && !isExchangeEnabled)
                        {
                            // Delete accepted domain
                            powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                            powershell.DeleteDomain(domainName);
                            d.IsAcceptedDomain = false;
                        }
                        else if (d.IsAcceptedDomain && isExchangeEnabled)
                        {
                            // Update accepted domain
                            powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                            powershell.UpdateDomain(domainName, domainType);
                            d.IsAcceptedDomain = true;
                        }
                    }
                    else
                    {
                        if (isDefault)
                        {
                            d.IsDefault = false;
                        }
                    }
                }

                database.SaveChanges();
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to update domain " + domainName + " for company " + companyCode, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                {
                    database.Dispose();
                }
            }
        }