<commit_before>﻿using NicBell.UCreate.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

namespace NicBell.UCreate.Helpers
{
    public abstract class BaseContentTypeHelper<T> : BaseTypeHelper<T> where T : BaseContentTypeAttribute
    {
        /// <summary>
        /// Service
        /// </summary>
        public IContentTypeService Service
        {
            get
            {
                return ApplicationContext.Current.Services.ContentTypeService;
            }
        }


        public abstract IContentTypeComposition GetByAlias(string alias);

        public abstract void Save(Type itemType);

        public abstract void SaveAllowedTypes(Type itemType);


        /// <summary>
        /// Sync all items
        /// </summary>
        public override void SyncAll()
        {
            var firstLevelTypes = TypesToSync.Where(x => x.BaseType == null || x.BaseType == typeof(Object));

            foreach (var itemType in firstLevelTypes)
            {
                SyncItem(itemType, true); //Deep sync
            }

            foreach (var itemType in TypesToSync)
            {
                SaveAllowedTypes(itemType);
            }
        }


        /// <summary>
        /// Sync single item
        /// </summary>
        /// <param name="itemType"></param>
        /// <param name="syncChildren"></param>
        public void SyncItem(Type itemType, bool syncChildren = false)
        {
            Save(itemType);

            if (syncChildren)
            {
                var childTypes = TypesToSync.Where(x => x.BaseType == itemType);

                foreach (var childType in childTypes)
                {
                    SyncItem(childType, syncChildren);
                }
            }
        }


        /// <summary>
        /// Maps properties
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="itemType"></param>
        /// <param name="overwrite"></param>
        protected void MapProperties(IContentTypeBase ct, Type itemType)
        {
            foreach (PropertyInfo propInfo in itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                PropertyAttribute propAttr = Attribute.GetCustomAttribute(propInfo, typeof(PropertyAttribute), false) as PropertyAttribute;

                if (propAttr != null)
                {
                    ct.RemovePropertyType(propAttr.Alias);

                    if (!String.IsNullOrEmpty(propAttr.TabName))
                    {
                        ct.AddPropertyGroup(propAttr.TabName);
                        ct.AddPropertyType(propAttr.GetPropertyType(), propAttr.TabName);
                    }
                    else
                    {
                        ct.AddPropertyType(propAttr.GetPropertyType());
                    }
                }
            }
        }


        /// <summary>
        /// Maps allowed children
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="allowedTypeAliases"></param>
        protected void MapAllowedTypes(IContentTypeBase ct, string[] allowedTypeAliases)<commit_msg>Ability to update existing property when alias remains the same...<commit_after>﻿using NicBell.UCreate.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

namespace NicBell.UCreate.Helpers
{
    public abstract class BaseContentTypeHelper<T> : BaseTypeHelper<T> where T : BaseContentTypeAttribute
    {
        /// <summary>
        /// Service
        /// </summary>
        public IContentTypeService Service
        {
            get
            {
                return ApplicationContext.Current.Services.ContentTypeService;
            }
        }


        public abstract IContentTypeComposition GetByAlias(string alias);

        public abstract void Save(Type itemType);

        public abstract void SaveAllowedTypes(Type itemType);


        /// <summary>
        /// Sync all items
        /// </summary>
        public override void SyncAll()
        {
            var firstLevelTypes = TypesToSync.Where(x => x.BaseType == null || x.BaseType == typeof(Object));

            foreach (var itemType in firstLevelTypes)
            {
                SyncItem(itemType, true); //Deep sync
            }

            foreach (var itemType in TypesToSync)
            {
                SaveAllowedTypes(itemType);
            }
        }


        /// <summary>
        /// Sync single item
        /// </summary>
        /// <param name="itemType"></param>
        /// <param name="syncChildren"></param>
        public void SyncItem(Type itemType, bool syncChildren = false)
        {
            Save(itemType);

            if (syncChildren)
            {
                var childTypes = TypesToSync.Where(x => x.BaseType == itemType);

                foreach (var childType in childTypes)
                {
                    SyncItem(childType, syncChildren);
                }
            }
        }


        /// <summary>
        /// Maps properties
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="itemType"></param>
        /// <param name="overwrite"></param>
        protected void MapProperties(IContentTypeBase ct, Type itemType)
        {
            foreach (PropertyInfo propInfo in itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                PropertyAttribute propAttr = Attribute.GetCustomAttribute(propInfo, typeof(PropertyAttribute), false) as PropertyAttribute;

                if (propAttr != null)
                {
                    var newProp = propAttr.GetPropertyType();

                    if (ct.PropertyTypeExists(propAttr.Alias))
                    {
                        var existingProp = ct.PropertyTypes.First(x => x.Alias == propAttr.Alias);

                        existingProp.DataTypeDefinitionId = newProp.DataTypeDefinitionId;
                        existingProp.Name = newProp.Name;
                        existingProp.Description = newProp.Description;
                        existingProp.Mandatory = newProp.Mandatory;
                        existingProp.ValidationRegExp = newProp.ValidationRegExp;

                        if (!String.IsNullOrEmpty(propAttr.TabName))
                        {
                            ct.MovePropertyType(propAttr.Alias, propAttr.TabName);
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(propAttr.TabName))
                        {
                            ct.AddPropertyGroup(propAttr.TabName);
                            ct.AddPropertyType(newProp, propAttr.TabName);
                        }
                        else
                        {
                            ct.AddPropertyType(newProp);
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Maps allowed children
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="allowedTypeAliases"></param>
        protected void MapAllowedTypes(IContentTypeBase ct, string[] allowedTypeAliases)
{'fraction_non_alphanumeric': 0.09790861862261889, 'fraction_numerical': 0.0, 'mean_word_length': 1.456806282722513, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 60, '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': '1683997', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1744, 'n_words': 368}