<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Rdbms;

namespace Umbraco.Core.Persistence.Factories
{
    internal class MemberTypeReadOnlyFactory : IEntityFactory<IMemberType, MemberTypeReadOnlyDto>
    {
        public IMemberType BuildEntity(MemberTypeReadOnlyDto dto)
        {
            var memberType = new MemberType(dto.ParentId)
                             {
                                 Alias = dto.Alias,
                                 AllowedAsRoot = dto.AllowAtRoot,
                                 CreateDate = dto.CreateDate,
                                 CreatorId = dto.UserId.HasValue ? dto.UserId.Value : 0,
                                 Description = dto.Description,
                                 Icon = dto.Icon,
                                 Id = dto.NodeId,
                                 IsContainer = dto.IsContainer,
                                 Key = dto.UniqueId.Value,
                                 Level = dto.Level,
                                 Name = dto.Text,
                                 Path = dto.Path,
                                 SortOrder = dto.SortOrder,
                                 Thumbnail = dto.Thumbnail,
                                 Trashed = dto.Trashed,
                                 UpdateDate = dto.CreateDate,
                                 AllowedContentTypes = Enumerable.Empty<ContentTypeSort>()
                             };

            var propertyTypeGroupCollection = GetPropertyTypeGroupCollection(dto, memberType);
            memberType.PropertyGroups = propertyTypeGroupCollection;

            var propertyTypes = GetPropertyTypes(dto, memberType);
            //By Convention we add 9 stnd PropertyTypes - This is only here to support loading of types that didn't have these conventions before.
            var standardPropertyTypes = Constants.Conventions.Member.StandardPropertyTypeStubs;
            foreach (var standardPropertyType in standardPropertyTypes)
            {
                if(dto.PropertyTypes.Any(x => x.Alias.Equals(standardPropertyType.Key))) continue;
                
                //Add the standard PropertyType to the current list
                propertyTypes.Add(standardPropertyType.Value);

                //Internal dictionary for adding "MemberCanEdit" and "VisibleOnProfile" properties to each PropertyType
                memberType.MemberTypePropertyTypes.Add(standardPropertyType.Key,
                    new Tuple<bool, bool, int>(false, false, default(int)));
            }
            memberType.PropertyTypes = propertyTypes;

            return memberType;
        }

        private PropertyGroupCollection GetPropertyTypeGroupCollection(MemberTypeReadOnlyDto dto, MemberType memberType)
        {
            var propertyGroups = new PropertyGroupCollection();
            foreach (var groupDto in dto.PropertyTypeGroups.Where(x => x.Id.HasValue))
            {
                var group = new PropertyGroup();
                //Only assign an Id if the PropertyGroup belongs to this ContentType
                if (groupDto.Id.HasValue && groupDto.Id == memberType.Id)
                {
                    group.Id = groupDto.Id.Value;

                    if (groupDto.ParentGroupId.HasValue)
                        group.ParentId = groupDto.ParentGroupId.Value;
                }
                else
                {<commit_msg>Fixes an issue with the MemberTypeReadOnlyFactory<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Rdbms;

namespace Umbraco.Core.Persistence.Factories
{
    internal class MemberTypeReadOnlyFactory : IEntityFactory<IMemberType, MemberTypeReadOnlyDto>
    {
        public IMemberType BuildEntity(MemberTypeReadOnlyDto dto)
        {
            var memberType = new MemberType(dto.ParentId)
                             {
                                 Alias = dto.Alias,
                                 AllowedAsRoot = dto.AllowAtRoot,
                                 CreateDate = dto.CreateDate,
                                 CreatorId = dto.UserId.HasValue ? dto.UserId.Value : 0,
                                 Description = dto.Description,
                                 Icon = dto.Icon,
                                 Id = dto.NodeId,
                                 IsContainer = dto.IsContainer,
                                 Key = dto.UniqueId.Value,
                                 Level = dto.Level,
                                 Name = dto.Text,
                                 Path = dto.Path,
                                 SortOrder = dto.SortOrder,
                                 Thumbnail = dto.Thumbnail,
                                 Trashed = dto.Trashed,
                                 UpdateDate = dto.CreateDate,
                                 AllowedContentTypes = Enumerable.Empty<ContentTypeSort>()
                             };

            var propertyTypeGroupCollection = GetPropertyTypeGroupCollection(dto, memberType);
            memberType.PropertyGroups = propertyTypeGroupCollection;

            var propertyTypes = GetPropertyTypes(dto, memberType);
            //By Convention we add 9 stnd PropertyTypes - This is only here to support loading of types that didn't have these conventions before.
            var standardPropertyTypes = Constants.Conventions.Member.StandardPropertyTypeStubs;
            foreach (var standardPropertyType in standardPropertyTypes)
            {
                if(dto.PropertyTypes.Any(x => x.Alias.Equals(standardPropertyType.Key))) continue;
                
                //Add the standard PropertyType to the current list
                propertyTypes.Add(standardPropertyType.Value);

                //Internal dictionary for adding "MemberCanEdit" and "VisibleOnProfile" properties to each PropertyType
                memberType.MemberTypePropertyTypes.Add(standardPropertyType.Key,
                    new Tuple<bool, bool, int>(false, false, default(int)));
            }
            memberType.PropertyTypes = propertyTypes;

            return memberType;
        }

        private PropertyGroupCollection GetPropertyTypeGroupCollection(MemberTypeReadOnlyDto dto, MemberType memberType)
        {
            var propertyGroups = new PropertyGroupCollection();
            foreach (var groupDto in dto.PropertyTypeGroups.Where(x => x.Id.HasValue))
            {
                var group = new PropertyGroup();
               
                //Only assign an Id if the PropertyGroup belongs to this ContentType
                if (groupDto.Id.HasValue 
                    /*SD: I've commented this out since it's never correct, why would the group id be the same as the type??
                     * && groupDto.Id == memberType.Id*/)
                {
                    group.Id = groupDto.Id.Value;

                    if (groupDto.ParentGroupId.HasValue)
                        group.ParentId = groupDto.ParentGroupId.Value;
                }
                else
                {
{'fraction_non_alphanumeric': 0.0637765735723218, 'fraction_numerical': 0.0005557871335278589, 'mean_word_length': 1.4375211649170336, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6000580', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1620, 'n_words': 379}