From fa59ca2d3f63f0073fd4556bb4eb408e47de674c Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Mon, 15 Jun 2015 14:59:47 +0200 Subject: [PATCH] Update Examine index when download count changes --- .../CustomHandlers/ProjectIndexer.cs | 65 ++++++++++++------- uWiki/BusinessLogic/Events.cs | 5 ++ uWiki/BusinessLogic/WikiFile.cs | 25 +++++-- 3 files changed, 67 insertions(+), 28 deletions(-) diff --git a/our.umbraco.org/CustomHandlers/ProjectIndexer.cs b/our.umbraco.org/CustomHandlers/ProjectIndexer.cs index 6298bcdc..0d6e6da1 100644 --- a/our.umbraco.org/CustomHandlers/ProjectIndexer.cs +++ b/our.umbraco.org/CustomHandlers/ProjectIndexer.cs @@ -5,56 +5,73 @@ using Examine.LuceneEngine; using Examine.LuceneEngine.Providers; using our.Examine; using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Services; using Umbraco.Web; +using uWiki.Businesslogic; namespace our.CustomHandlers { public class ProjectIndexer : ApplicationEventHandler { - protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Published += ContentService_Published; ContentService.Deleted += ContentService_Deleted; + WikiFile.AfterDownloadUpdate += WikiFile_AfterDownloadUpdate; } - void ContentService_Deleted(IContentService sender, Umbraco.Core.Events.DeleteEventArgs e) + void ContentService_Deleted(IContentService sender, Umbraco.Core.Events.DeleteEventArgs e) { - - var indexer = (SimpleDataIndexer)ExamineManager.Instance.IndexProviderCollection["projectIndexer"]; foreach (var item in e.DeletedEntities.Where(x => x.ContentType.Alias == "Project")) - indexer.DeleteFromIndex(item.Id.ToString()); + ((SimpleDataIndexer)ExamineManager.Instance.IndexProviderCollection["projectIndexer"]).DeleteFromIndex(item.Id.ToString()); } - void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs e) + void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs e) { - var indexer = (SimpleDataIndexer)ExamineManager.Instance.IndexProviderCollection["projectIndexer"]; - var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); - foreach (var item in e.PublishedEntities.Where(x => x.ContentType.Alias == "Project")) { if (item.GetValue("projectLive")) { - var content = umbracoHelper.TypedContent(item.Id); - var simpleDataSet = new SimpleDataSet { NodeDefinition = new IndexedNode(), RowData = new Dictionary() }; - - var karma = Utils.GetProjectTotalVotes(content.Id); - var files = uWiki.Businesslogic.WikiFile.CurrentFiles(content.Id); - var downloads = Utils.GetProjectTotalDownloadCount(content.Id); - var compatVersions = Utils.GetProjectCompatibleVersions(content.Id); - - simpleDataSet = ((ProjectNodeIndexDataService)indexer.DataService).MapProjectToSimpleDataIndexItem( - content, simpleDataSet, "project", karma, files, downloads, compatVersions); - - var xml = simpleDataSet.RowData.ToExamineXml(simpleDataSet.NodeDefinition.NodeId, simpleDataSet.NodeDefinition.Type); - indexer.ReIndexNode(xml, "project"); + UpdateProjectExamineIndex(item); } } } - + void WikiFile_AfterDownloadUpdate(object sender, FileDownloadUpdateEventArgs e) + { + var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); + var content = umbracoHelper.TypedContent(e.ProjectId); + UpdateProjectExamineIndex(content, e.Downloads); + } + private void UpdateProjectExamineIndex(IEntity item) + { + var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); + var content = umbracoHelper.TypedContent(item.Id); + var downloads = Utils.GetProjectTotalDownloadCount(content.Id); + UpdateProjectExamineIndex(content, downloads); + } + + private void UpdateProjectExamineIndex(IPublishedContent content, int downloads) + { + var simpleDataSet = new SimpleDataSet + { + NodeDefinition = new IndexedNode(), + RowData = new Dictionary() + }; + + var karma = Utils.GetProjectTotalVotes(content.Id); + var files = WikiFile.CurrentFiles(content.Id); + var compatVersions = Utils.GetProjectCompatibleVersions(content.Id); + + var simpleDataIndexer = (SimpleDataIndexer)ExamineManager.Instance.IndexProviderCollection["projectIndexer"]; + simpleDataSet = ((ProjectNodeIndexDataService)simpleDataIndexer.DataService) + .MapProjectToSimpleDataIndexItem(content, simpleDataSet, "project", karma, files, downloads, compatVersions); + + var xml = simpleDataSet.RowData.ToExamineXml(simpleDataSet.NodeDefinition.NodeId, simpleDataSet.NodeDefinition.Type); + simpleDataIndexer.ReIndexNode(xml, "project"); + } } - } diff --git a/uWiki/BusinessLogic/Events.cs b/uWiki/BusinessLogic/Events.cs index c9123a37..706e76af 100644 --- a/uWiki/BusinessLogic/Events.cs +++ b/uWiki/BusinessLogic/Events.cs @@ -8,6 +8,11 @@ namespace uWiki.Businesslogic { public class FileCreateEventArgs : System.ComponentModel.CancelEventArgs { } public class FileUpdateEventArgs : System.ComponentModel.CancelEventArgs { } public class FileRemoveEventArgs : System.ComponentModel.CancelEventArgs { } + public class FileDownloadUpdateEventArgs : EventArgs + { + public int ProjectId { get; set; } + public int Downloads { get; set; } + } public class CreateEventArgs : System.ComponentModel.CancelEventArgs { } public class UpdateEventArgs : System.ComponentModel.CancelEventArgs { } diff --git a/uWiki/BusinessLogic/WikiFile.cs b/uWiki/BusinessLogic/WikiFile.cs index 2ac1a76a..d3c0a7b9 100644 --- a/uWiki/BusinessLogic/WikiFile.cs +++ b/uWiki/BusinessLogic/WikiFile.cs @@ -86,7 +86,7 @@ namespace uWiki.Businesslogic } else { - wikiFiles.Add(result.nodeId, new List(new[] {file})); + wikiFiles.Add(result.nodeId, new List(new[] { file })); } } } @@ -390,14 +390,21 @@ namespace uWiki.Businesslogic } - public static void UpdateDownloadCount(int fileId, bool ignoreCookies, bool isPackage) + public void UpdateDownloadCount(int fileId, bool ignoreCookies, bool isPackage) { var cookie = HttpContext.Current.Request.Cookies["ProjectFileDownload" + fileId]; if (cookie != null && ignoreCookies == false) return; + var downloads = 0; + var projectId = 0; - var downloads = Application.SqlHelper.ExecuteScalar("Select downloads from wikiFiles where id = @id;", Application.SqlHelper.CreateParameter("@id", fileId)); + var reader = Application.SqlHelper.ExecuteReader("Select downloads, nodeId from wikiFiles where id = @id;", Application.SqlHelper.CreateParameter("@id", fileId)); + if (reader.Read()) + { + downloads = reader.GetInt("downloads"); + projectId = reader.GetInt("nodeId"); + } downloads = downloads + 1; Application.SqlHelper.ExecuteNonQuery( @@ -405,6 +412,8 @@ namespace uWiki.Businesslogic Application.SqlHelper.CreateParameter("@id", fileId), Application.SqlHelper.CreateParameter("@downloads", downloads)); + var totalDownloads = Application.SqlHelper.ExecuteScalar("Select SUM(downloads) from wikiFiles where nodeId = @projectId;", Application.SqlHelper.CreateParameter("@projectId", projectId)); + if (isPackage) { var currentMember = 0; @@ -420,6 +429,9 @@ namespace uWiki.Businesslogic Application.SqlHelper.CreateParameter("@memberId", currentMember)); } + var e = new FileDownloadUpdateEventArgs { ProjectId = projectId, Downloads = totalDownloads }; + FireAfterDownloadUpdate(e); + cookie = new HttpCookie("ProjectFileDownload" + fileId) { Expires = DateTime.Now.AddHours(1) }; HttpContext.Current.Response.Cookies.Add(cookie); } @@ -491,6 +503,11 @@ namespace uWiki.Businesslogic AfterUpdate(this, e); } - + public static event EventHandler AfterDownloadUpdate; + protected virtual void FireAfterDownloadUpdate(FileDownloadUpdateEventArgs e) + { + if (AfterDownloadUpdate != null) + AfterDownloadUpdate(this, e); + } } }