3
3
using System . Linq ;
4
4
using Newtonsoft . Json ;
5
5
using Newtonsoft . Json . Linq ;
6
- using Our . Umbraco . DocTypeGridEditor . Models ;
7
6
using Our . Umbraco . DocTypeGridEditor . Extensions ;
7
+ using Our . Umbraco . DocTypeGridEditor . Models ;
8
8
using Umbraco . Core ;
9
9
using Umbraco . Core . Models ;
10
10
using Umbraco . Core . Models . Editors ;
@@ -27,80 +27,80 @@ public static IPublishedContent ConvertValueToContent(string id, string docTypeA
27
27
if ( string . IsNullOrWhiteSpace ( docTypeAlias ) )
28
28
return null ;
29
29
30
- if ( UmbracoContext . Current != null )
31
- {
30
+ if ( UmbracoContext . Current == null )
31
+ return ConvertValue ( id , docTypeAlias , dataJson ) ;
32
+
32
33
return ( IPublishedContent ) ApplicationContext . Current . ApplicationCache . RequestCache . GetCacheItem (
33
- "DocTypeGridEditorHelper.ConvertValueToContent_" + id + "_" + docTypeAlias , ( ) =>
34
+ string . Concat ( "DocTypeGridEditorHelper.ConvertValueToContent_" , id , "_" , docTypeAlias ) ,
35
+ ( ) =>
34
36
{
35
- return ConvertValue ( id , docTypeAlias , dataJson ) ;
36
- } ) ;
37
- }
38
- return ( IPublishedContent ) ConvertValue ( id , docTypeAlias , dataJson ) ;
37
+ return ConvertValue ( id , docTypeAlias , dataJson ) ;
38
+ } ) ;
39
39
}
40
40
41
41
private static IPublishedContent ConvertValue ( string id , string docTypeAlias , string dataJson )
42
42
{
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 ) ;
48
48
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 > ( ) ;
52
52
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 ) ;
69
69
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 > ( ) ) ;
74
74
75
- var newValue = propEditor . ValueEditor . ConvertEditorToDb ( contentPropData , jProp . Value ) ;
75
+ var newValue = propEditor . ValueEditor . ConvertEditorToDb ( contentPropData , jProp . Value ) ;
76
76
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 ) ;
83
83
84
- properties . Add ( new DetachedPublishedProperty ( propType , newValue2 ) ) ;
85
- }
86
- }
84
+ properties . Add ( new DetachedPublishedProperty ( propType , newValue2 ) ) ;
85
+ }
86
+ }
87
87
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
+ }
94
94
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 ;
98
98
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
+ }
104
104
105
105
}
106
106
}
0 commit comments