|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using Umbraco.Core; |
| 6 | +using Umbraco.Core.Deploy; |
| 7 | +using Umbraco.Core.Models; |
| 8 | +using Umbraco.Core.Services; |
| 9 | + |
| 10 | +namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors |
| 11 | +{ |
| 12 | + public class MeganavValueConnector : IValueConnector |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// The entity service. |
| 16 | + /// </summary> |
| 17 | + private readonly IEntityService entityService; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Gets the property editor aliases that the value converter supports by default. |
| 21 | + /// </summary> |
| 22 | + public IEnumerable<string> PropertyEditorAliases => new string[] { "Cogworks.Meganav" }; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes a new instance of the <see cref="MeganavValueConnector"/> class. |
| 26 | + /// </summary> |
| 27 | + /// <param name="entityService">The entity service.</param> |
| 28 | + /// <exception cref="ArgumentNullException">entityService</exception> |
| 29 | + public MeganavValueConnector(IEntityService entityService) |
| 30 | + { |
| 31 | + this.entityService = entityService ?? throw new ArgumentNullException("entityService"); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets the deploy property corresponding to a content property. |
| 36 | + /// </summary> |
| 37 | + /// <param name="property">The content property.</param> |
| 38 | + /// <param name="dependencies">The content dependencies.</param> |
| 39 | + /// <returns> |
| 40 | + /// The deploy property value. |
| 41 | + /// </returns> |
| 42 | + public string GetValue(Property property, ICollection<ArtifactDependency> dependencies) |
| 43 | + { |
| 44 | + var value = property.Value as string; |
| 45 | + if (String.IsNullOrWhiteSpace(value) || !StringExtensions.DetectIsJson(value)) |
| 46 | + { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + // Parse links and convert ID to UDI |
| 51 | + var links = JArray.Parse(value); |
| 52 | + foreach (var link in links) |
| 53 | + { |
| 54 | + GuidUdi guidUdi = null; |
| 55 | + int id = link.Value<int>("id"); |
| 56 | + if (id != 0) |
| 57 | + { |
| 58 | + Attempt<Guid> keyForId = this.entityService.GetKeyForId(id, UmbracoObjectTypes.Document); |
| 59 | + if (keyForId.Success) |
| 60 | + { |
| 61 | + guidUdi = new GuidUdi("document", keyForId.Result); |
| 62 | + dependencies.Add(new ArtifactDependency(guidUdi, false, ArtifactDependencyMode.Exist)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + link["id"] = guidUdi?.ToString(); |
| 67 | + } |
| 68 | + |
| 69 | + return links.ToString(Formatting.None); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Sets a content property value using a deploy property. |
| 74 | + /// </summary> |
| 75 | + /// <param name="content">The content item.</param> |
| 76 | + /// <param name="alias">The property alias.</param> |
| 77 | + /// <param name="value">The deploy property value.</param> |
| 78 | + public void SetValue(IContentBase content, string alias, string value) |
| 79 | + { |
| 80 | + if (String.IsNullOrWhiteSpace(value)) |
| 81 | + { |
| 82 | + // Save empty value |
| 83 | + content.SetValue(alias, value); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (!StringExtensions.DetectIsJson(value)) |
| 88 | + { |
| 89 | + // Skip invalid value (probably shoudn't be saved, right?) |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // Parse links and convert UDI back to local ID |
| 94 | + var links = JArray.Parse(value); |
| 95 | + foreach (var link in links) |
| 96 | + { |
| 97 | + int id = 0; |
| 98 | + if (GuidUdi.TryParse(link.Value<string>("id"), out GuidUdi guidUdi)) |
| 99 | + { |
| 100 | + Attempt<int> idForUdi = this.entityService.GetIdForUdi(guidUdi); |
| 101 | + if (idForUdi.Success) |
| 102 | + { |
| 103 | + id = idForUdi.Result; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + link["id"] = id; |
| 108 | + } |
| 109 | + |
| 110 | + content.SetValue(alias, links.ToString(Formatting.None)); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments