public ContentPartDefinitionBuilder(ContentPartDefinition existing)
 {
     if (existing == null) {
         _fields = new List<ContentPartFieldDefinition>();
         _settings = new SettingsDictionary();
     }
     else {
         _name = existing.Name;
         _fields = existing.Fields.ToList();
         _settings = new SettingsDictionary(existing.Settings.ToDictionary(kv => kv.Key, kv => kv.Value));
     }
 }
        /// <summary>
        /// Exports a content part definition to a XML format.
        /// </summary>
        /// <param name="contentPartDefinition">The part definition to be exported.</param>
        /// <returns>The content part definition in a XML format.</returns>
        public XElement Export(ContentPartDefinition contentPartDefinition)
        {
            Argument.ThrowIfNull(contentPartDefinition, "contentPartDefinition");

            var partElement = NewElement(contentPartDefinition.Name, contentPartDefinition.Settings);
            foreach (var partField in contentPartDefinition.Fields) {
                var attributeName = string.Format("{0}.{1}", partField.Name, partField.FieldDefinition.Name);
                var partFieldElement = NewElement(attributeName, partField.Settings);
                partElement.Add(partFieldElement);
            }

            return partElement;
        }
 public ContentTypeDefinitionBuilder WithPart(ContentPartDefinition partDefinition, Action<ContentTypePartDefinitionBuilder> configuration)
 {
     var existingPart = _parts.SingleOrDefault(x => x.PartDefinition.Name == partDefinition.Name);
     if (existingPart != null) {
         _parts.Remove(existingPart);
     }
     else {
         existingPart = new ContentTypePartDefinition(partDefinition, new SettingsDictionary());
     }
     var configurer = new PartConfigurerImpl(existingPart);
     configuration(configurer);
     _parts.Add(configurer.Build());
     return this;
 }
 public PartConfigurerImpl(ContentTypePartDefinition part)
     : base(part)
 {
     _partDefinition = part.PartDefinition;
 }
        private void Apply(ContentPartDefinition model, ContentPartDefinitionRecord record)
        {
            record.Settings = _settingsFormatter.Map(model.Settings).ToString();

            var toRemove = record.ContentPartFieldDefinitionRecords
                .Where(partFieldDefinitionRecord => model.Fields.All(partField => partFieldDefinitionRecord.Name != partField.Name))
                .ToList();

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

            foreach (var field in model.Fields) {
                var fieldName = field.Name;
                var partFieldRecord = record.ContentPartFieldDefinitionRecords.SingleOrDefault(r => r.Name == fieldName);
                if (partFieldRecord == null) {
                    partFieldRecord = new ContentPartFieldDefinitionRecord {
                        ContentFieldDefinitionRecord = Acquire(field.FieldDefinition),
                        Name = field.Name
                    };
                    record.ContentPartFieldDefinitionRecords.Add(partFieldRecord);
                }
                Apply(field, partFieldRecord);
            }
        }
 private ContentPartDefinitionRecord Acquire(ContentPartDefinition contentPartDefinition)
 {
     var result = _partDefinitionRepository.Table.SingleOrDefault(x => x.Name == contentPartDefinition.Name);
     if (result == null) {
         result = new ContentPartDefinitionRecord { Name = contentPartDefinition.Name };
         _partDefinitionRepository.Create(result);
     }
     return result;
 }
 public void StorePartDefinition(ContentPartDefinition contentPartDefinition)
 {
     Apply(contentPartDefinition, Acquire(contentPartDefinition));
     TriggerContentDefinitionSignal();
 }