Update Examine index when download count changes

This commit is contained in:
Sebastiaan Janssen
2015-06-15 14:59:47 +02:00
parent d25b7a0215
commit fa59ca2d3f
3 changed files with 67 additions and 28 deletions
@@ -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<Umbraco.Core.Models.IContent> e)
void ContentService_Deleted(IContentService sender, Umbraco.Core.Events.DeleteEventArgs<IContent> 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<Umbraco.Core.Models.IContent> e)
void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> 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<bool>("projectLive"))
{
var content = umbracoHelper.TypedContent(item.Id);
var simpleDataSet = new SimpleDataSet { NodeDefinition = new IndexedNode(), RowData = new Dictionary<string, string>() };
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<string, string>()
};
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");
}
}
}
+5
View File
@@ -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 { }
+21 -4
View File
@@ -86,7 +86,7 @@ namespace uWiki.Businesslogic
}
else
{
wikiFiles.Add(result.nodeId, new List<WikiFile>(new[] {file}));
wikiFiles.Add(result.nodeId, new List<WikiFile>(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<int>("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<int>("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<FileDownloadUpdateEventArgs> AfterDownloadUpdate;
protected virtual void FireAfterDownloadUpdate(FileDownloadUpdateEventArgs e)
{
if (AfterDownloadUpdate != null)
AfterDownloadUpdate(this, e);
}
}
}