//======================================================================================================= Runtime ContentType internal static ContentType Create(Type type, string ctd) { var contentType = new ContentType(ContentType.GetByName("GenericContent"));//HACK: come from ctd var reader = new StringReader(ctd); var xml = new XPathDocument(reader); var nametable = new NameTable(); var nav = xml.CreateNavigator(); var nsres = new XmlNamespaceManager(nav.NameTable); nsres.AddNamespace("x", ContentDefinitionXmlNamespace); var contentTypeElement = nav.Select("/x:ContentType", nsres); contentTypeElement.MoveNext(); var contentTypeName = contentTypeElement.Current.GetAttribute("name", ""); contentType.Name = contentTypeName; var fieldElements = nav.Select("/x:ContentType/x:Fields/x:Field", nsres); foreach (XPathNavigator fieldElement in fieldElements) { var fieldDescriptor = FieldDescriptor.Parse(fieldElement, nsres, contentType); var fieldSetting = FieldSetting.Create(fieldDescriptor); contentType.FieldSettings.Add(fieldSetting); } contentType.SetFieldSlots(type); return(contentType); }
private FieldSetting CreateNewFieldType(FieldDescriptor fieldInfo, Dictionary <string, List <string> > newBindings, ContentListType listType, SlotTable slotTable, SchemaEditor editor) { List <string> bindList = new List <string>(); foreach (RepositoryDataType slotType in FieldManager.GetDataTypes(fieldInfo.FieldTypeShortName)) { if (slotType == RepositoryDataType.NotDefined) { continue; } int slotNumber = slotTable.ReserveSlot((DataType)slotType); string binding = EncodeBinding(slotType, slotNumber); bindList.Add(binding); PropertyType pt = editor.PropertyTypes[binding]; if (pt == null) { pt = editor.CreateContentListPropertyType((DataType)slotType, slotNumber); } editor.AddPropertyTypeToPropertySet(pt, listType); } newBindings.Add(fieldInfo.FieldName, bindList); return(FieldSetting.Create(fieldInfo, bindList, null)); }
private ContentListType ManageContentListTypeOneAttempt(Dictionary <string, FieldDescriptor> fieldInfoList, Dictionary <string, List <string> > oldBindings, bool modify, out List <FieldSetting> fieldSettings) { fieldSettings = new List <FieldSetting>(); if (!modify) { // Load foreach (string name in fieldInfoList.Keys) { fieldSettings.Add(FieldSetting.Create(fieldInfoList[name], oldBindings[name], null)); } return(this.ContentListType); } SchemaEditor editor = new SchemaEditor(); editor.Load(); bool hasChanges = false; var listType = this.ContentListType; Dictionary <string, List <string> > newBindings = new Dictionary <string, List <string> >(); SlotTable slotTable = new SlotTable(oldBindings); if (listType == null) { // new listType = editor.CreateContentListType(Guid.NewGuid().ToString()); foreach (string name in fieldInfoList.Keys) { fieldSettings.Add(CreateNewFieldType(fieldInfoList[name], newBindings, listType, slotTable, editor)); } hasChanges = true; } else { // merge listType = editor.ContentListTypes[listType.Name]; hasChanges |= RemoveUnusedFields(fieldInfoList, oldBindings, listType, editor); foreach (string name in fieldInfoList.Keys) { FieldSetting origField = GetFieldTypeByName(name, _fieldSettings); if (origField == null) { fieldSettings.Add(CreateNewFieldType(fieldInfoList[name], newBindings, listType, slotTable, editor)); hasChanges = true; } else { List <string> bindList = new List <string>(origField.Bindings.ToArray()); fieldSettings.Add(FieldSetting.Create(fieldInfoList[name], bindList, null)); newBindings.Add(name, bindList); } } } if (hasChanges) { editor.Register(); } this.ContentListBindings = newBindings; return(ActiveSchema.ContentListTypes[listType.Name]); }
/// <summary> /// Adds one or more new fields to this instance or replaces existing ones and saves the modifications. /// </summary> /// <param name="fieldInfos">Array of <see cref="Schema.FieldInfo"/>s that will be added.</param> public void AddFields(params SenseNet.ContentRepository.Schema.FieldInfo[] fieldInfos) { foreach (var fieldInfo in fieldInfos) { AddFieldInternal(FieldSetting.Create(fieldInfo, this)); } Build(); Save(); }
private List <FieldSetting> ManageAspectType(Dictionary <string, FieldDescriptor> fieldInfoList) { var fieldSettings = new List <FieldSetting>(); foreach (string name in fieldInfoList.Keys) { fieldSettings.Add(FieldSetting.Create(fieldInfoList[name], this)); } return(fieldSettings); }
private void ParseFieldElements(XPathNavigator fieldsElement, IXmlNamespaceResolver nsres) { foreach (XPathNavigator fieldElement in fieldsElement.SelectChildren(XPathNodeType.Element)) { FieldDescriptor fieldDescriptor = FieldDescriptor.Parse(fieldElement, nsres, this); CheckFieldValidation(fieldDescriptor, this.Name); int fieldIndex = GetFieldSettingIndexByName(fieldDescriptor.FieldName); FieldSetting fieldSetting = fieldIndex < 0 ? null : this.FieldSettings[fieldIndex]; if (fieldSetting == null) { //==== if there is not...: create and inherit fieldSetting = FieldSetting.Create(fieldDescriptor); this.FieldSettings.Add(fieldSetting); //-- inherit new fieldType foreach (ContentType child in this.ChildTypes) { child.InheritField(fieldSetting); } } else { //==== if there is ... if (fieldSetting.Owner == this) { string fieldSettingTypeName = fieldDescriptor.FieldSettingTypeName; if (String.IsNullOrEmpty(fieldSettingTypeName)) { fieldSettingTypeName = FieldManager.GetDefaultFieldSettingTypeName(fieldDescriptor.FieldTypeName); } if (fieldSettingTypeName == fieldSetting.GetType().FullName) { fieldSetting.Modify(fieldDescriptor); } else { ChangeFieldSetting(fieldSetting, fieldDescriptor); } } else { //==== if inherited: break inheritance //-- create new FieldSetting newFieldSetting = FieldSetting.Create(fieldDescriptor); newFieldSetting.ParentFieldSetting = fieldSetting; this.FieldSettings[fieldIndex] = newFieldSetting; //-- inherit foreach (ContentType child in this.ChildTypes) { child.InheritField(newFieldSetting); } } } } }
private void ChangeFieldSetting(FieldSetting oldSetting, FieldDescriptor fieldDescriptor) { FieldSetting newSetting = FieldSetting.Create(fieldDescriptor); int index = this.GetFieldSettingIndexByName(newSetting.Name); this.FieldSettings[index] = newSetting; newSetting.ParentFieldSetting = oldSetting.ParentFieldSetting; ChangeParentFieldSettingRecursive(oldSetting, newSetting); }
private static ContentType Create(ISupportsDynamicFields handler, ContentType baseContentType, string ctd) { var contentType = new ContentType(baseContentType); contentType.DisplayName = baseContentType.DisplayName; contentType.Description = baseContentType.Description; //contentType.ParentType = baseContentType.ParentType; contentType.ParentType = baseContentType; contentType.FieldSettings.AddRange(baseContentType.FieldSettings); var reader = new StringReader(ctd); var xml = new XPathDocument(reader); var nametable = new NameTable(); var nav = xml.CreateNavigator(); var nsres = new XmlNamespaceManager(nav.NameTable); nsres.AddNamespace("x", ContentDefinitionXmlNamespace); var contentTypeElement = nav.Select("/x:ContentType", nsres); contentTypeElement.MoveNext(); var contentTypeName = contentTypeElement.Current.GetAttribute("name", ""); contentType.Name = contentTypeName; var fieldElements = nav.Select("/x:ContentType/x:Fields/x:Field", nsres); foreach (XPathNavigator fieldElement in fieldElements) { var fieldDescriptor = FieldDescriptor.Parse(fieldElement, nsres, contentType); var fieldSetting = FieldSetting.Create(fieldDescriptor); contentType.SetFieldSlots(fieldSetting, handler); contentType.FieldSettings.Add(fieldSetting); } return(contentType); }
private FieldSetting CreateNewFieldType(FieldDescriptor fieldInfo) { return(FieldSetting.Create(fieldInfo, new List <string>(), this)); }