ProductSearchIndexBuilder.cs :  » Content-Management-Systems-CMS » mojoPortal » WebStore » UI » Helpers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » mojoPortal 
mojoPortal » WebStore » UI » Helpers » ProductSearchIndexBuilder.cs
// Author:              Joe Audette
// Created:                2008-12-11
// Last Modified:          2009-07-22
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.
// 

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Threading;
using log4net;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using WebStore.Business;
using mojoPortal.Web;

namespace WebStore.UI.Helpers{
    /// <summary>
    ///Indexes the products into the search index.
    /// </summary>
    public class ProductSearchIndexBuilder : IndexBuilderProvider
    {
        private static readonly ILog log
            = LogManager.GetLogger(typeof(ProductSearchIndexBuilder));

        private const string tabScript = "<script type=\"text/javascript\" > var myTabs = new YAHOO.widget.TabView(\"productdetails\"); </script>";

        /// <summary>
        /// This method is called when the site index is rebuilt
        /// </summary>
        /// <param name="pageSettings"></param>
        /// <param name="indexPath"></param>
        public override void RebuildIndex(
            PageSettings pageSettings,
            string indexPath)
        {
            if (WebConfigSettings.DisableSearchIndex) { return; }
            if (pageSettings == null)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("pageSettings object passed to ProductSearchIndexBuilder.RebuildIndex was null");
                }
                return;
            }

            //don't index pending/unpublished pages
            if (pageSettings.IsPending) { return; }

            log.Info("ProductSearchIndexBuilder indexing page - "
                + pageSettings.PageName);


            Guid webStoreFeatureGuid = new Guid("0cefbf18-56de-11dc-8f36-bac755d89593");
            ModuleDefinition webStoreFeature = new ModuleDefinition(webStoreFeatureGuid);

            List<PageModule> pageModules
                    = PageModule.GetPageModulesByPage(pageSettings.PageId);

