-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
ConvertRelatedLinksToMultiUrlPicker.cs
142 lines (116 loc) · 5.2 KB
/
ConvertRelatedLinksToMultiUrlPicker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Umbraco.Core.Migrations.Upgrade.V_8_0_0.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Dtos;
namespace Umbraco.Core.Migrations.Upgrade.V_8_0_0
{
public class ConvertRelatedLinksToMultiUrlPicker : MigrationBase
{
public ConvertRelatedLinksToMultiUrlPicker(IMigrationContext context) : base(context)
{ }
public override void Migrate()
{
var sqlDataTypes = Sql()
.Select<DataTypeDto>()
.From<DataTypeDto>()
.Where<DataTypeDto>(x => x.EditorAlias == Constants.PropertyEditors.Legacy.Aliases.RelatedLinks
|| x.EditorAlias == Constants.PropertyEditors.Legacy.Aliases.RelatedLinks2);
var dataTypes = Database.Fetch<DataTypeDto>(sqlDataTypes);
var dataTypeIds = dataTypes.Select(x => x.NodeId).ToList();
if (dataTypeIds.Count == 0) return;
foreach (var dataType in dataTypes)
{
dataType.EditorAlias = Constants.PropertyEditors.Aliases.MultiUrlPicker;
Database.Update(dataType);
}
var sqlPropertyTpes = Sql()
.Select<PropertyTypeDto80>()
.From<PropertyTypeDto80>()
.Where<PropertyTypeDto80>(x => dataTypeIds.Contains(x.DataTypeId));
var propertyTypeIds = Database.Fetch<PropertyTypeDto80>(sqlPropertyTpes).Select(x => x.Id).ToList();
if (propertyTypeIds.Count == 0) return;
var sqlPropertyData = Sql()
.Select<PropertyDataDto>()
.From<PropertyDataDto>()
.Where<PropertyDataDto>(x => propertyTypeIds.Contains(x.PropertyTypeId));
var properties = Database.Fetch<PropertyDataDto>(sqlPropertyData);
// Create a Multi URL Picker datatype for the converted RelatedLinks data
foreach (var property in properties)
{
var value = property.Value?.ToString();
if (string.IsNullOrWhiteSpace(value))
continue;
var relatedLinks = JsonConvert.DeserializeObject<List<RelatedLink>>(value);
var links = new List<LinkDto>();
foreach (var relatedLink in relatedLinks)
{
if (relatedLink == null)
continue;
GuidUdi udi = null;
if (relatedLink.IsInternal)
{
var linkIsUdi = GuidUdi.TryParse(relatedLink.Link, out udi);
if (linkIsUdi == false)
{
// oh no.. probably an integer, yikes!
if (int.TryParse(relatedLink.Link, out var intId))
{
var sqlNodeData = Sql()
.Select<NodeDto>()
.From<NodeDto>()
.Where<NodeDto>(x => x.NodeId == intId);
var node = Database.Fetch<NodeDto>(sqlNodeData).FirstOrDefault();
if (node != null)
// Note: RelatedLinks did not allow for picking media items,
// so if there's a value this will be a content item - hence
// the hardcoded "document" here
udi = new GuidUdi("document", node.UniqueId);
}
}
}
var link = new LinkDto
{
Name = relatedLink.Caption,
Target = relatedLink.NewWindow ? "_blank" : null,
Udi = udi,
// Should only have a URL if it's an external link otherwise it wil be a UDI
Url = relatedLink.IsInternal == false ? relatedLink.Link : null
};
links.Add(link);
}
var json = JsonConvert.SerializeObject(links);
// Update existing data
property.TextValue = json;
Database.Update(property);
}
}
}
internal class RelatedLink
{
public int? Id { get; internal set; }
internal bool IsDeleted { get; set; }
[JsonProperty("caption")]
public string Caption { get; set; }
[JsonProperty("link")]
public string Link { get; set; }
[JsonProperty("newWindow")]
public bool NewWindow { get; set; }
[JsonProperty("isInternal")]
public bool IsInternal { get; set; }
}
[DataContract]
internal class LinkDto
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "target")]
public string Target { get; set; }
[DataMember(Name = "udi")]
public GuidUdi Udi { get; set; }
[DataMember(Name = "url")]
public string Url { get; set; }
}
}