using Examine;
using ProjectName.Common.Constants;
using ProjectName.Web.Models.ModelsBuilder;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Web;
using Umbraco.Extensions;
namespace ProjectName.Web.Components
{
// Add Examine Component functionality on start-up
public class ExamineComponent : IComponent
{
private readonly IExamineManager _examineManager;
private readonly IUmbracoContextFactory _umbracoContextFactory;
public ExamineComponent(IExamineManager examineManager, IUmbracoContextFactory umbracoContextFactory)
{
_examineManager = examineManager;
_umbracoContextFactory = umbracoContextFactory;
}
public void Initialize()
{
if (!_examineManager.TryGetIndex(Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName, out IIndex index))
{
throw new InvalidOperationException(
$"No index found by the name {Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName}");
}
if (!(index is BaseIndexProvider indexProvider))
throw new InvalidOperationException("Could not cast)");
indexProvider.TransformingIndexValues += IndexProviderTransformingIndexValues;
}
private void IndexProviderTransformingIndexValues(object? sender, IndexingItemEventArgs e)
{
var values = e.ValueSet.Values.ToDictionary(x => x.Key, x => (IEnumerable<object>)x.Value);
if (int.TryParse(e.ValueSet.Id, out var nodeId))
{
using (var umbracoContext = _umbracoContextFactory.EnsureUmbracoContext())
{
var contentNode = umbracoContext?.UmbracoContext?.Content?.GetById(nodeId);
if (contentNode != null)
{
var val = e.ValueSet;
switch (val.ItemType)
{
case NewsArticle.ModelTypeAlias:
// Get create date
var createDate = val.GetValue(GetUmbracoFriendlyCamelCaseKey(nameof(NewsArticle.CreateDate)));
// Get update date
var updateDate = val.GetValue(GetUmbracoFriendlyCamelCaseKey(nameof(NewsArticle.UpdateDate)));
// Get publication date
var publicationDate = val.GetValue(GetUmbracoFriendlyCamelCaseKey(nameof(NewsArticle.PublishedDate)));
// Use the create date by default (not all articles will have a update date or publication date)
var finalDate = createDate;
if (DateTime.TryParse(publicationDate?.ToString() ?? string.Empty, out DateTime publicationDateValue))
{
if (publicationDateValue > DateTime.MinValue)
{
// Use the publication date as it was set
finalDate = publicationDate;
}
}
else if (DateTime.TryParse(updateDate?.ToString() ?? string.Empty, out DateTime updateDateValue))
{
if (updateDateValue > DateTime.MinValue)
{
// Publication date was not set but the item has been published. Use the update date
finalDate = updateDate;
}
}
if (DateTime.TryParse(finalDate?.ToString(), out DateTime finalDateTime))
{
// Add the new key to the dictionary
values.TryAdd(Search.ExamineFieldDefinition.SortableArticlePublishedDate, new List<object> { finalDateTime.Ticks });
}
break;
}
}
}
}
e.SetValues(values);
}
public void Terminate()
{
// do not do anything
}
/// <summary>
/// Returns Umbraco-friendly camel case key value from the property name
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
private string GetUmbracoFriendlyCamelCaseKey(string propertyName)
{
if (!string.IsNullOrEmpty(propertyName))
{
return $"{propertyName.Substring(0, 1).ToLower()}{propertyName[1..]}";
}
return propertyName;
}
}
}
Comments
Post a Comment