private void finalizeCodeActivity_Finalize_ExecuteCode(object sender, EventArgs e)
        {
            DataTypeDescriptor dataTypeDescriptor = GetDataTypeDescriptor();

            // Making changes to type
            DataTypeDescriptor newDataTypeDescriptor = dataTypeDescriptor.Clone();

            newDataTypeDescriptor.AddSuperInterface(typeof(ILocalizedControlled));

            List <CultureInfo> localesToCopyTo = new List <CultureInfo>();

            if (ThereAreReferencesInLocalizedData())
            {
                localesToCopyTo.AddRange(DataLocalizationFacade.ActiveLocalizationCultures);
            }
            else
            {
                string cultureName = this.GetBinding <string>("CultureName");
                localesToCopyTo.Add(CultureInfo.CreateSpecificCulture(cultureName));
            }

            var updateDataTypeDescriptor = new UpdateDataTypeDescriptor(dataTypeDescriptor, newDataTypeDescriptor, false)
            {
                LocalesToCopyTo = localesToCopyTo
            };

            GeneratedTypesFacade.UpdateType(updateDataTypeDescriptor);

            this.CloseCurrentView();
            this.CollapseAndRefresh();
        }
        //private DataFieldDescriptor BuildKeyFieldDescriptor()
        //{
        //    DataFieldDescriptor result;

        //    switch (_keyFieldType)
        //    {
        //        case KeyFieldType.Guid:
        //            result = BuildIdField();
        //            break;
        //        case KeyFieldType.RandomString4:
        //            result = new DataFieldDescriptor(Guid.NewGuid(), IdFieldName, StoreFieldType.String(22), typeof (string))
        //            {
        //                DefaultValue = DefaultValue.RandomString(4, true)
        //            };
        //            break;
        //        case KeyFieldType.RandomString8:
        //            result = new DataFieldDescriptor(Guid.NewGuid(), IdFieldName, StoreFieldType.String(22), typeof (string))
        //            {
        //                DefaultValue = DefaultValue.RandomString(8, false)
        //            };
        //            break;
        //        default: throw new InvalidOperationException("Not supported key field type value");
        //    }

        //    result.Position = -1;

        //    return result;
        //}

        private DataTypeDescriptor CreateUpdatedDataTypeDescriptor()
        {
            var dataTypeDescriptor = new DataTypeDescriptor(_oldDataTypeDescriptor.DataTypeId, _newTypeNamespace, _newTypeName, true)
            {
                Cachable   = _cacheable,
                Searchable = _searchable
            };

            dataTypeDescriptor.DataScopes.Add(DataScopeIdentifier.Public);

            Type[] indirectlyInheritedInterfaces =
            {
                typeof(IPublishControlled), typeof(ILocalizedControlled),
                typeof(IPageData),          typeof(IPageFolderData), typeof(IPageMetaData)
            };

            // Foreign interfaces should stay inherited
            foreach (var superInterface in _oldDataTypeDescriptor.SuperInterfaces)
            {
                if (!indirectlyInheritedInterfaces.Contains(superInterface))
                {
                    dataTypeDescriptor.AddSuperInterface(superInterface);
                }
            }

            if (_publishControlled && _dataAssociationType != DataAssociationType.Composition)
            {
                dataTypeDescriptor.AddSuperInterface(typeof(IPublishControlled));
            }

            if (_localizedControlled && _dataAssociationType != DataAssociationType.Composition)
            {
                dataTypeDescriptor.AddSuperInterface(typeof(ILocalizedControlled));
            }

            if (_oldDataTypeDescriptor.SuperInterfaces.Contains(typeof(IPageFolderData)))
            {
                dataTypeDescriptor.AddSuperInterface(typeof(IPageData));
                dataTypeDescriptor.AddSuperInterface(typeof(IPageFolderData));
            }
            else if (_oldDataTypeDescriptor.SuperInterfaces.Contains(typeof(IPageMetaData)))
            {
                dataTypeDescriptor.AddSuperInterface(typeof(IPageData));
                dataTypeDescriptor.AddSuperInterface(typeof(IPageMetaData));
            }
            else
            {
                //DataFieldDescriptor idDataFieldDescriptor =
                //    (from dfd in _oldDataTypeDescriptor.Fields
                //     where dfd.Name == KeyFieldName
                //     select dfd).Single();

                //dataTypeDescriptor.Fields.Add(idDataFieldDescriptor);
                //dataTypeDescriptor.KeyPropertyNames.Add(KeyFieldName);
            }

            dataTypeDescriptor.Title = _newTypeTitle;

            foreach (var dataFieldDescriptor in _newDataFieldDescriptors)
            {
                dataTypeDescriptor.Fields.Add(dataFieldDescriptor);
            }

            if (KeyFieldName != null && !dataTypeDescriptor.KeyPropertyNames.Contains(KeyFieldName))
            {
                dataTypeDescriptor.KeyPropertyNames.Add(KeyFieldName);
            }

            dataTypeDescriptor.LabelFieldName    = _newLabelFieldName;
            dataTypeDescriptor.InternalUrlPrefix = _newInternalUrlPrefix;

            dataTypeDescriptor.DataAssociations.AddRange(_oldDataTypeDescriptor.DataAssociations);

            var position = 100;

            if (_foreignKeyDataFieldDescriptor != null)
            {
                _foreignKeyDataFieldDescriptor.Position = position++;

                if (_foreignKeyDataFieldDescriptor.Name != PageReferenceFieldName)
                {
                    dataTypeDescriptor.Fields.Add(_foreignKeyDataFieldDescriptor);
                }
            }

            return(dataTypeDescriptor);
        }
        private DataTypeDescriptor CreateNewDataTypeDescriptor(
            string typeNamespace,
            string typeName,
            string typeTitle,
            string labelFieldName,
            string internalUrlPrefix,
            bool cachable,
            bool searchable,
            bool publishControlled,
            bool localizedControlled,
            IEnumerable <DataFieldDescriptor> dataFieldDescriptors,
            DataFieldDescriptor foreignKeyDataFieldDescriptor,
            DataTypeAssociationDescriptor dataTypeAssociationDescriptor,
            DataFieldDescriptor compositionRuleForeignKeyDataFieldDescriptor)
        {
            var id = Guid.NewGuid();
            var dataTypeDescriptor = new DataTypeDescriptor(id, typeNamespace, typeName, true)
            {
                Cachable          = cachable,
                Searchable        = searchable,
                Title             = typeTitle,
                LabelFieldName    = labelFieldName,
                InternalUrlPrefix = internalUrlPrefix
            };

            dataTypeDescriptor.DataScopes.Add(DataScopeIdentifier.Public);

            if (publishControlled && _dataAssociationType != DataAssociationType.Composition)
            {
                dataTypeDescriptor.AddSuperInterface(typeof(IPublishControlled));
            }

            if (localizedControlled)
            {
                dataTypeDescriptor.AddSuperInterface(typeof(ILocalizedControlled));
            }

            //bool addKeyField = true;
            if (_dataAssociationType == DataAssociationType.Aggregation)
            {
                dataTypeDescriptor.AddSuperInterface(typeof(IPageDataFolder));
            }
            else if (_dataAssociationType == DataAssociationType.Composition)
            {
                //addKeyField = false;
                dataTypeDescriptor.AddSuperInterface(typeof(IPageData));
                dataTypeDescriptor.AddSuperInterface(typeof(IPageRelatedData));
                dataTypeDescriptor.AddSuperInterface(typeof(IPageMetaData));
            }

            //if (addKeyField)
            //{
            //    var idDataFieldDescriptor = BuildKeyFieldDescriptor();

            //    dataTypeDescriptor.Fields.Add(idDataFieldDescriptor);
            //    dataTypeDescriptor.KeyPropertyNames.Add(IdFieldName);
            //}

            foreach (var dataFieldDescriptor in dataFieldDescriptors)
            {
                dataTypeDescriptor.Fields.Add(dataFieldDescriptor);
            }

            if (_newKeyFieldName != null)
            {
                dataTypeDescriptor.KeyPropertyNames.Add(_newKeyFieldName);
            }

            var position = 100;

            if (_foreignKeyDataFieldDescriptor != null)
            {
                _foreignKeyDataFieldDescriptor.Position = position++;

                if (foreignKeyDataFieldDescriptor.Name != PageReferenceFieldName)
                {
                    dataTypeDescriptor.Fields.Add(foreignKeyDataFieldDescriptor);
                    dataTypeDescriptor.DataAssociations.Add(dataTypeAssociationDescriptor);
                }
            }

            return(dataTypeDescriptor);
        }