public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService) { if (property.Value == null || string.IsNullOrWhiteSpace(property.Value.ToString())) { return(string.Empty); } var value = JsonConvert.DeserializeObject <List <object> >(property.Value.ToString()); if (value == null) { return(string.Empty); } // Process value PreValueCollection preValues = null; for (var i = 0; i < value.Count; i++) { var o = value[i]; var propValues = ((JObject)o); // convert from old style (v0.1.1) data format if necessary NestedContentHelper.ConvertItemValueFromV011(propValues, propertyType.DataTypeDefinitionId, ref preValues); var contentType = NestedContentHelper.GetContentTypeFromItem(propValues); if (contentType == null) { continue; } var propValueKeys = propValues.Properties().Select(x => x.Name).ToArray(); foreach (var propKey in propValueKeys) { var propType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == propKey); if (propType == null) { if (IsSystemPropertyKey(propKey) == false) { // Property missing so just delete the value propValues[propKey] = null; } } else { // Create a fake property using the property abd stored value var prop = new Property(propType, propValues[propKey] == null ? null : propValues[propKey].ToString()); // Lookup the property editor var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias); // Get the editor to do it's conversion var newValue = propEditor.ValueEditor.ConvertDbToEditor(prop, propType, ApplicationContext.Current.Services.DataTypeService); // Store the value back propValues[propKey] = (newValue == null) ? null : JToken.FromObject(newValue); } } } // Update the value on the property property.Value = JsonConvert.SerializeObject(value); // Pass the call down return(base.ConvertDbToEditor(property, propertyType, dataTypeService)); }
public override string ConvertDbToString(Property property, PropertyType propertyType, IDataTypeService dataTypeService) { // Convert / validate value if (property.Value == null || string.IsNullOrWhiteSpace(property.Value.ToString())) { return(string.Empty); } var value = JsonConvert.DeserializeObject <List <object> >(property.Value.ToString()); if (value == null) { return(string.Empty); } // Process value PreValueCollection preValues = null; for (var i = 0; i < value.Count; i++) { var o = value[i]; var propValues = ((JObject)o); // convert from old style (v0.1.1) data format if necessary NestedContentHelper.ConvertItemValueFromV011(propValues, propertyType.DataTypeDefinitionId, ref preValues); var contentType = NestedContentHelper.GetContentTypeFromItem(propValues); if (contentType == null) { continue; } var propValueKeys = propValues.Properties().Select(x => x.Name).ToArray(); foreach (var propKey in propValueKeys) { var propType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == propKey); if (propType == null) { if (IsSystemPropertyKey(propKey) == false) { // Property missing so just delete the value propValues[propKey] = null; } } else { try { // Create a fake property using the property abd stored value var prop = new Property(propType, propValues[propKey] == null ? null : propValues[propKey].ToString()); // Lookup the property editor var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias); // Get the editor to do it's conversion, and store it back propValues[propKey] = propEditor.ValueEditor.ConvertDbToString(prop, propType, dataTypeService); } catch (InvalidOperationException) { // https://github.com/umco/umbraco-nested-content/issues/111 // Catch any invalid cast operations as likely means courier failed due to missing // or trashed item so couldn't convert a guid back to an int propValues[propKey] = null; } } } } // Update the value on the property property.Value = JsonConvert.SerializeObject(value); // Pass the call down return(base.ConvertDbToString(property, propertyType, dataTypeService)); }
public static object ConvertPropertyToNestedContent(this PublishedPropertyType propertyType, object source, bool preview) { using (DisposableTimer.DebugDuration <PublishedPropertyType>(string.Format("ConvertPropertyToNestedContent ({0})", propertyType.DataTypeId))) { if (source != null && !source.ToString().IsNullOrWhiteSpace()) { var rawValue = JsonConvert.DeserializeObject <List <object> >(source.ToString()); var processedValue = new List <IPublishedContent>(); var preValueCollection = NestedContentHelper.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeId); var preValueDictionary = preValueCollection.PreValuesAsDictionary.ToDictionary(x => x.Key, x => x.Value.Value); for (var i = 0; i < rawValue.Count; i++) { var item = (JObject)rawValue[i]; // Convert from old style (v.0.1.1) data format if necessary // - Please note: This call has virtually no impact on rendering performance for new style (>v0.1.1). // Even so, this should be removed eventually, when it's safe to assume that there is // no longer any need for conversion. NestedContentHelper.ConvertItemValueFromV011(item, propertyType.DataTypeId, ref preValueCollection); var contentTypeAlias = NestedContentHelper.GetContentTypeAliasFromItem(item); if (string.IsNullOrEmpty(contentTypeAlias)) { continue; } var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias); if (publishedContentType == null) { continue; } var propValues = item.ToObject <Dictionary <string, object> >(); var properties = new List <IPublishedProperty>(); foreach (var jProp in propValues) { var propType = publishedContentType.GetPropertyType(jProp.Key); if (propType != null) { properties.Add(new DetachedPublishedProperty(propType, jProp.Value, preview)); } } // Parse out the name manually object nameObj = null; if (propValues.TryGetValue("name", out nameObj)) { // Do nothing, we just want to parse out the name if we can } // Get the current request node we are embedded in var pcr = UmbracoContext.Current == null ? null : UmbracoContext.Current.PublishedContentRequest; var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null; // Create the model based on our implementation of IPublishedContent IPublishedContent content = new DetachedPublishedContent( nameObj == null ? null : nameObj.ToString(), publishedContentType, properties.ToArray(), containerNode, i, preview); if (PublishedContentModelFactoryResolver.HasCurrent && PublishedContentModelFactoryResolver.Current.HasValue) { // Let the current model factory create a typed model to wrap our model content = PublishedContentModelFactoryResolver.Current.Factory.CreateModel(content); } // Add the (typed) model as a result processedValue.Add(content); } if (propertyType.IsSingleNestedContentProperty()) { return(processedValue.FirstOrDefault()); } return(processedValue); } } return(null); }
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) { try { using (DisposableTimer.DebugDuration <NestedContentValueConverter>(string.Format("ConvertDataToSource ({0})", propertyType.DataTypeId))) { if (source != null && !source.ToString().IsNullOrWhiteSpace()) { var rawValue = JsonConvert.DeserializeObject <List <object> >(source.ToString()); var processedValue = new List <IPublishedContent>(); var preValueCollection = NestedContentHelper.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeId); var preValueDictionary = preValueCollection.AsPreValueDictionary(); for (var i = 0; i < rawValue.Count; i++) { var item = (JObject)rawValue[i]; // Convert from old style (v.0.1.1) data format if necessary // - Please note: This call has virtually no impact on rendering performance for new style (>v0.1.1). // Even so, this should be removed eventually, when it's safe to assume that there is // no longer any need for conversion. NestedContentHelper.ConvertItemValueFromV011(item, propertyType.DataTypeId, ref preValueCollection); var contentTypeAlias = NestedContentHelper.GetContentTypeAliasFromItem(item); if (string.IsNullOrEmpty(contentTypeAlias)) { continue; } var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias); if (publishedContentType == null) { continue; } var propValues = item.ToObject <Dictionary <string, object> >(); var properties = new List <IPublishedProperty>(); foreach (var jProp in propValues) { var propType = publishedContentType.GetPropertyType(jProp.Key); if (propType != null) { properties.Add(new DetachedPublishedProperty(propType, jProp.Value)); } } // Parse out the name manually object nameObj = null; if (propValues.TryGetValue("name", out nameObj)) { // Do nothing, we just want to parse out the name if we can } processedValue.Add(new DetachedPublishedContent( nameObj == null ? null : nameObj.ToString(), publishedContentType, properties.ToArray(), i)); } // Detect min/max items == 1 and just return a single IPublishedContent int minItems, maxItems; if (preValueDictionary.ContainsKey("minItems") && int.TryParse(preValueDictionary["minItems"], out minItems) && minItems == 1 && preValueDictionary.ContainsKey("maxItems") && int.TryParse(preValueDictionary["maxItems"], out maxItems) && maxItems == 1) { return(processedValue.FirstOrDefault()); } return(processedValue); } } } catch (Exception e) { LogHelper.Error <NestedContentValueConverter>("Error converting value", e); } return(null); }