|
| 1 | +using Jumoo.uSync.Core; |
| 2 | +using Jumoo.uSync.Core.Mappers; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Converters; |
| 5 | +using Newtonsoft.Json.Serialization; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using Umbraco.Core; |
| 10 | +using Umbraco.Core.Models; |
| 11 | +using Umbraco.Core.Services; |
| 12 | +using Umbraco.Web.Models; |
| 13 | + |
| 14 | +namespace Jumoo.uSync.ContentMappers |
| 15 | +{ |
| 16 | + public class RelatedLinksMapper : IContentMapper2 |
| 17 | + { |
| 18 | + private readonly IEntityService _entityService; |
| 19 | + private readonly JsonSerializerSettings _serializerSettings; |
| 20 | + |
| 21 | + public RelatedLinksMapper() |
| 22 | + { |
| 23 | + _entityService = ApplicationContext.Current.Services.EntityService; |
| 24 | + |
| 25 | + _serializerSettings = new JsonSerializerSettings |
| 26 | + { |
| 27 | + ContractResolver = new CamelCasePropertyNamesContractResolver(), |
| 28 | + Converters = new List<JsonConverter> { new StringEnumConverter() }, |
| 29 | + Formatting = Formatting.Indented, |
| 30 | + NullValueHandling = NullValueHandling.Ignore, |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 35 | + public string[] PropertyEditorAliases => new[] |
| 36 | + { |
| 37 | + Constants.PropertyEditors.RelatedLinksAlias, |
| 38 | + Constants.PropertyEditors.RelatedLinks2Alias, |
| 39 | + }; |
| 40 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 41 | + |
| 42 | + public string GetExportValue(int dataTypeDefinitionId, string value) |
| 43 | + { |
| 44 | + var links = JsonConvert.DeserializeObject<List<RelatedLink>>(value, _serializerSettings); |
| 45 | + |
| 46 | + if (links?.Any() == true) |
| 47 | + { |
| 48 | + foreach (var link in links) |
| 49 | + { |
| 50 | + if (link.Type == RelatedLinkType.Internal && |
| 51 | + int.TryParse(link.Link, out var id) == true && |
| 52 | + id > 0) |
| 53 | + { |
| 54 | + var attempt = _entityService.uSyncGetKeyForId(id); |
| 55 | + if (attempt.Success == true) |
| 56 | + { |
| 57 | + link.Link = attempt.Result.ToString(); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return JsonConvert.SerializeObject(links, _serializerSettings); |
| 64 | + } |
| 65 | + |
| 66 | + public string GetImportValue(int dataTypeDefinitionId, string content) |
| 67 | + { |
| 68 | + var links = JsonConvert.DeserializeObject<List<RelatedLink>>(content, _serializerSettings); |
| 69 | + |
| 70 | + if (links?.Any() == true) |
| 71 | + { |
| 72 | + foreach (var link in links) |
| 73 | + { |
| 74 | + if (link.Type == RelatedLinkType.Internal && |
| 75 | + Guid.TryParse(link.Link, out var guid) == true && |
| 76 | + guid.Equals(Guid.Empty) == false) |
| 77 | + { |
| 78 | + var attempt = _entityService.GetIdForKey(guid, UmbracoObjectTypes.Document); |
| 79 | + if (attempt.Success) |
| 80 | + { |
| 81 | + link.Link = attempt.Result.ToString(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return JsonConvert.SerializeObject(links, _serializerSettings); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments