Skip to content

Instantly share code, notes, and snippets.

@JJCLane
Created November 17, 2016 17:39
Show Gist options
  • Save JJCLane/838dedbae10605fbf32a06d45010c95b to your computer and use it in GitHub Desktop.
Save JJCLane/838dedbae10605fbf32a06d45010c95b to your computer and use it in GitHub Desktop.
A simple data resolver for the Umbraco Similarity package
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Courier.Core;
using Umbraco.Courier.Core.Logging;
using Umbraco.Courier.DataResolvers;
using Umbraco.Courier.ItemProviders;
using System.Collections.Generic;
namespace Project.BusinessLogic.Data_Resolvers
{
public class SimilarityDataResolver : PropertyDataResolverProvider
{
public override string EditorAlias
{
get
{
return "Similarity";
}
}
private enum Direction
{
Extracting,
Packaging
}
public override void PackagingProperty(Item item, ContentProperty propertyData)
{
UpdateContentIdentifiers(item, propertyData, Direction.Packaging);
}
public override void ExtractingProperty(Item item, ContentProperty propertyData)
{
UpdateContentIdentifiers(item, propertyData, Direction.Extracting);
}
private void UpdateContentIdentifiers(Item item, ContentProperty propertyData, Direction direction)
{
if (propertyData.Value == null) return;
var contentIds = propertyData.Value.ToString().Split(',');
var newIds = new List<string>();
foreach (var contentId in contentIds)
{
string contentIdentifier = null;
switch (direction)
{
case Direction.Packaging:
contentIdentifier = ExecutionContext.DatabasePersistence.GetUniqueId(int.Parse(contentId), UmbracoNodeObjectTypeIds.Document).ToString();
item.Dependencies.Add(contentIdentifier, ItemProviderIds.documentItemProviderGuid);
break;
case Direction.Extracting:
contentIdentifier = ExecutionContext.DatabasePersistence.GetNodeId(Guid.Parse(contentId), UmbracoNodeObjectTypeIds.Document).ToString();
break;
}
newIds.Add(contentIdentifier);
}
propertyData.Value = String.Join(",", newIds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment