Skip to content

Commit 2f29469

Browse files
authored
Merge pull request #17 from umbraco/temp-u4-10998
u4-10998 change innerContentConnector to either lookup contenttypes by guids o…
2 parents 41a3da1 + 4d22ca1 commit 2f29469

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

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

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,24 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
6060
if (innerContent == null)
6161
return null;
6262

63-
var allContentTypes = innerContent.Select(x => x.IcContentTypeAlias)
64-
.Distinct()
65-
.ToDictionary(a => a, a => _contentTypeService.GetContentType(a));
66-
67-
//Ensure all of these content types are found
68-
if (allContentTypes.Values.Any(contentType => contentType == null))
69-
{
70-
throw new InvalidOperationException($"Could not resolve these content types for the Inner Content property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}");
71-
}
72-
73-
//Ensure that these content types have dependencies added
74-
foreach (var contentType in allContentTypes.Values)
75-
{
76-
dependencies.Add(new ArtifactDependency(contentType.GetUdi(), false, ArtifactDependencyMode.Match));
77-
}
78-
79-
foreach (var row in innerContent)
63+
var distinctContentTypes = new Dictionary<GuidUdi, IContentType>();
64+
foreach (var innerContentItem in innerContent)
8065
{
81-
var contentType = allContentTypes[row.IcContentTypeAlias];
66+
var contentType = _contentTypeService.GetContentType(innerContentItem.IcContentTypeGuid);
67+
if (contentType == null)
68+
contentType = _contentTypeService.GetContentType(innerContentItem.IcContentTypeAlias);
69+
if (contentType == null)
70+
throw new InvalidOperationException($"Could not resolve these content types for the Inner Content property with key: {innerContentItem.Key}, and name: {innerContentItem.Name}");
71+
72+
// ensure the content type is added as a unique dependency
73+
var contentTypeUdi = contentType.GetUdi();
74+
if (distinctContentTypes.ContainsKey(contentTypeUdi) == false)
75+
{
76+
distinctContentTypes.Add(contentTypeUdi,contentType);
77+
dependencies.Add(new ArtifactDependency(contentTypeUdi,false,ArtifactDependencyMode.Match));
78+
}
8279

83-
foreach (var key in row.PropertyValues.Keys.ToArray())
80+
foreach (var key in innerContentItem.PropertyValues.Keys.ToArray())
8481
{
8582
var propertyType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == key);
8683

@@ -95,7 +92,7 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
9592

9693
// this should be enough for all other value connectors to work with
9794
// as all they should need is the value, and the property type infos
98-
var mockProperty = new Property(propertyType, row.PropertyValues[key]);
95+
var mockProperty = new Property(propertyType, innerContentItem.PropertyValues[key]);
9996

10097
object parsedValue = propValueConnector.GetValue(mockProperty, dependencies);
10198

@@ -111,7 +108,7 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
111108
parsedValue = parsedValue.ToString();
112109
}
113110

114-
row.PropertyValues[key] = parsedValue;
111+
innerContentItem.PropertyValues[key] = parsedValue;
115112
}
116113
}
117114

@@ -147,21 +144,16 @@ public void SetValue(IContentBase content, string alias, string value)
147144
if (innerContent == null)
148145
return;
149146

150-
var allContentTypes = innerContent.Select(x => x.IcContentTypeAlias)
151-
.Distinct()
152-
.ToDictionary(a => a, a => _contentTypeService.GetContentType(a));
153-
154-
//Ensure all of these content types are found
155-
if (allContentTypes.Values.Any(contentType => contentType == null))
147+
foreach (var innerContentItem in innerContent)
156148
{
157-
throw new InvalidOperationException($"Could not resolve these content types for the Inner Content property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}");
158-
}
149+
var contentType = _contentTypeService.GetContentType(innerContentItem.IcContentTypeGuid);
150+
if (contentType == null)
151+
contentType = _contentTypeService.GetContentType(innerContentItem.IcContentTypeAlias);
152+
if (contentType == null)
153+
throw new InvalidOperationException($"Could not resolve these content types for the Inner Content property with key: {innerContentItem.Key}, and name: {innerContentItem.Name}");
159154

160-
var mocks = new Dictionary<IContentType, IContent>();
161155

162-
foreach (var row in innerContent)
163-
{
164-
var contentType = allContentTypes[row.IcContentTypeAlias];
156+
var mocks = new Dictionary<IContentType, IContent>();
165157

166158
// note
167159
// the way we do it here, doing content.SetValue() several time on the same content, reduces
@@ -176,7 +168,7 @@ public void SetValue(IContentBase content, string alias, string value)
176168
if (!mocks.TryGetValue(contentType, out mockContent))
177169
mockContent = mocks[contentType] = new Content("IC_" + Guid.NewGuid(), -1, contentType);
178170

179-
foreach (var key in row.PropertyValues.Keys.ToArray())
171+
foreach (var key in innerContentItem.PropertyValues.Keys.ToArray())
180172
{
181173
var propertyType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == key);
182174

@@ -189,7 +181,7 @@ public void SetValue(IContentBase content, string alias, string value)
189181
// throws if not found - no need for a null check
190182
var propValueConnector = ValueConnectors.Get(propertyType);
191183

192-
var rowValue = row.PropertyValues[key];
184+
var rowValue = innerContentItem.PropertyValues[key];
193185

194186
if (rowValue != null)
195187
{
@@ -198,7 +190,7 @@ public void SetValue(IContentBase content, string alias, string value)
198190
// integers needs to be converted into strings
199191
if (convertedValue is int)
200192
{
201-
row.PropertyValues[key] = convertedValue.ToString();
193+
innerContentItem.PropertyValues[key] = convertedValue.ToString();
202194
}
203195
else
204196
{
@@ -207,17 +199,17 @@ public void SetValue(IContentBase content, string alias, string value)
207199
var jtokenValue = convertedValue.ToString().DetectIsJson() ? JToken.Parse(convertedValue.ToString()) : null;
208200
if (jtokenValue != null)
209201
{
210-
row.PropertyValues[key] = jtokenValue;
202+
innerContentItem.PropertyValues[key] = jtokenValue;
211203
}
212204
else
213205
{
214-
row.PropertyValues[key] = convertedValue;
206+
innerContentItem.PropertyValues[key] = convertedValue;
215207
}
216208
}
217209
}
218210
else
219211
{
220-
row.PropertyValues[key] = rowValue;
212+
innerContentItem.PropertyValues[key] = rowValue;
221213
}
222214
}
223215
}
@@ -250,6 +242,8 @@ public class InnerContentValue
250242
public string Icon { get; set; }
251243
[JsonProperty("icContentTypeAlias")]
252244
public string IcContentTypeAlias { get; set; }
245+
[JsonProperty("icContentTypeGuid")]
246+
public Guid IcContentTypeGuid { get; set; }
253247

254248
/// <summary>
255249
/// The remaining properties will be serialized to a dictionary

0 commit comments

Comments
 (0)