/// <summary>
        /// Returns the rewritten URL for the given source URL
        /// </summary>
        /// <param name="sourceUrl">The given source URL</param>
        /// <returns>The rewritten URL</returns>
        public string RewriteUrl(string sourceUrl)
        {
            // VALIDATE INPUT
            if (string.IsNullOrEmpty(sourceUrl))
            {
                return(string.Empty);
            }

            // GET APPLICATION PATH
            string      appPath     = "/";
            HttpRequest httpRequest = HttpContextHelper.SafeGetRequest();

            if (httpRequest != null)
            {
                appPath = httpRequest.ApplicationPath;
                if (!appPath.EndsWith("/"))
                {
                    appPath += "/";
                }
            }

            // CHECK FOR CUSTOM URL
            string    appRelativeUrl = UrlHelper.GetAppRelativeUrl(sourceUrl, appPath);
            CustomUrl customUrl      = LoadCustomUrl(appRelativeUrl);

            if (customUrl != null)
            {
                string query = UrlHelper.GetQueryString(sourceUrl);
                return(DoCustomRewrite(appPath, customUrl, query));
            }
            else
            {
                return(DoDefaultRewrite(sourceUrl));
            }
        }
        public static CustomUrl Load(Int32 customUrlId, bool useCache)
        {
            if (customUrlId == 0)
            {
                return(null);
            }
            CustomUrl customUrl = null;
            string    key       = "CustomUrl_" + customUrlId.ToString();

            if (useCache)
            {
                customUrl = ContextCache.GetObject(key) as CustomUrl;
                if (customUrl != null)
                {
                    return(customUrl);
                }
            }
            customUrl = new CustomUrl();
            if (customUrl.Load(customUrlId))
            {
                if (useCache)
                {
                    ContextCache.SetObject(key, customUrl);
                }
                return(customUrl);
            }
            return(null);
        }
예제 #3
0
 /// <summary>
 /// Loads the given CustomUrl object from the given database data reader.
 /// </summary>
 /// <param name="customUrl">The CustomUrl object to load.</param>
 /// <param name="dr">The database data reader to read data from.</param>
 public static void LoadDataReader(CustomUrl customUrl, IDataReader dr)
 {
     //SET FIELDS FROM ROW DATA
     customUrl.CustomUrlId       = dr.GetInt32(0);
     customUrl.StoreId           = dr.GetInt32(1);
     customUrl.CatalogNodeId     = dr.GetInt32(2);
     customUrl.CatalogNodeTypeId = dr.GetByte(3);
     customUrl.Url     = dr.GetString(4);
     customUrl.IsDirty = false;
 }
        public static bool Delete(Int32 customUrlId)
        {
            CustomUrl customUrl = new CustomUrl();

            if (customUrl.Load(customUrlId))
            {
                return(customUrl.Delete());
            }
            return(false);
        }
        public static CustomUrlCollection LoadForStore(int maximumRows, int startRowIndex, string sortExpression)
        {
            int storeId = Token.Instance.StoreId;
            //CREATE THE DYNAMIC SQL TO LOAD OBJECT
            StringBuilder selectQuery = new StringBuilder();

            selectQuery.Append("SELECT");
            if (maximumRows > 0)
            {
                selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
            }
            selectQuery.Append(" " + CustomUrl.GetColumnNames(string.Empty));
            selectQuery.Append(" FROM ac_CustomUrls");
            selectQuery.Append(" WHERE StoreId = @storeId");
            if (!string.IsNullOrEmpty(sortExpression))
            {
                selectQuery.Append(" ORDER BY " + sortExpression);
            }
            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());

            database.AddInParameter(selectCommand, "@storeId", System.Data.DbType.Int32, storeId);
            //EXECUTE THE COMMAND
            CustomUrlCollection results = new CustomUrlCollection();
            int thisIndex = 0;
            int rowCount  = 0;

            using (IDataReader dr = database.ExecuteReader(selectCommand))
            {
                while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
                {
                    if (thisIndex >= startRowIndex)
                    {
                        CustomUrl customUrl = new CustomUrl();
                        CustomUrl.LoadDataReader(customUrl, dr);
                        results.Add(customUrl);
                        rowCount++;
                    }
                    thisIndex++;
                }
                dr.Close();
            }
            return(results);
        }
예제 #6
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.");
            }
        }
        public string DoCustomRewrite(string serverPath, CustomUrl customUrl, string query)
        {
            StringBuilder dynamicUrl  = new StringBuilder();
            string        displayPage = string.Empty;

            switch (customUrl.CatalogNodeType)
            {
            case CatalogNodeType.Category:
                displayPage = CatalogDataSource.GetDisplayPage(customUrl.CatalogNodeId, CatalogNodeType.Category);
                dynamicUrl.Append(serverPath);
                dynamicUrl.Append(displayPage);
                dynamicUrl.Append("?CategoryId=" + customUrl.CatalogNodeId);
                break;

            case CatalogNodeType.Product:
                displayPage = CatalogDataSource.GetDisplayPage(AlwaysConvert.ToInt(customUrl.CatalogNodeId), CatalogNodeType.Product);
                dynamicUrl.Append(serverPath);
                dynamicUrl.Append(displayPage);
                dynamicUrl.Append("?ProductId=" + customUrl.CatalogNodeId);
                break;

            case CatalogNodeType.Webpage:
                displayPage = CatalogDataSource.GetDisplayPage(customUrl.CatalogNodeId, CatalogNodeType.Webpage);
                dynamicUrl.Append(serverPath);
                dynamicUrl.Append(displayPage);
                dynamicUrl.Append("?WebpageId=" + customUrl.CatalogNodeId);
                break;

            case CatalogNodeType.Link:
                displayPage = CatalogDataSource.GetDisplayPage(customUrl.CatalogNodeId, CatalogNodeType.Link);
                dynamicUrl.Append(serverPath);
                dynamicUrl.Append(displayPage);
                dynamicUrl.Append("?LinkId=" + customUrl.CatalogNodeId);
                break;
            }
            //PRESERVE QUERY STRING
            if (!string.IsNullOrEmpty(query))
            {
                dynamicUrl.Append("&" + query);
            }
            return(dynamicUrl.ToString());
        }
        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);
        }
 public static SaveResult Insert(CustomUrl customUrl)
 {
     return(customUrl.Save());
 }
 public static SaveResult Update(CustomUrl customUrl)
 {
     return(customUrl.Save());
 }
 public static bool Delete(CustomUrl customUrl)
 {
     return(customUrl.Delete());
 }