Exemplo n.º 1
0
        public virtual SaveResult Save()
        {
            CustomUrl checkExists = CustomUrlDataSource.LoadCustomUrl(this.Url);

            if (checkExists == null || checkExists.CustomUrlId == this.CustomUrlId)
            {
                SaveResult   result   = this.BaseSave();
                IUrlRewriter rewriter = RewriteServiceLocator.GetInstance(Token.Instance.UrlRewriterSettings.Provider);
                if (rewriter != null)
                {
                    rewriter.ReloadCache();
                }
                return(result);
            }
            else
            {
                throw new InvalidOperationException("The value of Url is already defined in the database.  Url must be unique.");
            }
        }
        private CustomUrl LoadCustomUrl(string resourceUrl)
        {
            // MAKE SURE WE HAVE A VALID CACHE
            if (!_ValidCache)
            {
                this.InitializeCache();
            }

            // CHECK FOR CUSTOM URL IN CACHE
            string loweredSourceUrl = resourceUrl.ToLowerInvariant();

            if (this._CustomUrls.Contains(loweredSourceUrl))
            {
                // RETURN CACHED ITEM
                return(this._CustomUrls[loweredSourceUrl]);
            }
            // SEE IF WE HAVE MORE CUSTOM URLS IN DATABASE
            if (this._CacheSize < this._CustomUrlCount)
            {
                // TRY TO FIND CUSTOM URL BY QUERY
                CustomUrl customUrl = CustomUrlDataSource.LoadCustomUrl(loweredSourceUrl);
                if (customUrl != null)
                {
                    // CUSTOM URL FOUND, KICK OUT THE LAST ITEM IN CACHE
                    this._CustomUrls.RemoveAt(this._CustomUrls.Count - 1);

                    // PUT THE NEW ITEM TO THE TOP OF CACHE
                    this._CustomUrls.Insert(0, customUrl);

                    // RETURN THE NEW CUSTOM URL
                    return(customUrl);
                }
            }

            // NO REWRITE FOUND FOR THE GIVEN URL
            return(null);
        }