Esempio n. 1
0
        public void CreateField(EntityMetadataPart entity, AddFieldViewModel viewModel, IUpdateModel updateModel)
        {
            var settingsDictionary = new SettingsDictionary();

            settingsDictionary["DisplayName"] = viewModel.DisplayName;
            settingsDictionary["AddInLayout"] = viewModel.AddInLayout.ToString();
            settingsDictionary["EntityName"]  = entity.Name;
            var field = new FieldMetadataRecord {
                ContentFieldDefinitionRecord = FetchFieldDefinition(viewModel.FieldTypeName),
                Name = viewModel.Name
            };

            entity.FieldMetadataRecords.Add(field);
            _contentDefinitionEditorEvents.UpdateFieldSettings(viewModel.FieldTypeName, viewModel.Name, settingsDictionary, updateModel);
            field.Settings             = CompileSetting(settingsDictionary);
            field.EntityMetadataRecord = entity.Record;
        }
        private void UpdateEntity(EntityMetadataPart previousEntity, EntityMetadataPart entity)
        {
            _contentDefinitionManager.AlterTypeDefinition(entity.Name, builder =>
                                                          builder.DisplayedAs(entity.DisplayName));

            foreach (var fieldMetadataRecord in previousEntity.FieldMetadataRecords)
            {
                var exist = entity.FieldMetadataRecords.Any(x => x.OriginalId == fieldMetadataRecord.Id);
                if (!exist)
                {
                    var record = fieldMetadataRecord;
                    _contentDefinitionManager.AlterPartDefinition(entity.Name,
                                                                  typeBuilder => typeBuilder.RemoveField(record.Name));
                    _schemaUpdateService.DropColumn(entity.Name, fieldMetadataRecord.Name);
                    _fieldEvents.OnDeleting(entity.Name, fieldMetadataRecord.Name);
                }
            }

            var needUpdateFields = new List <FieldMetadataRecord>();

            foreach (var fieldMetadataRecord in entity.FieldMetadataRecords)
            {
                if (fieldMetadataRecord.OriginalId != 0)
                {
                    needUpdateFields.Add(fieldMetadataRecord);
                }
                else
                {
                    AddField(entity.Name, fieldMetadataRecord);
                    _schemaUpdateService.CreateColumn(entity.Name, fieldMetadataRecord.Name, fieldMetadataRecord.ContentFieldDefinitionRecord.Name);
                }
            }

            foreach (var fieldMetadataRecord in needUpdateFields)
            {
                var record   = fieldMetadataRecord;
                var settings = _settingsFormatter.Map(Parse(record.Settings));
                _contentDefinitionManager.AlterPartDefinition(entity.Name, builder =>
                                                              builder.WithField(record.Name, fieldBuilder => {
                    fieldBuilder.WithDisplayName(settings["DisplayName"]);
                    _contentDefinitionEditorEvents.UpdateFieldSettings(fieldBuilder, settings);
                }));
                record.Settings = _settingsFormatter.Map(settings).ToString();
            }
            _entityEvents.OnUpdating(entity.Name);
        }