private void UpdatePropertyTypes(IContentTypeComposition contentTypeItem)
        {
            //Loop through the _genericProperties ArrayList and update all existing PropertyTypes
            foreach (GenericPropertyWrapper gpw in _genericProperties)
            {
                if (gpw.PropertyType == null) continue;
                if (contentTypeItem.PropertyTypes == null || contentTypeItem.PropertyTypes.Any(x => x.Alias == gpw.PropertyType.Alias) == false) continue;
                var propertyType = contentTypeItem.PropertyTypes.First(x => x.Alias == gpw.PropertyType.Alias);
                if (propertyType == null) continue;
                var dataTypeDefinition = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionById(gpw.GenricPropertyControl.Type);
                propertyType.Alias = gpw.GenricPropertyControl.Alias;
                propertyType.Name = gpw.GenricPropertyControl.Name;
                propertyType.Description = gpw.GenricPropertyControl.Description;
                propertyType.ValidationRegExp = gpw.GenricPropertyControl.Validation;
                propertyType.Mandatory = gpw.GenricPropertyControl.Mandatory;
                propertyType.DataTypeDatabaseType = dataTypeDefinition.DatabaseType;
                propertyType.DataTypeDefinitionId = dataTypeDefinition.Id;
                propertyType.DataTypeId = dataTypeDefinition.ControlId;
                if (propertyType.PropertyGroupId == null || propertyType.PropertyGroupId.Value != gpw.GenricPropertyControl.Tab)
                {
                    if (gpw.GenricPropertyControl.Tab == 0)
                    {
                        propertyType.PropertyGroupId = new Lazy<int>(() => 0);
                    }
                    else if (contentTypeItem.PropertyGroups.Any(x => x.Id == gpw.GenricPropertyControl.Tab))
                    {
                        propertyType.PropertyGroupId = new Lazy<int>(() => gpw.GenricPropertyControl.Tab);
                    }
                    else if (contentTypeItem.PropertyGroups.Any(x => x.ParentId == gpw.GenricPropertyControl.Tab))
                    {
                        var propertyGroup = contentTypeItem.PropertyGroups.First(x => x.ParentId == gpw.GenricPropertyControl.Tab);
                        propertyType.PropertyGroupId = new Lazy<int>(() => propertyGroup.Id);
                    }
                    else
                    {
                        var propertyGroup = contentTypeItem.CompositionPropertyGroups.First(x => x.Id == gpw.GenricPropertyControl.Tab);
                        contentTypeItem.AddPropertyGroup(propertyGroup.Name);
                        contentTypeItem.MovePropertyType(propertyType.Alias, propertyGroup.Name);
                    }
                }
            }

            //Update the SortOrder of the PropertyTypes
            foreach (HtmlInputHidden propSorter in _sortLists)
            {
                if (propSorter.Value.Trim() != "")
                {
                    string[] newSortOrders = propSorter.Value.Split("&".ToCharArray());
                    for (int i = 0; i < newSortOrders.Length; i++)
                    {
                        var propertyTypeId = int.Parse(newSortOrders[i].Split("=".ToCharArray())[1]);
                        if (contentTypeItem.PropertyTypes != null &&
                            contentTypeItem.PropertyTypes.Any(x => x.Id == propertyTypeId))
                        {
                            var propertyType = contentTypeItem.PropertyTypes.First(x => x.Id == propertyTypeId);
                            propertyType.SortOrder = i;
                        }
                    }
                }
            }
        }
        private void UpdatePropertyTypes(IContentTypeComposition contentTypeItem)
        {
            //Loop through the _genericProperties ArrayList and update all existing PropertyTypes
            foreach (GenericPropertyWrapper gpw in _genericProperties)
            {
                if (gpw.PropertyType == null) continue;
                if (contentTypeItem.PropertyTypes == null || contentTypeItem.PropertyTypes.Any(x => x.Alias == gpw.PropertyType.Alias) == false) continue;
                var propertyType = contentTypeItem.PropertyTypes.First(x => x.Alias == gpw.PropertyType.Alias);
                if (propertyType == null) continue;
                var dataTypeDefinition = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionById(gpw.GenricPropertyControl.Type);
                propertyType.Alias = gpw.GenricPropertyControl.Alias;
                propertyType.Name = gpw.GenricPropertyControl.Name;
                propertyType.Description = gpw.GenricPropertyControl.Description;
                propertyType.ValidationRegExp = gpw.GenricPropertyControl.Validation;
                propertyType.Mandatory = gpw.GenricPropertyControl.Mandatory;
                propertyType.DataTypeDatabaseType = dataTypeDefinition.DatabaseType;
                propertyType.DataTypeDefinitionId = dataTypeDefinition.Id;
                propertyType.DataTypeId = dataTypeDefinition.ControlId;
                if (propertyType.PropertyGroupId == null || propertyType.PropertyGroupId.Value != gpw.GenricPropertyControl.Tab)
                {
                    if (gpw.GenricPropertyControl.Tab == 0)
                    {
                        propertyType.PropertyGroupId = new Lazy<int>(() => 0);
                    }
                    else if (contentTypeItem.PropertyGroups.Any(x => x.Id == gpw.GenricPropertyControl.Tab))
                    {
                        propertyType.PropertyGroupId = new Lazy<int>(() => gpw.GenricPropertyControl.Tab);
                    }
                    else if (contentTypeItem.PropertyGroups.Any(x => x.ParentId == gpw.GenricPropertyControl.Tab))
                    {
                        var propertyGroup = contentTypeItem.PropertyGroups.First(x => x.ParentId == gpw.GenricPropertyControl.Tab);
                        propertyType.PropertyGroupId = new Lazy<int>(() => propertyGroup.Id);
                    }
                    else
                    {
                        var propertyGroup = contentTypeItem.CompositionPropertyGroups.First(x => x.Id == gpw.GenricPropertyControl.Tab);
                        contentTypeItem.AddPropertyGroup(propertyGroup.Name);
                        contentTypeItem.MovePropertyType(propertyType.Alias, propertyGroup.Name);
                    }
                }
                //Is only called to flush cache since gpw.PropertyType.Save() isn't called
                // clear local cache
                cms.businesslogic.cache.Cache.ClearCacheItem("UmbracoPropertyTypeCache" + gpw.PropertyType.Id);
                // clear cache in ContentType
                cms.businesslogic.cache.Cache.ClearCacheItem("ContentType_PropertyTypes_Content:" + contentTypeItem.Id);
                _contentType.ClearVirtualTabs();
            }
        
            //Update the SortOrder of the PropertyTypes
            foreach (HtmlInputHidden propSorter in _sortLists)
            {
                if (propSorter.Value.Trim() != "")
                {
                    string tabId = propSorter.ID;
                    // remove leading "propSort_" and trailing "_Content"
                    tabId = tabId.Substring(9, tabId.Length - 9 - 8);
                    // calc the position of the prop SO i.e. after "t_<tabId>Contents[]="
                    int propSOPosition = "t_".Length + tabId.Length + "Contents[]=".Length + 1;

                    string[] tempSO = propSorter.Value.Split("&".ToCharArray());
                    for (int i = 0; i < tempSO.Length; i++)
                    {
                        string propSO = tempSO[i].Substring(propSOPosition);
                        int propertyTypeId = int.Parse(propSO);
                        if (contentTypeItem.PropertyTypes != null &&
                            contentTypeItem.PropertyTypes.Any(x => x.Id == propertyTypeId))
                        {
                            var propertyType = contentTypeItem.PropertyTypes.First(x => x.Id == propertyTypeId);
                            propertyType.SortOrder = i;
                        }
                    }
                }
            }
        }