PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/WebStore.UI/Helpers/ProductSearchIndexBuilder.cs

#
C# | 270 lines | 194 code | 50 blank | 26 comment | 27 complexity | 2bf909b019dbceb32a199bf89bbd06ab MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. // Author: Joe Audette
  2. // Created: 2008-12-11
  3. // Last Modified: 2009-07-22
  4. //
  5. // The use and distribution terms for this software are covered by the
  6. // Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. // which can be found in the file CPL.TXT at the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by
  9. // the terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Data;
  16. using System.Globalization;
  17. using System.Text;
  18. using System.Threading;
  19. using log4net;
  20. using mojoPortal.Business;
  21. using mojoPortal.Business.WebHelpers;
  22. using WebStore.Business;
  23. using mojoPortal.Web;
  24. namespace WebStore.UI.Helpers
  25. {
  26. /// <summary>
  27. ///Indexes the products into the search index.
  28. /// </summary>
  29. public class ProductSearchIndexBuilder : IndexBuilderProvider
  30. {
  31. private static readonly ILog log = LogManager.GetLogger(typeof(ProductSearchIndexBuilder));
  32. private static bool debugLog = log.IsDebugEnabled;
  33. private const string tabScript = "<script type=\"text/javascript\" > var myTabs = new YAHOO.widget.TabView(\"productdetails\"); </script>";
  34. /// <summary>
  35. /// This method is called when the site index is rebuilt
  36. /// </summary>
  37. /// <param name="pageSettings"></param>
  38. /// <param name="indexPath"></param>
  39. public override void RebuildIndex(
  40. PageSettings pageSettings,
  41. string indexPath)
  42. {
  43. if (WebConfigSettings.DisableSearchIndex) { return; }
  44. if (pageSettings == null)
  45. {
  46. log.Error("pageSettings object passed to ProductSearchIndexBuilder.RebuildIndex was null");
  47. return;
  48. }
  49. //don't index pending/unpublished pages
  50. if (pageSettings.IsPending) { return; }
  51. log.Info("ProductSearchIndexBuilder indexing page - "
  52. + pageSettings.PageName);
  53. Guid webStoreFeatureGuid = new Guid("0cefbf18-56de-11dc-8f36-bac755d89593");
  54. ModuleDefinition webStoreFeature = new ModuleDefinition(webStoreFeatureGuid);
  55. List<PageModule> pageModules
  56. = PageModule.GetPageModulesByPage(pageSettings.PageId);
  57. // adding a try catch here because this is invoked even for non-implemented db platforms and it causes an error
  58. try
  59. {
  60. DataTable dataTable
  61. = Product.GetBySitePage(
  62. pageSettings.SiteId,
  63. pageSettings.PageId);
  64. foreach (DataRow row in dataTable.Rows)
  65. {
  66. IndexItem indexItem = new IndexItem();
  67. indexItem.SiteId = pageSettings.SiteId;
  68. indexItem.PageId = pageSettings.PageId;
  69. indexItem.PageName = pageSettings.PageName;
  70. indexItem.ViewRoles = pageSettings.AuthorizedRoles;
  71. indexItem.ModuleViewRoles = row["ViewRoles"].ToString();
  72. indexItem.FeatureId = webStoreFeatureGuid.ToString();
  73. indexItem.FeatureName = webStoreFeature.FeatureName;
  74. indexItem.FeatureResourceFile = webStoreFeature.ResourceFile;
  75. indexItem.ItemKey = row["Guid"].ToString();
  76. indexItem.ModuleId = Convert.ToInt32(row["ModuleID"], CultureInfo.InvariantCulture);
  77. indexItem.ModuleTitle = row["ModuleTitle"].ToString();
  78. indexItem.Title = row["Name"].ToString();
  79. indexItem.ViewPage = row["Url"].ToString().Replace("/", string.Empty);
  80. if (indexItem.ViewPage.Length > 0)
  81. {
  82. indexItem.UseQueryStringParams = false;
  83. }
  84. else
  85. {
  86. indexItem.ViewPage = "WebStore/ProductDetail.aspx";
  87. }
  88. indexItem.PageMetaDescription = row["MetaDescription"].ToString();
  89. indexItem.PageMetaKeywords = row["MetaKeywords"].ToString();
  90. indexItem.Content = row["Abstract"].ToString()
  91. + " " + row["Description"].ToString()
  92. + " " + row["MetaDescription"].ToString()
  93. + " " + row["MetaKeywords"].ToString();
  94. // lookup publish dates
  95. foreach (PageModule pageModule in pageModules)
  96. {
  97. if (indexItem.ModuleId == pageModule.ModuleId)
  98. {
  99. indexItem.PublishBeginDate = pageModule.PublishBeginDate;
  100. indexItem.PublishEndDate = pageModule.PublishEndDate;
  101. }
  102. }
  103. IndexHelper.RebuildIndex(indexItem, indexPath);
  104. if (debugLog) log.Debug("Indexed " + indexItem.Title);
  105. }
  106. }
  107. catch { }
  108. }
  109. public override void ContentChangedHandler(
  110. object sender,
  111. ContentChangedEventArgs e)
  112. {
  113. if (WebConfigSettings.DisableSearchIndex) { return; }
  114. if (sender == null) return;
  115. if (!(sender is Product)) return;
  116. Product product = sender as Product;
  117. SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
  118. product.SiteId = siteSettings.SiteId;
  119. product.SearchIndexPath = IndexHelper.GetSearchIndexPath(siteSettings.SiteId);
  120. if (e.IsDeleted)
  121. {
  122. Store store = new Store(product.StoreGuid);
  123. // get list of pages where this module is published
  124. List<PageModule> pageModules
  125. = PageModule.GetPageModulesByModule(store.ModuleId);
  126. foreach (PageModule pageModule in pageModules)
  127. {
  128. IndexHelper.RemoveIndexItem(
  129. pageModule.PageId,
  130. store.ModuleId,
  131. product.Guid.ToString());
  132. }
  133. }
  134. else
  135. {
  136. if (ThreadPool.QueueUserWorkItem(new WaitCallback(IndexItem), product))
  137. {
  138. if (debugLog) log.Debug("ProductSearchIndexBuilder.IndexItem queued");
  139. }
  140. else
  141. {
  142. log.Error("Failed to queue a thread for ProductSearchIndexBuilder.IndexItem");
  143. }
  144. }
  145. }
  146. private static void IndexItem(object o)
  147. {
  148. if (WebConfigSettings.DisableSearchIndex) { return; }
  149. if (o == null) return;
  150. if (!(o is Product)) return;
  151. Product content = o as Product;
  152. IndexItem(content);
  153. }
  154. private static void IndexItem(Product product)
  155. {
  156. if (WebConfigSettings.DisableSearchIndex) { return; }
  157. if (product == null)
  158. {
  159. if (log.IsErrorEnabled)
  160. {
  161. log.Error("product object passed to ProductSearchIndexBuilder.IndexItem was null");
  162. }
  163. return;
  164. }
  165. Store store = new Store(product.StoreGuid);
  166. Module module = new Module(store.ModuleId);
  167. Guid webStoreFeatureGuid = new Guid("0cefbf18-56de-11dc-8f36-bac755d89593");
  168. ModuleDefinition webStoreFeature = new ModuleDefinition(webStoreFeatureGuid);
  169. //// get list of pages where this module is published
  170. List<PageModule> pageModules
  171. = PageModule.GetPageModulesByModule(store.ModuleId);
  172. foreach (PageModule pageModule in pageModules)
  173. {
  174. PageSettings pageSettings
  175. = new PageSettings(
  176. product.SiteId,
  177. pageModule.PageId);
  178. //don't index pending/unpublished pages
  179. if (pageSettings.IsPending) { continue; }
  180. IndexItem indexItem = new IndexItem();
  181. if (product.SearchIndexPath.Length > 0)
  182. {
  183. indexItem.IndexPath = product.SearchIndexPath;
  184. }
  185. indexItem.SiteId = product.SiteId;
  186. indexItem.PageId = pageSettings.PageId;
  187. indexItem.PageName = pageSettings.PageName;
  188. indexItem.ViewRoles = pageSettings.AuthorizedRoles;
  189. indexItem.ModuleViewRoles = module.ViewRoles;
  190. if (product.Url.Length > 0)
  191. {
  192. indexItem.ViewPage = product.Url.Replace("~/", string.Empty);
  193. indexItem.UseQueryStringParams = false;
  194. }
  195. else
  196. {
  197. indexItem.ViewPage = "/WebStore/ProductDetail.aspx";
  198. }
  199. indexItem.ItemKey = product.Guid.ToString();
  200. indexItem.ModuleId = store.ModuleId;
  201. indexItem.ModuleTitle = module.ModuleTitle;
  202. indexItem.Title = product.Name;
  203. indexItem.PageMetaDescription = product.MetaDescription;
  204. indexItem.PageMetaKeywords = product.MetaKeywords;
  205. indexItem.Content
  206. = product.Teaser
  207. + " " + product.Description.Replace(tabScript, string.Empty)
  208. + " " + product.MetaDescription
  209. + " " + product.MetaKeywords;
  210. indexItem.FeatureId = webStoreFeatureGuid.ToString();
  211. indexItem.FeatureName = webStoreFeature.FeatureName;
  212. indexItem.FeatureResourceFile = webStoreFeature.ResourceFile;
  213. indexItem.PublishBeginDate = pageModule.PublishBeginDate;
  214. indexItem.PublishEndDate = pageModule.PublishEndDate;
  215. IndexHelper.RebuildIndex(indexItem);
  216. }
  217. if (debugLog) log.Debug("Indexed " + product.Name);
  218. }
  219. }
  220. }