Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit 9fa0285

Browse files
committed
DocTypeGridEditorHelper - small refactoring + whitespace formatting.
1 parent 6a18015 commit 9fa0285

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

src/Our.Umbraco.DocTypeGridEditor/Helpers/DocTypeGridEditorHelper.cs

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using System.Linq;
44
using Newtonsoft.Json;
55
using Newtonsoft.Json.Linq;
6-
using Our.Umbraco.DocTypeGridEditor.Models;
76
using Our.Umbraco.DocTypeGridEditor.Extensions;
7+
using Our.Umbraco.DocTypeGridEditor.Models;
88
using Umbraco.Core;
99
using Umbraco.Core.Models;
1010
using Umbraco.Core.Models.Editors;
@@ -27,80 +27,80 @@ public static IPublishedContent ConvertValueToContent(string id, string docTypeA
2727
if (string.IsNullOrWhiteSpace(docTypeAlias))
2828
return null;
2929

30-
if (UmbracoContext.Current != null)
31-
{
30+
if (UmbracoContext.Current == null)
31+
return ConvertValue(id, docTypeAlias, dataJson);
32+
3233
return (IPublishedContent)ApplicationContext.Current.ApplicationCache.RequestCache.GetCacheItem(
33-
"DocTypeGridEditorHelper.ConvertValueToContent_" + id + "_" + docTypeAlias, () =>
34+
string.Concat("DocTypeGridEditorHelper.ConvertValueToContent_", id, "_", docTypeAlias),
35+
() =>
3436
{
35-
return ConvertValue(id, docTypeAlias, dataJson);
36-
});
37-
}
38-
return (IPublishedContent) ConvertValue(id, docTypeAlias, dataJson);
37+
return ConvertValue(id, docTypeAlias, dataJson);
38+
});
3939
}
4040

4141
private static IPublishedContent ConvertValue(string id, string docTypeAlias, string dataJson)
4242
{
43-
using (var timer = DisposableTimer.DebugDuration<DocTypeGridEditorHelper>(string.Format("ConvertValueToContent ({0}, {1})", id, docTypeAlias)))
44-
{
45-
Guid docTypeGuid;
46-
if (Guid.TryParse(docTypeAlias, out docTypeGuid))
47-
docTypeAlias = Services.ContentTypeService.GetAliasByGuid(docTypeGuid);
43+
using (var timer = DisposableTimer.DebugDuration<DocTypeGridEditorHelper>(string.Format("ConvertValueToContent ({0}, {1})", id, docTypeAlias)))
44+
{
45+
Guid docTypeGuid;
46+
if (Guid.TryParse(docTypeAlias, out docTypeGuid))
47+
docTypeAlias = Services.ContentTypeService.GetAliasByGuid(docTypeGuid);
4848

49-
var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, docTypeAlias);
50-
var contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(docTypeAlias);
51-
var properties = new List<IPublishedProperty>();
49+
var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, docTypeAlias);
50+
var contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(docTypeAlias);
51+
var properties = new List<IPublishedProperty>();
5252

53-
// Convert all the properties
54-
var data = JsonConvert.DeserializeObject(dataJson);
55-
var propValues = ((JObject) data).ToObject<Dictionary<string, object>>();
56-
foreach (var jProp in propValues)
57-
{
58-
var propType = publishedContentType.GetPropertyType(jProp.Key);
59-
if (propType != null)
60-
{
61-
/* Because we never store the value in the database, we never run the property editors
62-
* "ConvertEditorToDb" method however the property editors will expect their value to
63-
* be in a "DB" state so to get round this, we run the "ConvertEditorToDb" here before
64-
* we go on to convert the value for the view.
65-
*/
66-
var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);
67-
var propPreValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(
68-
propType.DataTypeId);
53+
// Convert all the properties
54+
var data = JsonConvert.DeserializeObject(dataJson);
55+
var propValues = ((JObject)data).ToObject<Dictionary<string, object>>();
56+
foreach (var jProp in propValues)
57+
{
58+
var propType = publishedContentType.GetPropertyType(jProp.Key);
59+
if (propType != null)
60+
{
61+
/* Because we never store the value in the database, we never run the property editors
62+
* "ConvertEditorToDb" method however the property editors will expect their value to
63+
* be in a "DB" state so to get round this, we run the "ConvertEditorToDb" here before
64+
* we go on to convert the value for the view.
65+
*/
66+
var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);
67+
var propPreValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(
68+
propType.DataTypeId);
6969

70-
var contentPropData = new ContentPropertyData(
71-
jProp.Value == null ? null : jProp.Value.ToString(),
72-
propPreValues,
73-
new Dictionary<string, object>());
70+
var contentPropData = new ContentPropertyData(
71+
jProp.Value != null ? jProp.Value.ToString() : null,
72+
propPreValues,
73+
new Dictionary<string, object>());
7474

75-
var newValue = propEditor.ValueEditor.ConvertEditorToDb(contentPropData, jProp.Value);
75+
var newValue = propEditor.ValueEditor.ConvertEditorToDb(contentPropData, jProp.Value);
7676

77-
/* Now that we have the DB stored value, we actually need to then convert it into it's
78-
* XML serialized state as expected by the published property by calling ConvertDbToString
79-
*/
80-
var propType2 = contentType.CompositionPropertyTypes.Single(x => x.Alias == propType.PropertyTypeAlias);
81-
var newValue2 = propEditor.ValueEditor.ConvertDbToString(new Property(propType2, newValue), propType2,
82-
ApplicationContext.Current.Services.DataTypeService);
77+
/* Now that we have the DB stored value, we actually need to then convert it into it's
78+
* XML serialized state as expected by the published property by calling ConvertDbToString
79+
*/
80+
var propType2 = contentType.CompositionPropertyTypes.Single(x => x.Alias == propType.PropertyTypeAlias);
81+
var newValue2 = propEditor.ValueEditor.ConvertDbToString(new Property(propType2, newValue), propType2,
82+
ApplicationContext.Current.Services.DataTypeService);
8383

84-
properties.Add(new DetachedPublishedProperty(propType, newValue2));
85-
}
86-
}
84+
properties.Add(new DetachedPublishedProperty(propType, newValue2));
85+
}
86+
}
8787

88-
// Parse out the name manually
89-
object nameObj = null;
90-
if (propValues.TryGetValue("name", out nameObj))
91-
{
92-
// Do nothing, we just want to parse out the name if we can
93-
}
88+
// Parse out the name manually
89+
object nameObj = null;
90+
if (propValues.TryGetValue("name", out nameObj))
91+
{
92+
// Do nothing, we just want to parse out the name if we can
93+
}
9494

95-
// Get the current request node we are embedded in
96-
var pcr = UmbracoContext.Current == null ? null : UmbracoContext.Current.PublishedContentRequest;
97-
var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;
95+
// Get the current request node we are embedded in
96+
var pcr = UmbracoContext.Current != null ? UmbracoContext.Current.PublishedContentRequest : null;
97+
var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;
9898

99-
return new DetachedPublishedContent(nameObj == null ? null : nameObj.ToString(),
100-
publishedContentType,
101-
properties.ToArray(),
102-
containerNode);
103-
}
99+
return new DetachedPublishedContent(nameObj != null ? nameObj.ToString() : null,
100+
publishedContentType,
101+
properties.ToArray(),
102+
containerNode);
103+
}
104104

105105
}
106106
}

0 commit comments

Comments
 (0)