Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Courier property resolver for Tea Commerce Variants property editor
namespace TeaCommerce.Umbraco.Courier
{
public class TeaCommerceVariantPropertyDataResolver : PropertyDataResolverProvider
{
public override string EditorAlias
{
get
{
return "TeaCommerce.VariantEditor";
}
}
public override void PackagingDataType(DataType item)
{
if (item == null || item.Prevalues == null || item.Prevalues.Count == 0)
return;
// Cache resolved doc types
var resolvedDocTypes = new Dictionary<string, DocumentType>();
// Variant doc type dependency
var variantDocTypeAlias = item.Prevalues.FirstOrDefault(x => x.Alias.InvariantEquals("variantDocumentType"));
if (variantDocTypeAlias != null && !string.IsNullOrEmpty(variantDocTypeAlias.Value)) {
var documentType = GetDocumentType(variantDocTypeAlias.Value, resolvedDocTypes);
if (documentType != null) {
item.Dependencies.Add(documentType.UniqueId.ToString(), ItemProviderIds.documentTypeItemProviderGuid);
}
}
// Variant group doc type dependencies
var variantGroupDocumentTypeAlaises = item.Prevalues.FirstOrDefault(x => x.Alias.InvariantEquals("variantGroupDocumentTypes"));
if (variantGroupDocumentTypeAlaises != null && !string.IsNullOrEmpty(variantGroupDocumentTypeAlaises.Value)) {
var aliases = variantGroupDocumentTypeAlaises.Value.Split(new[]{ ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var alias in aliases) {
var documentType = GetDocumentType(alias, resolvedDocTypes);
if (documentType != null) {
item.Dependencies.Add(documentType.UniqueId.ToString(), ItemProviderIds.documentTypeItemProviderGuid);
}
}
}
}
public override void PackagingProperty(Item item, ContentProperty propertyData)
{
if (propertyData == null || propertyData.Value == null)
return;
// Parse the property value to JObject
var json = JsonConvert.DeserializeObject<JObject>(propertyData.Value.ToString());
if (json == null)
return;
// Get the variants
var variants = (JArray)json["variants"];
if (variants == null)
return;
foreach (var variant in variants)
{
// Get the combinations
var combinations = (JArray)variant["combination"];
if (combinations == null)
continue;
foreach (var combination in combinations)
{
int tmpId;
Guid tmpIdGuid;
// Process ID property
if (int.TryParse(combination["id"].ToString(), out tmpId)) {
tmpIdGuid = ExecutionContext.DatabasePersistence.GetUniqueId(tmpId, UmbracoNodeObjectTypeIds.Document);
if (!Guid.Empty.Equals(tmpIdGuid)) {
item.Dependencies.Add(tmpIdGuid.ToString(), ItemProviderIds.documentItemProviderGuid);
combination["id"] = tmpIdGuid.ToString();
}
}
// Process Group ID property
if (int.TryParse(combination["groupId"].ToString(), out tmpId)) {
tmpIdGuid = ExecutionContext.DatabasePersistence.GetUniqueId(tmpId, UmbracoNodeObjectTypeIds.Document);
if (!Guid.Empty.Equals(tmpIdGuid)) {
item.Dependencies.Add(tmpIdGuid.ToString(), ItemProviderIds.documentItemProviderGuid);
combination["groupId"] = tmpIdGuid.ToString();
}
}
}
}
propertyData.Value = json.ToString();
}
public override void ExtractingProperty(Item item, ContentProperty propertyData)
{
if (propertyData.Value != null)
{
// Parse the property value to JObject
var json = JsonConvert.DeserializeObject<JObject>(propertyData.Value.ToString());
if (json == null)
return;
// Get the variants
var variants = (JArray)json["variants"];
if (variants == null)
return;
foreach (var variant in variants)
{
// Get the combinations
var combinations = (JArray)variant["combination"];
if (combinations == null)
continue;
foreach (var combination in combinations)
{
Guid tmpIdGuid;
// Process ID property
if (Guid.TryParse(combination["id"].ToString(), out tmpIdGuid)) {
combination["id"] = ExecutionContext.DatabasePersistence.GetNodeId(tmpIdGuid, UmbracoNodeObjectTypeIds.Document);
}
// Process Group ID property
if (Guid.TryParse(combination["groupId"].ToString(), out tmpIdGuid)) {
combination["groupId"] = ExecutionContext.DatabasePersistence.GetNodeId(tmpIdGuid, UmbracoNodeObjectTypeIds.Document);
}
}
}
propertyData.Value = json.ToString();
}
}
private DocumentType GetDocumentType(string docTypeAlias, IDictionary<string, DocumentType> cache)
{
DocumentType documentType;
// Don't look it up if we already have done that
if (cache.TryGetValue(docTypeAlias, out documentType) == false)
{
documentType = ExecutionContext.DatabasePersistence.RetrieveItem<DocumentType>(new ItemIdentifier(docTypeAlias, ItemProviderIds.documentTypeItemProviderGuid));
cache[docTypeAlias] = documentType;
}
return documentType;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.