Skip to content

Commit 69effc7

Browse files
author
Claus
committed
deploy-385 UrlPicker connector needs to be fixed like the Courier resolver
1 parent cd8e6bb commit 69effc7

File tree

2 files changed

+89
-13
lines changed

2 files changed

+89
-13
lines changed

src/Umbraco.Deploy.Contrib.Connectors/Umbraco.Deploy.Contrib.Connectors.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
<Reference Include="System.Threading.Tasks.Dataflow, Version=4.6.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
162162
<HintPath>..\packages\System.Threading.Tasks.Dataflow.4.7.0\lib\portable-net45+win8+wpa81\System.Threading.Tasks.Dataflow.dll</HintPath>
163163
</Reference>
164+
<Reference Include="System.Web" />
164165
<Reference Include="System.Web.Cors, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
165166
<HintPath>..\packages\Microsoft.AspNet.Cors.5.0.0\lib\net45\System.Web.Cors.dll</HintPath>
166167
</Reference>

src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/UrlPickerValueConnector.cs

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Web.Hosting;
36
using Newtonsoft.Json;
47
using Umbraco.Core;
58
using Umbraco.Core.Deploy;
@@ -16,6 +19,27 @@ public class UrlPickerValueConnector : IValueConnector
1619
{
1720
private readonly IEntityService _entityService;
1821

22+
private readonly Version UrlPickerVersionStoringArray = new Version(0, 15, 0, 1);
23+
24+
private static Lazy<Version> UrlPickerVersion
25+
{
26+
get
27+
{
28+
return new Lazy<Version>(() =>
29+
{
30+
// we cannot just get the assembly and get version from that, as we need to check the FileVersion
31+
// (apparently that is different from the assembly version...)
32+
var urlPickerAssemblyPath = HostingEnvironment.MapPath("~/bin/UrlPicker.dll");
33+
if (System.IO.File.Exists(urlPickerAssemblyPath))
34+
{
35+
var fileVersionInfo = FileVersionInfo.GetVersionInfo(urlPickerAssemblyPath);
36+
return new Version(fileVersionInfo.FileVersion);
37+
}
38+
return null;
39+
});
40+
}
41+
}
42+
1943
/// <summary>
2044
/// Initializes a new instance of the <see cref="UrlPickerValueConnector"/> class.
2145
/// </summary>
@@ -28,7 +52,7 @@ public UrlPickerValueConnector(IEntityService entityService)
2852
}
2953

3054
/// <inheritdoc/>
31-
public virtual IEnumerable<string> PropertyEditorAliases => new[] { "Imulus.UrlPicker" };
55+
public virtual IEnumerable<string> PropertyEditorAliases => new[] {"Imulus.UrlPicker"};
3256

3357
/// <inheritdoc/>
3458
public string GetValue(Property property, ICollection<ArtifactDependency> dependencies)
@@ -37,14 +61,30 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
3761
if (string.IsNullOrWhiteSpace(svalue))
3862
return string.Empty;
3963

40-
var urlPickerPropertyData = JsonConvert.DeserializeObject<IEnumerable<UrlPickerPropertyData>>(svalue);
41-
foreach (var urlPicker in urlPickerPropertyData)
64+
IEnumerable<UrlPickerPropertyData> urlPickerPropertyDatas;
65+
66+
// If using an old version of the UrlPicker - data is serialized as a single object
67+
// Otherwise data is serialized as an array of objects, even if only one is selected.
68+
if (UrlPickerVersion.Value < UrlPickerVersionStoringArray)
69+
{
70+
var urlPickerPropertyData = JsonConvert.DeserializeObject<UrlPickerPropertyData>(svalue);
71+
urlPickerPropertyDatas = new List<UrlPickerPropertyData> {urlPickerPropertyData};
72+
}
73+
else
74+
{
75+
urlPickerPropertyDatas = JsonConvert.DeserializeObject<IEnumerable<UrlPickerPropertyData>>(svalue)
76+
.ToList();
77+
}
78+
79+
80+
foreach (var urlPicker in urlPickerPropertyDatas)
4281
{
4382
// If the contentId/mediaId of the TypeData is set try get the GuidUdi for the content/media and
4483
// mark it as a dependency we need to deploy.
4584
// We need the Guid for the content/media because the integer value could be different in the different environments.
4685
GuidUdi contentGuidUdi;
47-
if (TryGetGuidUdi(urlPicker.TypeData.ContentId, UmbracoObjectTypes.Document, Constants.UdiEntityType.Document, out contentGuidUdi))
86+
if (TryGetGuidUdi(urlPicker.TypeData.ContentId, UmbracoObjectTypes.Document,
87+
Constants.UdiEntityType.Document, out contentGuidUdi))
4888
{
4989
dependencies.Add(new ArtifactDependency(contentGuidUdi, false, ArtifactDependencyMode.Exist));
5090
urlPicker.TypeData.ContentId = contentGuidUdi.Guid;
@@ -53,14 +93,20 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
5393
// The picker can have values set for both content and media even though only one of them is "active".
5494
// We still need to resolve the value for both settings.
5595
GuidUdi mediaGuidUdi;
56-
if (TryGetGuidUdi(urlPicker.TypeData.MediaId, UmbracoObjectTypes.Media, Constants.UdiEntityType.Media, out mediaGuidUdi))
96+
if (TryGetGuidUdi(urlPicker.TypeData.MediaId, UmbracoObjectTypes.Media, Constants.UdiEntityType.Media,
97+
out mediaGuidUdi))
5798
{
5899
dependencies.Add(new ArtifactDependency(mediaGuidUdi, false, ArtifactDependencyMode.Exist));
59100
urlPicker.TypeData.MediaId = mediaGuidUdi.Guid;
60101
}
61102
}
62-
63-
return JsonConvert.SerializeObject(urlPickerPropertyData);
103+
// If using an old version of the UrlPicker - data is serialized as a single object
104+
// Otherwise data is serialized as an array of objects, even if only one is selected.
105+
if (UrlPickerVersion.Value < UrlPickerVersionStoringArray)
106+
{
107+
return JsonConvert.SerializeObject(urlPickerPropertyDatas.FirstOrDefault());
108+
}
109+
return JsonConvert.SerializeObject(urlPickerPropertyDatas);
64110
}
65111

