PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mojoPortal.Features.UI/ImageGallery/Components/GalleryImageIndexBuilderProvider.cs

http://mojoportal.codeplex.com
C# | 231 lines | 166 code | 46 blank | 19 comment | 21 complexity | 7325281519488eb7f6d6bd38b75f9607 MD5 | raw file
Possible License(s): CPL-1.0, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, Apache-2.0
  1. /// Author: Joe Audette
  2. /// Created: 2007-08-30
  3. /// Last Modified: 2013-01-15
  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. using log4net;
  13. using mojoPortal.Business;
  14. using mojoPortal.Business.WebHelpers;
  15. using mojoPortal.SearchIndex;
  16. using mojoPortal.Web;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Data;
  20. namespace mojoPortal.Features
  21. {
  22. public class GalleryImageIndexBuilderProvider : IndexBuilderProvider
  23. {
  24. private static readonly ILog log = LogManager.GetLogger(typeof(GalleryImageIndexBuilderProvider));
  25. private static bool debugLog = log.IsDebugEnabled;
  26. public GalleryImageIndexBuilderProvider()
  27. { }
  28. public override void RebuildIndex(
  29. PageSettings pageSettings,
  30. string indexPath)
  31. {
  32. if (WebConfigSettings.DisableSearchIndex) { return; }
  33. if (pageSettings == null) return;
  34. //don't index pending/unpublished pages
  35. if (pageSettings.IsPending) { return; }
  36. log.Info("GalleryImageIndexBuilderProvider indexing page - "
  37. + pageSettings.PageName);
  38. try
  39. {
  40. Guid galleryFeatureGuid = new Guid("d572f6b4-d0ed-465d-ad60-60433893b401");
  41. ModuleDefinition galleryFeature = new ModuleDefinition(galleryFeatureGuid);
  42. List<PageModule> pageModules
  43. = PageModule.GetPageModulesByPage(pageSettings.PageId);
  44. DataTable dataTable = GalleryImage.GetImagesByPage(pageSettings.SiteId, pageSettings.PageId);
  45. foreach (DataRow row in dataTable.Rows)
  46. {
  47. mojoPortal.SearchIndex.IndexItem indexItem = new mojoPortal.SearchIndex.IndexItem();
  48. indexItem.SiteId = pageSettings.SiteId;
  49. indexItem.PageId = pageSettings.PageId;
  50. indexItem.PageName = pageSettings.PageName;
  51. indexItem.ViewRoles = pageSettings.AuthorizedRoles;
  52. indexItem.ModuleViewRoles = row["ViewRoles"].ToString();
  53. if (pageSettings.UseUrl)
  54. {
  55. indexItem.ViewPage = pageSettings.Url.Replace("~/", string.Empty);
  56. indexItem.UseQueryStringParams = false;
  57. }
  58. indexItem.FeatureId = galleryFeatureGuid.ToString();
  59. indexItem.FeatureName = galleryFeature.FeatureName;
  60. indexItem.FeatureResourceFile = galleryFeature.ResourceFile;
  61. // TODO: it would be good to check the module settings and if not
  62. // in compact mode use the GalleryBrowse.aspx page
  63. //indexItem.ViewPage = "GalleryBrowse.aspx";
  64. indexItem.ItemId = Convert.ToInt32(row["ItemID"]);
  65. indexItem.ModuleId = Convert.ToInt32(row["ModuleID"]);
  66. indexItem.ModuleTitle = row["ModuleTitle"].ToString();
  67. indexItem.Title = row["Caption"].ToString();
  68. indexItem.Content = row["Description"].ToString();
  69. indexItem.QueryStringAddendum = "&ItemID"
  70. + row["ModuleID"].ToString()
  71. + "=" + row["ItemID"].ToString();
  72. indexItem.CreatedUtc = Convert.ToDateTime(row["UploadDate"]);
  73. indexItem.LastModUtc = indexItem.CreatedUtc;
  74. // lookup publish dates
  75. foreach (PageModule pageModule in pageModules)
  76. {
  77. if (indexItem.ModuleId == pageModule.ModuleId)
  78. {
  79. indexItem.PublishBeginDate = pageModule.PublishBeginDate;
  80. indexItem.PublishEndDate = pageModule.PublishEndDate;
  81. }
  82. }
  83. mojoPortal.SearchIndex.IndexHelper.RebuildIndex(indexItem, indexPath);
  84. if (debugLog) log.Debug("Indexed " + indexItem.Title);
  85. }
  86. }
  87. catch (System.Data.Common.DbException ex)
  88. {
  89. log.Error(ex);
  90. }
  91. }
  92. public override void ContentChangedHandler(
  93. object sender,
  94. ContentChangedEventArgs e)
  95. {
  96. if (WebConfigSettings.DisableSearchIndex) { return; }
  97. GalleryImage galleryImage = (GalleryImage)sender;
  98. if (e.IsDeleted)
  99. {
  100. SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
  101. if (siteSettings == null) return;
  102. // get list of pages where this module is published
  103. List<PageModule> pageModules
  104. = PageModule.GetPageModulesByModule(galleryImage.ModuleId);
  105. foreach (PageModule pageModule in pageModules)
  106. {
  107. RemoveGalleryImageIndexItem(
  108. siteSettings.SiteId,
  109. pageModule.PageId,
  110. galleryImage.ModuleId,
  111. galleryImage.ItemId);
  112. }
  113. }
  114. else
  115. {
  116. IndexItem(galleryImage);
  117. }
  118. }
  119. private static void IndexItem(GalleryImage galleryImage)
  120. {
  121. if (WebConfigSettings.DisableSearchIndex) { return; }
  122. SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
  123. if ((siteSettings == null)
  124. || (galleryImage == null))
  125. {
  126. return;
  127. }
  128. Guid galleryFeatureGuid = new Guid("d572f6b4-d0ed-465d-ad60-60433893b401");
  129. ModuleDefinition galleryFeature = new ModuleDefinition(galleryFeatureGuid);
  130. Module module = new Module(galleryImage.ModuleId);
  131. // get list of pages where this module is published
  132. List<PageModule> pageModules
  133. = PageModule.GetPageModulesByModule(galleryImage.ModuleId);
  134. foreach (PageModule pageModule in pageModules)
  135. {
  136. PageSettings pageSettings
  137. = new PageSettings(
  138. siteSettings.SiteId,
  139. pageModule.PageId);
  140. //don't index pending/unpublished pages
  141. if (pageSettings.IsPending) { continue; }
  142. mojoPortal.SearchIndex.IndexItem indexItem = new mojoPortal.SearchIndex.IndexItem();
  143. indexItem.SiteId = siteSettings.SiteId;
  144. indexItem.PageId = pageSettings.PageId;
  145. indexItem.PageName = pageSettings.PageName;
  146. indexItem.ViewRoles = pageSettings.AuthorizedRoles;
  147. indexItem.ModuleViewRoles = module.ViewRoles;
  148. indexItem.FeatureId = galleryFeatureGuid.ToString();
  149. indexItem.FeatureName = galleryFeature.FeatureName;
  150. indexItem.FeatureResourceFile = galleryFeature.ResourceFile;
  151. indexItem.CreatedUtc = galleryImage.UploadDate;
  152. indexItem.LastModUtc = galleryImage.UploadDate;
  153. indexItem.ItemId = galleryImage.ItemId;
  154. indexItem.ModuleId = galleryImage.ModuleId;
  155. indexItem.QueryStringAddendum = "&ItemID"
  156. + galleryImage.ModuleId.ToString()
  157. + "=" + galleryImage.ItemId.ToString();
  158. indexItem.ModuleTitle = module.ModuleTitle;
  159. indexItem.Title = galleryImage.Caption;
  160. indexItem.Content = galleryImage.Description;
  161. indexItem.PublishBeginDate = pageModule.PublishBeginDate;
  162. indexItem.PublishEndDate = pageModule.PublishEndDate;
  163. mojoPortal.SearchIndex.IndexHelper.RebuildIndex(indexItem);
  164. if (debugLog) log.Debug("Indexed " + galleryImage.Caption);
  165. }
  166. }
  167. private static void RemoveGalleryImageIndexItem(
  168. int siteId,
  169. int pageId,
  170. int moduleId,
  171. int itemId)
  172. {
  173. if (WebConfigSettings.DisableSearchIndex) { return; }
  174. mojoPortal.SearchIndex.IndexItem indexItem = new mojoPortal.SearchIndex.IndexItem();
  175. indexItem.SiteId = siteId;
  176. indexItem.PageId = pageId;
  177. indexItem.ModuleId = moduleId;
  178. indexItem.ItemId = itemId;
  179. indexItem.QueryStringAddendum = "&ItemID" + moduleId.ToString()
  180. + "=" + itemId.ToString();
  181. mojoPortal.SearchIndex.IndexHelper.RemoveIndex(indexItem);
  182. if (debugLog) log.Debug("Removed Index ");
  183. }
  184. }
  185. }