<commit_before>﻿using NicBell.UCreate.Attributes;
using NicBell.UCreate.Helpers;
using System;
using System.ComponentModel;
using System.Reflection;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;

namespace NicBell.UCreate.Models
{
    public abstract class BaseDocType : PublishedContentModel
    {
        public BaseDocType(IPublishedContent content)
            : base(content)
        {
            SetValues(content);
        }


        /// <summary>
        /// Gets all properties with PropertyAttribute and sets their value
        /// </summary>
        /// <param name="content"></param>
        private void SetValues(IPublishedContent content)
        {
            var properties = ReflectionHelper.GetPropertiesWithAttribute<PropertyAttribute>(GetType(), bindingFlags: BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties)
            {
                var alias = property.GetCustomAttribute<PropertyAttribute>(false).Alias;
                var publishedProperty = content.GetProperty(alias);

                //Local links cause stack overflow when accessing value during publish.
                //Use @Html.Raw(TemplateUtilities.ParseInternalLinks(value)) to resolve these URLs on the template.
                if (publishedProperty.DataValue is string &&
                    ((publishedProperty.DataValue as string).Contains("localLink") || (publishedProperty.DataValue as string).Contains("umb://")))
                {
                    SetValue(property, publishedProperty.DataValue);
                }
                else
                {
                    SetValue(property, publishedProperty.Value);
                }
            }
        }


        /// <summary>
        /// Set value for a property.
        /// </summary>
        /// <param name="property"></param>
        /// <param name="value"></param>
        private void SetValue(PropertyInfo property, object value)
        {
            //Check for type converter..
            if (property.IsDefined(typeof(TypeConverterAttribute)))
            {
                var converterAttr = property.GetCustomAttribute<TypeConverterAttribute>();
                var converter = Activator.CreateInstance(Type.GetType(converterAttr.ConverterTypeName)) as TypeConverter;
                property.SetValue(this, converter.ConvertFrom(value ?? string.Empty));
            }
            else
            {
                var convert = value.TryConvertTo(property.PropertyType);
                if (convert.Success)
                    property.SetValue(this, convert.Result);
            }
        }
    }
}
<commit_msg>Fixed a problem when setting values of doctype properties containing macros.<commit_after>﻿using NicBell.UCreate.Attributes;
using NicBell.UCreate.Helpers;
using System;
using System.ComponentModel;
using System.Reflection;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;

namespace NicBell.UCreate.Models
{
    public abstract class BaseDocType : PublishedContentModel
    {
        public BaseDocType(IPublishedContent content)
            : base(content)
        {
            SetValues(content);
        }


        /// <summary>
        /// Gets all properties with PropertyAttribute and sets their value
        /// </summary>
        /// <param name="content"></param>
        private void SetValues(IPublishedContent content)
        {
            var properties = ReflectionHelper.GetPropertiesWithAttribute<PropertyAttribute>(GetType(), bindingFlags: BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties)
            {
                var alias = property.GetCustomAttribute<PropertyAttribute>(false).Alias;
                var publishedProperty = content.GetProperty(alias);

                // Local links cause stack overflow when accessing value during publish.
                // Use @Html.Raw(TemplateUtilities.ParseInternalLinks(value)) to resolve these URLs on the template.
                // Use DataValue when property contains a macro, otherwise this error occurs:
                //     "Cannot render a macro when there is no current PublishedContentRequest."
                if (publishedProperty.DataValue is string &&
                    ((publishedProperty.DataValue as string).Contains("localLink") || (publishedProperty.DataValue as string).Contains("umb://") ||
                     (publishedProperty.DataValue as string).Contains("UMBRACO_MACRO")))
                {
                    SetValue(property, publishedProperty.DataValue);
                }
                else
                {
                    SetValue(property, publishedProperty.Value);
                }
            }
        }


        /// <summary>
        /// Set value for a property.
        /// </summary>
        /// <param name="property"></param>
        /// <param name="value"></param>
        private void SetValue(PropertyInfo property, object value)
        {
            //Check for type converter..
            if (property.IsDefined(typeof(TypeConverterAttribute)))
            {
                var converterAttr = property.GetCustomAttribute<TypeConverterAttribute>();
                var converter = Activator.CreateInstance(Type.GetType(converterAttr.ConverterTypeName)) as TypeConverter;
                property.SetValue(this, converter.ConvertFrom(value ?? string.Empty));
            }
            else
            {
                var convert = value.TryConvertTo(property.PropertyType);
                if (convert.Success)
                    property.SetValue(this, convert.Result);
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.09243111266131845, 'fraction_numerical': 0.0, 'mean_word_length': 1.9823192927717108, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3852521', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1274, 'n_words': 311}