public ContentTypeDefinitionBuilder(ContentTypeDefinition existing)
        {
            Current = existing;

            if (existing == null) {
                _parts = new List<ContentTypePartDefinition>();
                _settings = new SettingsDictionary();
            }
            else {
                _name = existing.Name;
                _displayName = existing.DisplayName;
                _parts = existing.Parts.ToList();
                _settings = new SettingsDictionary(existing.Settings.ToDictionary(kv => kv.Key, kv => kv.Value));
            }
        }
        /// <summary>
        /// Exports a content type definition to a XML format.
        /// </summary>
        /// <param name="contentTypeDefinition">The type definition to be exported.</param>
        /// <returns>The content type definition in an XML format.</returns>
        public XElement Export(ContentTypeDefinition contentTypeDefinition)
        {
            Argument.ThrowIfNull(contentTypeDefinition, nameof(contentTypeDefinition));

            var typeElement = NewElement(contentTypeDefinition.Name, contentTypeDefinition.Settings);
            if (typeElement.Attribute("DisplayName") == null && contentTypeDefinition.DisplayName != null) {
                typeElement.Add(new XAttribute("DisplayName", contentTypeDefinition.DisplayName));
            }

            foreach (var typePart in contentTypeDefinition.Parts) {
                typeElement.Add(NewElement(typePart.PartDefinition.Name, typePart.Settings));
            }

            return typeElement;
        }
        private void Apply(ContentTypeDefinition model, ContentTypeDefinitionRecord record)
        {
            record.DisplayName = model.DisplayName;
            record.Settings = _settingsFormatter.Map(model.Settings).ToString();

            var toRemove = record.ContentTypePartDefinitionRecords
                .Where(partDefinitionRecord => model.Parts.All(part => partDefinitionRecord.ContentPartDefinitionRecord.Name != part.PartDefinition.Name))
                .ToList();

            foreach (var remove in toRemove) {
                record.ContentTypePartDefinitionRecords.Remove(remove);
            }

            foreach (var part in model.Parts) {
                var partName = part.PartDefinition.Name;
                var typePartRecord = record.ContentTypePartDefinitionRecords.SingleOrDefault(r => r.ContentPartDefinitionRecord.Name == partName);
                if (typePartRecord == null) {
                    typePartRecord = new ContentTypePartDefinitionRecord { ContentPartDefinitionRecord = Acquire(part.PartDefinition) };
                    record.ContentTypePartDefinitionRecords.Add(typePartRecord);
                }
                Apply(part, typePartRecord);
            }
        }
        private ContentTypeDefinitionRecord Acquire(ContentTypeDefinition contentTypeDefinition)
        {
            var result = _contentStorageManager
                .Query<ContentTypeDefinitionRecord>(x => x.Name == contentTypeDefinition.Name)
                .SingleOrDefault();

            if (result == null) {
                result = new ContentTypeDefinitionRecord { Name = contentTypeDefinition.Name, DisplayName = contentTypeDefinition.DisplayName };
                _contentStorageManager.Store(result);
            }
            return result;
        }
 public void StoreTypeDefinition(ContentTypeDefinition contentTypeDefinition)
 {
     Apply(contentTypeDefinition, Acquire(contentTypeDefinition));
 }