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();
                }
            }
        }