Files
OurUmbraco/our.umbraco.org/Examine/ProjectsIndexDataService/ProjectNodeIndexDataService.cs
T

83 lines
3.4 KiB
C#
Raw Normal View History

using Examine;
using Examine.LuceneEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core.Models;
using our.Examine.DocumentationIndexDataService.Helper;
2015-02-25 15:00:01 +01:00
using Umbraco.Core.Logging;
namespace our.ExamineServices
{
public class ProjectNodeIndexDataService : ISimpleDataService
{
public SimpleDataSet MapProjectToSimpleDataIndexItem(IPublishedContent project, SimpleDataSet simpleDataSet, int index, string indexType)
{
simpleDataSet.NodeDefinition.NodeId = project.Id;
simpleDataSet.NodeDefinition.Type = indexType;
simpleDataSet.RowData.Add("body", umbraco.library.StripHtml( project.GetProperty("description").Value.ToString() )) ;
simpleDataSet.RowData.Add("nodeName", project.Name);
simpleDataSet.RowData.Add("updateDate", project.UpdateDate.SerializeForLucene());
simpleDataSet.RowData.Add("nodeTypeAlias", "project");
simpleDataSet.RowData.Add("url", project.Url );
2015-02-25 15:00:01 +01:00
var karma = our.Utills.GetProjectTotalKarma(project.Id);
var files = uWiki.Businesslogic.WikiFile.CurrentFiles(project.Id);
var downloads = our.Utills.GetProjectTotalDownloadCount(project.Id);
var image = files.Where(x => x.FileType == "screenshot").FirstOrDefault();
var imageFile = "";
if (image != null)
imageFile = image.Path;
//Clean up version data before its included in the index
int o;
var version = project.GetProperty("compatibleVersions").Value;
var versions = version.ToString().ToLower()
.Replace("nan", "")
.Replace("saved", "")
.Replace("v", "")
.Trim(',').Split(',')
.Where(x => int.TryParse(x, out o))
.Select(x => (decimal.Parse(x.PadRight(3, '0') ) / 100));
simpleDataSet.RowData.Add("karma", karma.ToString());
simpleDataSet.RowData.Add("downloads", downloads.ToString());
simpleDataSet.RowData.Add("image", imageFile);
simpleDataSet.RowData.Add("versions", string.Join(",", versions));
return simpleDataSet;
}
public IEnumerable<SimpleDataSet> GetAllData(string indexType)
{
var dataSets = new List<SimpleDataSet>();
2015-02-25 15:00:01 +01:00
var projects = Umbraco.Web.UmbracoContext.Current.ContentCache.GetByXPath("//Community/Projects//Project [projectLive='1']");
//index all projects
2015-02-25 15:00:01 +01:00
for (int i = 0; i < projects.Count(); i++)
{
2015-02-25 15:00:01 +01:00
var project = projects.ElementAt(i);
try
{
2015-02-25 15:00:01 +01:00
LogHelper.Debug(this.GetType(), "Indexing " + project.Name);
var simpleDataSet = new SimpleDataSet { NodeDefinition = new IndexedNode(), RowData = new Dictionary<string, string>() };
simpleDataSet = MapProjectToSimpleDataIndexItem(project, simpleDataSet, project.Id, indexType);
dataSets.Add(simpleDataSet);
}
catch (Exception ex)
{
2015-02-25 15:00:01 +01:00
LogHelper.Error(this.GetType(), ex.Message, ex);
}
}
return dataSets;
}
}
}