示例#1
0
        public string CreateRelationship(OneToManyRelationshipModel oneToMany) {
            if (oneToMany == null) {
                return "Invalid model.";
            }
            var primaryEntity = _contentMetadataService.GetEntity(oneToMany.PrimaryEntity);
            var relatedEntity = _contentMetadataService.GetDraftEntity(oneToMany.RelatedEntity);
            if (primaryEntity == null || relatedEntity == null
                || !primaryEntity.HasPublished()) {
                return "Invalid entity";
            }
            if (RelationshipExists(oneToMany.Name)) {
                return "Name already exist.";
            }

            var relationship = CreateRelation(new RelationshipRecord {
                Name = oneToMany.Name,
                PrimaryEntity = primaryEntity.Record,
                RelatedEntity = relatedEntity.Record,
                Type = (byte) RelationshipType.OneToMany
            });

            var updateModel = new ReferenceUpdateModel(new ReferenceFieldSettings {
                AlwaysInLayout = oneToMany.AlwaysInLayout,
                ContentTypeName = oneToMany.PrimaryEntity,
                DisplayAsLink = oneToMany.DisplayAsLink,
                HelpText = oneToMany.HelpText,
                IsAudit = oneToMany.IsAudit,
                Required = oneToMany.Required,
                RelationshipId = relationship.Id,
                RelationshipName = relationship.Name
            });
            var fieldViewModel = new AddFieldViewModel {
                AddInLayout = true,
                Name = oneToMany.FieldName,
                DisplayName = oneToMany.FieldLabel,
                FieldTypeName = "ReferenceField"
            };
            _contentMetadataService.CreateField(relatedEntity, fieldViewModel, updateModel);

            var fieldRecord = relatedEntity.FieldMetadataRecords.FirstOrDefault(field => field.Name == oneToMany.FieldName);
            var projectionPart = CreateProjection(oneToMany.RelatedEntity, oneToMany.ColumnFieldList);

            _oneToManyRepository.Create(new OneToManyRelationshipRecord {
                DeleteOption = (byte) oneToMany.DeleteOption,
                LookupField = fieldRecord,
                RelatedListProjection = projectionPart.Record,
                RelatedListLabel = oneToMany.RelatedListLabel,
                Relationship = relationship,
                ShowRelatedList = oneToMany.ShowRelatedList
            });

            return relationship.Id.ToString();
        }
示例#2
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;
 }