            // adding a try catch here because this is invoked even for non-implemented db platforms and it causes an error
            try
            {
                DataTable dataTable
                    = Product.GetBySitePage(
                    pageSettings.SiteId,
                    pageSettings.PageId);

                foreach (DataRow row in dataTable.Rows)
                {
                    IndexItem indexItem = new IndexItem();
                    indexItem.SiteId = pageSettings.SiteId;
                    indexItem.PageId = pageSettings.PageId;
                    indexItem.PageName = pageSettings.PageName;
                    indexItem.ViewRoles = pageSettings.AuthorizedRoles;
                    indexItem.ModuleViewRoles = row["ViewRoles"].ToString();
                    indexItem.FeatureId = webStoreFeatureGuid.ToString();
                    indexItem.FeatureName = webStoreFeature.FeatureName;
                    indexItem.FeatureResourceFile = webStoreFeature.ResourceFile;

                    indexItem.ItemKey = row["Guid"].ToString();
                    indexItem.ModuleId = Convert.ToInt32(row["ModuleID"], CultureInfo.InvariantCulture);
                    indexItem.ModuleTitle = row["ModuleTitle"].ToString();
                    indexItem.Title = row["Name"].ToString();
                    indexItem.ViewPage = row["Url"].ToString().Replace("/", string.Empty);
                    if (indexItem.ViewPage.Length > 0)
                    {
                        indexItem.UseQueryStringParams = false;
                    }
                    else
                    {
                        indexItem.ViewPage = "WebStore/ProductDetail.aspx";
                    }

                    indexItem.PageMetaDescription = row["MetaDescription"].ToString();
                    indexItem.PageMetaKeywords = row["MetaKeywords"].ToString();

                    indexItem.Content = row["Abstract"].ToString()
                        + " " + row["Description"].ToString()
                        + " " + row["MetaDescription"].ToString()
                        + " " + row["MetaKeywords"].ToString();


                    // lookup publish dates
                    foreach (PageModule pageModule in pageModules)
                    {
                        if (indexItem.ModuleId == pageModule.ModuleId)
                        {
                            indexItem.PublishBeginDate = pageModule.PublishBeginDate;
                            indexItem.PublishEndDate = pageModule.PublishEndDate;
                        }
                    }

                    IndexHelper.RebuildIndex(indexItem, indexPath);
                

                    if (log.IsDebugEnabled) log.Debug("Indexed " + indexItem.Title);

                }

            }
            catch { }
            


        }


        public override void ContentChangedHandler(
            object sender,
            ContentChangedEventArgs e)
        {
            if (WebConfigSettings.DisableSearchIndex) { return; }
            if (sender == null) return;
            if (!(sender is Product)) return;

            Product product = sender as Product;
            
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            product.SiteId = siteSettings.SiteId;
            product.SearchIndexPath = IndexHelper.GetSearchIndexPath(siteSettings.SiteId);

            if (e.IsDeleted)
            {
                Store store = new Store(product.StoreGuid);
                // get list of pages where this module is published
                List<PageModule> pageModules
                    = PageModule.GetPageModulesByModule(store.ModuleId);

                foreach (PageModule pageModule in pageModules)
                {
                    IndexHelper.RemoveIndexItem(
                        pageModule.PageId,
                        store.ModuleId,
                        product.Guid.ToString());
                }
            }
            else
            {
                if (ThreadPool.QueueUserWorkItem(new WaitCallback(IndexItem), product))
                {
                    if (log.IsDebugEnabled) log.Debug("ProductSearchIndexBuilder.IndexItem queued");
                }
                else
                {
                    if (log.IsErrorEnabled) log.Error("Failed to queue a thread for ProductSearchIndexBuilder.IndexItem");
                }

            }

        }

        private static void IndexItem(object o)
        {
            if (WebConfigSettings.DisableSearchIndex) { return; }
            if (o == null) return;
            if (!(o is Product)) return;

            Product content = o as Product;
            IndexItem(content);

        }


        private static void IndexItem(Product product)
        {
            if (WebConfigSettings.DisableSearchIndex) { return; }
            if (product == null)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("product object passed to ProductSearchIndexBuilder.IndexItem was null");
                }
                return;
            }

            Store store = new Store(product.StoreGuid);

            Module module = new Module(store.ModuleId);
            Guid webStoreFeatureGuid = new Guid("0cefbf18-56de-11dc-8f36-bac755d89593");
            ModuleDefinition webStoreFeature = new ModuleDefinition(webStoreFeatureGuid);
            
            //// get list of pages where this module is published
            List<PageModule> pageModules
                = PageModule.GetPageModulesByModule(store.ModuleId);

            foreach (PageModule pageModule in pageModules)
            {
                PageSettings pageSettings
                    = new PageSettings(
                    product.SiteId,
                    pageModule.PageId);

                //don't index pending/unpublished pages
                if (pageSettings.IsPending) { continue; }

                IndexItem indexItem = new IndexItem();
                if (product.SearchIndexPath.Length > 0)
                {
                    indexItem.IndexPath = product.SearchIndexPath;
                }
                indexItem.SiteId = product.SiteId;
                indexItem.PageId = pageSettings.PageId;
                indexItem.PageName = pageSettings.PageName;
                indexItem.ViewRoles = pageSettings.AuthorizedRoles;
                indexItem.ModuleViewRoles = module.ViewRoles;
                if (product.Url.Length > 0)
                {
                    indexItem.ViewPage = product.Url.Replace("~/", string.Empty);
                    indexItem.UseQueryStringParams = false;
                }
                else
                {
                    indexItem.ViewPage = "/WebStore/ProductDetail.aspx";
                }
                indexItem.ItemKey = product.Guid.ToString();
                indexItem.ModuleId = store.ModuleId;
                indexItem.ModuleTitle = module.ModuleTitle;
                indexItem.Title = product.Name;
                indexItem.PageMetaDescription = product.MetaDescription;
                indexItem.PageMetaKeywords = product.MetaKeywords;

                indexItem.Content 
                    = product.Teaser 
                    + " " + product.Description.Replace(tabScript, string.Empty) 
                    + " " + product.MetaDescription 
                    + " " + product.MetaKeywords;

                indexItem.FeatureId = webStoreFeatureGuid.ToString();
                indexItem.FeatureName = webStoreFeature.FeatureName;
                indexItem.FeatureResourceFile = webStoreFeature.ResourceFile;
                indexItem.PublishBeginDate = pageModule.PublishBeginDate;
                indexItem.PublishEndDate = pageModule.PublishEndDate;

                IndexHelper.RebuildIndex(indexItem);
            }

            if (log.IsDebugEnabled) log.Debug("Indexed " + product.Name);

        }


    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.