Created
November 17, 2016 17:39
-
-
Save JJCLane/838dedbae10605fbf32a06d45010c95b to your computer and use it in GitHub Desktop.
A simple data resolver for the Umbraco Similarity package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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