public static void DeleteCustomUrl(Guid customUrlId)
        {
            using (CustomUrlTableAdapter adapter = new CustomUrlTableAdapter())
            {
                using (MasterDataSet.CustomUrlDataTable table = adapter.GetCustomUrl(customUrlId))
                {
                    if (table.Count > 0)
                    {
                        MasterDataSet.CustomUrlRow row = table[0];

                        if (row.IsInstanceIdNull())
                        {
                            RemoveOrganizationCustomUrlFromCache(row.OrganizationId);
                        }
                        else
                        {
                            RemoveInstanceCustomUrlFromCache(row.InstanceId);
                        }

                        row.Delete();

                        adapter.Update(table);
                    }
                }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            m_UserContext = UserContext.Current;

            if (!Page.IsPostBack)
            {
                MasterDataSet.CustomUrlRow row = null;

                if (m_UserContext.InstanceId != Guid.Empty)
                {
                    row = CustomUrlProvider.GetCustomUrl(m_UserContext.OrganizationId, m_UserContext.InstanceId);
                }

                if (row == null)
                {
                    row = CustomUrlProvider.GetCustomUrlByOrganizationId(m_UserContext.OrganizationId);
                }

                if (row != null)
                {
                    VanityUrlTextBox.Text     = row.PartialCustomUrl.ToLowerInvariant();
                    FullVanityUrlTextBox.Text = row.FullCustomUrl.ToLowerInvariant();
                }
                if (ShowFullVanityUrl)
                {
                    TrFullCustomURL.Visible       = true;
                    PartialVanityUrlTitle.Visible = true;
                }
            }
        }
        protected void SimpleViewSaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Guid orgId  = m_UserContext.OrganizationId;
                    Guid?instId = null;
                    if (m_UserContext.InstanceId != Guid.Empty)
                    {
                        instId = m_UserContext.InstanceId;
                    }

                    MasterDataSet.CustomUrlRow row = null;

                    if (instId.HasValue)
                    {
                        row = CustomUrlProvider.GetCustomUrl(orgId, instId.Value);
                    }

                    if (row == null)
                    {
                        row = CustomUrlProvider.GetCustomUrlByOrganizationId(orgId);
                    }

                    if (row != null)
                    {
                        CustomUrlProvider.UpdateCustomUrl(row.CustomUrlId, ((ShowFullVanityUrl && !string.IsNullOrEmpty(FullVanityUrlTextBox.Text.Trim())) ? FullVanityUrlTextBox.Text.ToLowerInvariant() : null), VanityUrlTextBox.Text.ToLowerInvariant());
                    }
                    else
                    {
                        CustomUrlProvider.InsertCustomUrl(orgId, instId, ((ShowFullVanityUrl && !string.IsNullOrEmpty(FullVanityUrlTextBox.Text.Trim())) ? FullVanityUrlTextBox.Text.ToLowerInvariant() : null), VanityUrlTextBox.Text.ToLowerInvariant());
                    }

                    SimpleViewTitleLabel.Text = Resources.CustomUrlsControl_SimpleViewMessageLabel_Text;

                    if (SaveButtonClick != null)
                    {
                        SaveButtonClick(sender, e);
                    }
                }
                catch (ConstraintException ex)
                {
                    BaseControl.ShowError(ex, SimpleErrorPanel);
                }
            }
        }
        /// <summary>
        /// Checks if custom url is exists in system
        /// </summary>
        /// <param name="partialCustomUrl">Partial Custom Url with out domain</param>
        /// <returns></returns>
        public static bool ValidateCustomUrl(string partialCustomUrl)
        {
            if (!string.IsNullOrEmpty(partialCustomUrl))
            {
                using (CustomUrlTableAdapter adapter = new CustomUrlTableAdapter())
                {
                    using (MasterDataSet.CustomUrlDataTable table = adapter.GetCustomUrls(null, null, partialCustomUrl, partialCustomUrl))
                    {
                        if (table.Count > 0)
                        {
                            return(false);
                        }
                        else
                        {
                            Organization org     = null;
                            string       segment = RemoveSchemeFormUri(partialCustomUrl);
                            if (segment.Contains("-"))
                            {
                                org = OrganizationProvider.GetOrganizationByPseudoId(segment.Split('-')[0]);
                            }
                            else
                            {
                                org = OrganizationProvider.GetOrganizationByPseudoId(segment);
                            }

                            if (org == null)
                            {
                                MasterDataSet.CustomUrlRow row = GetCustomUrl(segment);
                                if (row != null)
                                {
                                    org = OrganizationProvider.GetOrganization(row.OrganizationId);
                                }
                            }

                            return(org == null);
                        }
                    }
                }
            }

            return(false);
        }
        protected void EditForm_DataBound(object sender, System.EventArgs e)
        {
            Guid?instanceId = null;

            if (EditForm.CurrentMode == DetailsViewMode.Edit)
            {
                MasterDataSet.CustomUrlRow row = EditForm.DataItem as MasterDataSet.CustomUrlRow;
                if (row != null)
                {
                    if (!row.IsInstanceIdNull())
                    {
                        instanceId = row.InstanceId;
                    }
                    if (instanceId.HasValue)
                    {
                        NameLabel.Visible   = false;
                        EditForm.ObjectName = Resources.CustomUrlsControl_EditForm_ObjectName_Instance;
                    }
                    else
                    {
                        NameLabel.Visible   = true;
                        NameLabel.Text      = OrganizationProvider.GetName(row.OrganizationId);
                        EditForm.ObjectName = Resources.CustomUrlsControl_EditForm_ObjectName_Organization;
                    }
                    this.SelectInstance(instanceId);
                }
            }

            if (EditForm.CurrentMode != DetailsViewMode.ReadOnly)
            {
                string adress = FrameworkConfiguration.Current.WebApplication.CustomUrl.PartialCustomUrlRootAddressesFirst;
                using (RadComboBoxItem item = new RadComboBoxItem(adress, adress))
                {
                    item.Selected = true;
                    RootAddressesList.Items.Add(item);
                }
            }
        }
        /// <summary>
        /// Parses specified host and returns the organization and instance.
        /// </summary>
        /// <param name="host">Host component of the URL.</param>
        /// <param name="organization">An organization.</param>
        /// <param name="instance">An instance.</param>
        public static void ParseHost(string host, ref Organization organization, ref Instance instance)
        {
            Guid[] values = GetCustomUrlFromCache(host);
            if (values != null)
            {
                organization = OrganizationProvider.GetOrganizationFromCache(values[0], true);

                if (values.Length > 1)
                {
                    instance = InstanceProvider.GetInstanceFromCache(values[1], values[0], true);
                }

                return;
            }

            MasterDataSet.CustomUrlRow row = GetCustomUrl(host);
            if (row != null)
            {
                organization = OrganizationProvider.GetOrganizationFromCache(row.OrganizationId, true);

                if (instance == null)
                {
                    if (!row.IsInstanceIdNull())
                    {
                        instance = InstanceProvider.GetInstanceFromCache(row.InstanceId, row.OrganizationId, true);
                    }
                }
            }
            else
            {
                string[] segments = host.ToLowerInvariant().Split('.');

                if (segments.Length < 2)
                {
                    return;
                }

                string segment = segments[0];

                if (string.Compare(segment, FrameworkConfiguration.Current.WebApplication.CustomUrl.AuthenticationTicketDomain, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return;
                }

                MasterDataSet.CustomUrlRow customUrlRow = null;
                string   instPseudoId = null;
                string[] pseudos      = segment.Split('-');

                if (pseudos.Length > 1)
                {
                    organization = OrganizationProvider.GetOrganizationByPseudoIdFromCache(pseudos[0]);
                    instPseudoId = pseudos[1];

                    if (organization == null)
                    {
                        customUrlRow = GetCustomUrl(pseudos[0]);
                        if (customUrlRow != null)
                        {
                            organization = OrganizationProvider.GetOrganizationFromCache(customUrlRow.OrganizationId, true);
                        }
                    }
                }
                else
                {
                    organization = OrganizationProvider.GetOrganizationByPseudoIdFromCache(segment);
                }

                if (organization == null)
                {
                    customUrlRow = GetCustomUrl(segment);
                    if (customUrlRow != null)
                    {
                        organization = OrganizationProvider.GetOrganizationFromCache(customUrlRow.OrganizationId, true);

                        if (instance == null)
                        {
                            if (!customUrlRow.IsInstanceIdNull())
                            {
                                instance = InstanceProvider.GetInstanceFromCache(customUrlRow.InstanceId, customUrlRow.OrganizationId, true);
                            }
                        }
                    }
                }

                if (organization != null)
                {
                    if (instance == null)
                    {
                        if (!string.IsNullOrEmpty(instPseudoId))
                        {
                            instance = InstanceProvider.GetInstanceByPseudoIdFromCache(instPseudoId, organization.OrganizationId);
                        }
                    }
                }
            }

            if (organization != null)
            {
                if (instance == null)
                {
                    values = new Guid[] { organization.OrganizationId }
                }
                ;
                else
                {
                    values = new Guid[] { organization.OrganizationId, instance.InstanceId }
                };
                PutCustomUrlToCache(host, values);
            }
        }
        /// <summary>
        /// Gets Custom url by organizationId and instanceId. If need only by organization use instanceId == Guid.Empty
        /// </summary>
        /// <param name="organizationId">Organization Id</param>
        /// <param name="instanceId">Instance Id</param>
        /// <returns>Custom Url</returns>
        public static string GetVanityUrl(Guid organizationId, Guid instanceId)
        {
            string customUrl = null;

            if (instanceId != Guid.Empty)
            {
                customUrl = GetInstanceCustomUrlFromCache(instanceId);
            }

            if (string.IsNullOrEmpty(customUrl))
            {
                customUrl = GetOrganizationCustomUrlFromCache(organizationId);
            }

            if (!string.IsNullOrEmpty(customUrl))
            {
                return(customUrl);
            }

            Organization org = OrganizationProvider.GetOrganizationFromCache(organizationId, true);

            if (org != null)
            {
                Instance inst = null;
                MasterDataSet.CustomUrlRow row = null;

                if (instanceId != Guid.Empty)
                {
                    row  = GetCustomUrl(organizationId, instanceId);
                    inst = InstanceProvider.GetInstanceFromCache(instanceId, organizationId, true);
                }

                if (row == null)
                {
                    row = GetCustomUrlByOrganizationId(organizationId);
                }

                CustomUrlElement customUrlSettings = FrameworkConfiguration.Current.WebApplication.CustomUrl;

                if (row != null)
                {
                    if (row.IsInstanceIdNull() && (inst != null))
                    {
                        customUrl = row.PartialCustomUrl + "-" + inst.PseudoId + "." + customUrlSettings.PartialCustomUrlRootAddressesFirst;

                        PutInstanceCustomUrlToCache(instanceId, customUrl);
                    }
                    else
                    {
                        if (customUrlSettings.PartialCustomUrlIsPrimary)
                        {
                            customUrl = row.PartialCustomUrl + "." + customUrlSettings.PartialCustomUrlRootAddressesFirst;
                        }
                        else
                        {
                            customUrl = (!string.IsNullOrEmpty(row.FullCustomUrl))
                                ? row.FullCustomUrl
                                : row.PartialCustomUrl + "." + customUrlSettings.PartialCustomUrlRootAddressesFirst;
                        }

                        if (row.IsInstanceIdNull())
                        {
                            PutOrganizationCustomUrlToCache(organizationId, customUrl);
                        }
                        else
                        {
                            PutInstanceCustomUrlToCache(row.InstanceId, customUrl);
                        }
                    }
                }
                else
                {
                    if (inst != null)
                    {
                        customUrl = org.PseudoId + "-" + inst.PseudoId + "." + customUrlSettings.PartialCustomUrlRootAddressesFirst;
                    }
                    else
                    {
                        customUrl = org.PseudoId + "." + customUrlSettings.PartialCustomUrlRootAddressesFirst;
                    }
                }
            }

            return(string.IsNullOrEmpty(customUrl) ? string.Empty : customUrl);
        }
        public static void UpdateCustomUrl(Guid customUrlId, string fullCustomUrl, string partialCustomUrl)
        {
            if (customUrlId == Guid.Empty)
            {
                return;
            }

            ValidatePartialCustomUrl(partialCustomUrl);

            using (CustomUrlTableAdapter adapter = new CustomUrlTableAdapter())
            {
                using (MasterDataSet.CustomUrlDataTable table = adapter.GetCustomUrls(null, null, fullCustomUrl, partialCustomUrl))
                {
                    MasterDataSet.CustomUrlRow row = null;
                    if (table.Count > 0)
                    {
                        if (table.Count == 1)
                        {
                            if (table[0].CustomUrlId == customUrlId)
                            {
                                row = table[0];
                            }
                            else
                            {
                                throw new ConstraintException(Resources.CustomUrlProvider_CustomUrlAlreadyExists);
                            }
                        }
                        else
                        {
                            throw new ConstraintException(Resources.CustomUrlProvider_CustomUrlAlreadyExists);
                        }
                    }

                    if (row == null)
                    {
                        row = GetCustomUrl(customUrlId);
                    }

                    if (!ValidateCustomUrl(fullCustomUrl) && !string.IsNullOrEmpty(fullCustomUrl) && (row != null && string.Compare(row.FullCustomUrl, fullCustomUrl, StringComparison.OrdinalIgnoreCase) != 0))
                    {
                        throw new ConstraintException(Resources.CustomUrlProvider_CustomUrlAlreadyExists);
                    }

                    if (!ValidateCustomUrl(partialCustomUrl) && !string.IsNullOrEmpty(partialCustomUrl) && (row != null && string.Compare(row.PartialCustomUrl, partialCustomUrl, StringComparison.OrdinalIgnoreCase) != 0))
                    {
                        throw new ConstraintException(Resources.CustomUrlProvider_CustomUrlAlreadyExists);
                    }

                    if (string.IsNullOrEmpty(fullCustomUrl))
                    {
                        fullCustomUrl = string.Empty;
                    }
                    if (string.IsNullOrEmpty(partialCustomUrl))
                    {
                        partialCustomUrl = string.Empty;
                    }

                    adapter.Update(customUrlId, fullCustomUrl, partialCustomUrl);

                    if (row.IsInstanceIdNull())
                    {
                        RemoveOrganizationCustomUrlFromCache(row.OrganizationId);
                    }
                    else
                    {
                        RemoveInstanceCustomUrlFromCache(row.InstanceId);
                    }
                }
            }
        }