66112
/// <inheritdoc/>
@@ -72,8 +118,21 @@ public void SetValue(IContentBase content, string alias, string value)
72118
return;
73119
}
74120

75-
var urlPickerPropertyData = JsonConvert.DeserializeObject<IEnumerable<UrlPickerPropertyData>>(value);
76-
foreach (var urlPicker in urlPickerPropertyData)
121+
IEnumerable<UrlPickerPropertyData> urlPickerPropertyDatas;
122+
123+
// If using an old version of the UrlPicker - data is serialized as a single object
124+
// Otherwise data is serialized as an array of objects, even if only one is selected.
125+
if (UrlPickerVersion.Value < UrlPickerVersionStoringArray)
126+
{
127+
var urlPickerPropertyData = JsonConvert.DeserializeObject<UrlPickerPropertyData>(value);
128+
urlPickerPropertyDatas = new List<UrlPickerPropertyData> {urlPickerPropertyData};
129+
}
130+
else
131+
{
132+
urlPickerPropertyDatas = JsonConvert.DeserializeObject<IEnumerable<UrlPickerPropertyData>>(value).ToList();
133+
}
134+
135+
foreach (var urlPicker in urlPickerPropertyDatas)
77136
{
78137
// When we set the value we want to switch the Guid value of the contentId/mediaId to the integervalue
79138
// as this is what the UrlPicker uses to lookup it's content/media
@@ -87,11 +146,21 @@ public void SetValue(IContentBase content, string alias, string value)
87146
if (TryGetId(urlPicker.TypeData.MediaId, UmbracoObjectTypes.Media, out mediaId))
88147
urlPicker.TypeData.MediaId = mediaId;
89148
}
90-
91-
content.SetValue(alias, JsonConvert.SerializeObject(urlPickerPropertyData));
149+
150+
// If using an old version of the UrlPicker - data is serialized as a single object
151+
// Otherwise data is serialized as an array of objects, even if only one is selected.
152+
if (UrlPickerVersion.Value < UrlPickerVersionStoringArray)
153+
{
154+
content.SetValue(alias, JsonConvert.SerializeObject(urlPickerPropertyDatas.FirstOrDefault()));
155+
}
156+
else
157+
{
158+
content.SetValue(alias, JsonConvert.SerializeObject(urlPickerPropertyDatas));
159+
}
92160
}
93161

94-
private bool TryGetGuidUdi(object value, UmbracoObjectTypes umbracoObjectType, string entityType, out GuidUdi udi)
162+
private bool TryGetGuidUdi(object value, UmbracoObjectTypes umbracoObjectType, string entityType,
163+
out GuidUdi udi)
95164
{
96165
int id;
97166
if (value != null && int.TryParse(value.ToString(), out id))
@@ -127,10 +196,13 @@ internal class UrlPickerPropertyData
127196
{
128197
[JsonProperty("type")]
129198
public string Type { get; set; }
199+
130200
[JsonProperty("meta")]
131201
public Meta Meta { get; set; }
202+
132203
[JsonProperty("typeData")]
133204
public TypeData TypeData { get; set; }
205+
134206
[JsonProperty("disabled")]
135207
public bool Disabled { get; set; }
136208
}
@@ -139,6 +211,7 @@ internal class Meta
139211
{
140212
[JsonProperty("title")]
141213
public string Title { get; set; }
214+
142215
[JsonProperty("newWindow")]
143216
public bool NewWindow { get; set; }
144217
}
@@ -147,10 +220,12 @@ internal class TypeData
147220
{
148221
[JsonProperty("url")]
149222
public string Url { get; set; }
223+
150224
[JsonProperty("contentId")]
151225
public object ContentId { get; set; }
226+
152227
[JsonProperty("mediaId")]
153228
public object MediaId { get; set; }
154229
}
155230
}
156-
}
231+
}

0 commit comments

Comments
 (0)