/// <summary> /// Adds an interface the data type should inherit from /// </summary> /// <param name="interfaceType"></param> /// <param name="addInheritedFields"></param> internal void AddSuperInterface(Type interfaceType, bool addInheritedFields) { if (_superInterfaces.Contains(interfaceType) || interfaceType == typeof(IData)) { return; } _superInterfaces.Add(interfaceType); if (addInheritedFields) { foreach (PropertyInfo propertyInfo in interfaceType.GetProperties()) { if (propertyInfo.Name == nameof(IPageData.PageId) && interfaceType == typeof(IPageData)) { continue; } DataFieldDescriptor dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true); this.Fields.Add(dataFieldDescriptor); } } foreach (string propertyName in interfaceType.GetKeyPropertyNames()) { if (KeyPropertyNames.Contains(propertyName)) { continue; } PropertyInfo property = ReflectionBasedDescriptorBuilder.FindProperty(interfaceType, propertyName); if (DynamicTypeReflectionFacade.IsKeyField(property)) { this.KeyPropertyNames.Add(propertyName, false); } } foreach (var dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType)) { if (!this.DataScopes.Contains(dataScopeIdentifier)) { this.DataScopes.Add(dataScopeIdentifier); } } var superInterfaces = interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t)); foreach (Type superSuperInterfaceType in superInterfaces) { AddSuperInterface(superSuperInterfaceType, addInheritedFields); } }
/// <exclude /> public static string GetBrokenReferencesReport(List <IData> brokenReferences) { var sb = new StringBuilder(); const int maximumLinesToShow = 6; int toDisplay = brokenReferences.Count > maximumLinesToShow ? maximumLinesToShow - 1 : brokenReferences.Count; for (int i = 0; i < toDisplay; i++) { IData brokenReference = brokenReferences[i]; Type type = brokenReference.DataSourceId.InterfaceType; string typeTitle = DynamicTypeReflectionFacade.GetTitle(type); if (string.IsNullOrEmpty(typeTitle)) { typeTitle = type.FullName; } string labelPropertyName = DynamicTypeReflectionFacade.GetLabelPropertyName(type); if (string.IsNullOrEmpty(labelPropertyName)) { labelPropertyName = "Id"; // This is a nasty fallback, but will work in most cases and all the time with generated types. } PropertyInfo labelPropertyInfo = brokenReference.GetType().GetProperty(labelPropertyName, BindingFlags.Instance | BindingFlags.Public); string dataLabel; if (labelPropertyInfo != null) { object propertyFieldValue = labelPropertyInfo.GetValue(brokenReference, null); dataLabel = (propertyFieldValue ?? "NULL").ToString(); } else { dataLabel = "'Failed to resolve ({0}) field'".FormatWith(labelPropertyName); } sb.Append("{0}, {1}".FormatWith(typeTitle, dataLabel)); sb.Append("\n\r"); } if (brokenReferences.Count > maximumLinesToShow) { sb.Append("..."); } return(sb.ToString()); }
internal static void Initialize_PostDataTypes() { foreach (Type type in DataProviderRegistry.AllInterfaces) { string internalUrlPrefix = DynamicTypeReflectionFacade.GetInternalUrlPrefix(type); if (string.IsNullOrEmpty(internalUrlPrefix)) { continue; } Register(new DataInternalUrlConverter(internalUrlPrefix, type)); Register(type, new DataInternalUrlProvider(internalUrlPrefix, type)); } }
/// <summary> /// Removes a super interface /// </summary> /// <param name="interfaceType">Type to remove</param> public void RemoveSuperInterface(Type interfaceType) { if (interfaceType == typeof(IData)) { return; } if (_superInterfaces.Contains(interfaceType)) { _superInterfaces.Remove(interfaceType); } foreach (PropertyInfo propertyInfo in interfaceType.GetProperties()) { var dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true); if (this.Fields.Contains(dataFieldDescriptor)) { this.Fields.Remove(dataFieldDescriptor); } if (DynamicTypeReflectionFacade.IsKeyField(propertyInfo) && this.KeyPropertyNames.Contains(propertyInfo.Name)) { this.KeyPropertyNames.Remove(propertyInfo.Name); } } foreach (var dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType)) { if (this.DataScopes.Contains(dataScopeIdentifier)) { this.DataScopes.Remove(dataScopeIdentifier); } } var superInterfaces = interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t)); foreach (Type superInterfaceType in superInterfaces) { RemoveSuperInterface(superInterfaceType); } }
/// <summary> /// Copies all properties that exists on the targetData from the sourceData except the DataSourceId /// </summary> /// <param name="sourceData"></param> /// <param name="targetData"></param> /// <param name="useDefaultValues"></param> public static void ProjectedCopyTo(this IData sourceData, IData targetData, bool useDefaultValues) { foreach (PropertyInfo targetPropertyInfo in targetData.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (targetPropertyInfo.Name == "DataSourceId") { continue; } if (targetPropertyInfo.CanWrite) { PropertyInfo sourcePropertyInfo = sourceData.GetType().GetProperty(targetPropertyInfo.Name, BindingFlags.Public | BindingFlags.Instance); if (sourcePropertyInfo != null) { object value = sourcePropertyInfo.GetValue(sourceData, null); targetPropertyInfo.SetValue(targetData, value, null); } else if (useDefaultValues) { object oldValue = targetPropertyInfo.GetValue(targetData, null); if (oldValue == null) { DefaultValue defaultValue = DynamicTypeReflectionFacade.GetDefaultValue(targetPropertyInfo); if (defaultValue != null) { targetPropertyInfo.SetValue(targetData, defaultValue.Value, null); } else { // Do something here ?? /MRJ } } } } } }
internal static DataTypeDescriptor FromXml(XElement element, bool inheritedFieldsIncluded) { Verify.ArgumentNotNull(element, "element"); if (element.Name != "DataTypeDescriptor") { throw new ArgumentException("The xml is not correctly formatted."); } Guid dataTypeId = (Guid)element.GetRequiredAttribute("dataTypeId"); string name = element.GetRequiredAttributeValue("name"); string @namespace = element.GetRequiredAttributeValue("namespace"); // TODO: check why "hasCustomPhysicalSortOrder" is not used bool hasCustomPhysicalSortOrder = (bool)element.GetRequiredAttribute("hasCustomPhysicalSortOrder"); bool isCodeGenerated = (bool)element.GetRequiredAttribute("isCodeGenerated"); XAttribute cachableAttribute = element.Attribute("cachable"); XAttribute buildNewHandlerTypeNameAttribute = element.Attribute("buildNewHandlerTypeName"); XElement dataAssociationsElement = element.GetRequiredElement("DataAssociations"); XElement dataScopesElement = element.GetRequiredElement("DataScopes"); XElement keyPropertyNamesElement = element.GetRequiredElement("KeyPropertyNames"); // TODO: check why "superInterfaceKeyPropertyNamesElement" is not used // XElement superInterfaceKeyPropertyNamesElement = element.Element("SuperInterfaceKeyPropertyNames"); XElement superInterfacesElement = element.GetRequiredElement("SuperInterfaces"); XElement fieldsElement = element.GetRequiredElement("Fields"); XElement indexesElement = element.Element("Indexes"); XAttribute titleAttribute = element.Attribute("title"); XAttribute labelFieldNameAttribute = element.Attribute("labelFieldName"); XAttribute internalUrlPrefixAttribute = element.Attribute("internalUrlPrefix"); string typeManagerTypeName = (string)element.Attribute("typeManagerTypeName"); bool cachable = cachableAttribute != null && (bool)cachableAttribute; var dataTypeDescriptor = new DataTypeDescriptor(dataTypeId, @namespace, name, isCodeGenerated) { Cachable = cachable }; if (titleAttribute != null) { dataTypeDescriptor.Title = titleAttribute.Value; } if (labelFieldNameAttribute != null) { dataTypeDescriptor.LabelFieldName = labelFieldNameAttribute.Value; } if (internalUrlPrefixAttribute != null) { dataTypeDescriptor.InternalUrlPrefix = internalUrlPrefixAttribute.Value; } if (typeManagerTypeName != null) { typeManagerTypeName = TypeManager.FixLegasyTypeName(typeManagerTypeName); dataTypeDescriptor.TypeManagerTypeName = typeManagerTypeName; } if (buildNewHandlerTypeNameAttribute != null) { dataTypeDescriptor.BuildNewHandlerTypeName = buildNewHandlerTypeNameAttribute.Value; } foreach (XElement elm in dataAssociationsElement.Elements()) { var dataTypeAssociationDescriptor = DataTypeAssociationDescriptor.FromXml(elm); dataTypeDescriptor.DataAssociations.Add(dataTypeAssociationDescriptor); } foreach (XElement elm in dataScopesElement.Elements("DataScopeIdentifier")) { string dataScopeName = elm.GetRequiredAttributeValue("name"); if (DataScopeIdentifier.IsLegasyDataScope(dataScopeName)) { Log.LogWarning("DataTypeDescriptor", "Ignored legacy data scope '{0}' on type '{1}.{2}' while deserializing DataTypeDescriptor. The '{0}' data scope is no longer supported.".FormatWith(dataScopeName, @namespace, name)); continue; } DataScopeIdentifier dataScopeIdentifier = DataScopeIdentifier.Deserialize(dataScopeName); dataTypeDescriptor.DataScopes.Add(dataScopeIdentifier); } foreach (XElement elm in superInterfacesElement.Elements("SuperInterface")) { string superInterfaceTypeName = elm.GetRequiredAttributeValue("type"); if (superInterfaceTypeName.StartsWith("Composite.Data.ProcessControlled.IDeleteControlled")) { Log.LogWarning("DataTypeDescriptor", string.Format("Ignored legacy super interface '{0}' on type '{1}.{2}' while deserializing DataTypeDescriptor. This super interface is no longer supported.", superInterfaceTypeName, @namespace, name)); continue; } Type superInterface; try { superInterface = TypeManager.GetType(superInterfaceTypeName); } catch (Exception ex) { throw XmlConfigurationExtensionMethods.GetConfigurationException("Failed to load super interface '{0}'".FormatWith(superInterfaceTypeName), ex, elm); } dataTypeDescriptor.AddSuperInterface(superInterface, !inheritedFieldsIncluded); } foreach (XElement elm in fieldsElement.Elements()) { var dataFieldDescriptor = DataFieldDescriptor.FromXml(elm); try { dataTypeDescriptor.Fields.Add(dataFieldDescriptor); } catch (Exception ex) { throw XmlConfigurationExtensionMethods.GetConfigurationException("Failed to add a data field: " + ex.Message, ex, elm); } } foreach (XElement elm in keyPropertyNamesElement.Elements("KeyPropertyName")) { var propertyName = elm.GetRequiredAttributeValue("name"); bool isDefinedOnSuperInterface = dataTypeDescriptor.SuperInterfaces.Any(f => f.GetProperty(propertyName) != null); if (!isDefinedOnSuperInterface) { dataTypeDescriptor.KeyPropertyNames.Add(propertyName); } } if (indexesElement != null) { dataTypeDescriptor.Indexes = indexesElement.Elements("Index").Select(DataTypeIndex.FromXml).ToList(); } // Loading field rendering profiles for static data types if (!isCodeGenerated && typeManagerTypeName != null) { Type type = Type.GetType(typeManagerTypeName); if (type != null) { foreach (var fieldDescriptor in dataTypeDescriptor.Fields) { var property = type.GetProperty(fieldDescriptor.Name); if (property != null) { var formRenderingProfile = DynamicTypeReflectionFacade.GetFormRenderingProfile(property); if (formRenderingProfile != null) { fieldDescriptor.FormRenderingProfile = formRenderingProfile; } } } } } return(dataTypeDescriptor); }
/// <summary> /// Adds an interface the data type should inherit from /// </summary> /// <param name="interfaceType"></param> /// <param name="addInheritedFields"></param> internal void AddSuperInterface(Type interfaceType, bool addInheritedFields) { if (_superInterfaces.Contains(interfaceType) || interfaceType == typeof(IData)) { return; } _superInterfaces.Add(interfaceType); if (addInheritedFields) { foreach (PropertyInfo propertyInfo in interfaceType.GetProperties()) { if (propertyInfo.Name == "PageId" && interfaceType == typeof(IPageData)) { continue; } DataFieldDescriptor dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true); this.Fields.Add(dataFieldDescriptor); } } foreach (string propertyName in interfaceType.GetKeyPropertyNames()) { if (KeyPropertyNames.Contains(propertyName)) { continue; } PropertyInfo property = interfaceType.GetProperty(propertyName); if (property == null) { List <Type> superInterfaces = interfaceType.GetInterfacesRecursively(t => typeof(IData).IsAssignableFrom(t) && t != typeof(IData)); foreach (Type superInterface in superInterfaces) { property = superInterface.GetProperty(propertyName); if (property != null) { break; } } } Verify.IsNotNull(property, "Missing property '{0}' on type '{1}' or one of its interfaces".FormatWith(propertyName, interfaceType)); if (DynamicTypeReflectionFacade.IsKeyField(property)) { this.KeyPropertyNames.Add(propertyName, false); } } foreach (DataScopeIdentifier dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType)) { if (!this.DataScopes.Contains(dataScopeIdentifier)) { this.DataScopes.Add(dataScopeIdentifier); } } foreach (Type superSuperInterfaceType in interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t))) { AddSuperInterface(superSuperInterfaceType, addInheritedFields); } }
private IData CloneData <T>(T data) where T : class, IData { IData newdata = DataFacade.BuildNew <T>(); var dataProperties = typeof(T).GetPropertiesRecursively(); foreach (var propertyInfo in dataProperties.Where(f => f.CanWrite)) { if (typeof(T).GetPhysicalKeyProperties().Contains(propertyInfo) && propertyInfo.PropertyType == typeof(Guid)) { propertyInfo.SetValue(newdata, Guid.NewGuid()); } else { propertyInfo.SetValue(newdata, propertyInfo.GetValue(data)); } } var page = data as IPage; if (page != null) { if (IsRootPage(page)) { SetNewValue <T>(page, newdata, dataProperties.First(p => p.Name == nameof(IPage.Title))); } else if (!SetNewValue <T>(page, newdata, dataProperties.First(p => p.Name == nameof(IPage.MenuTitle)))) { SetNewValue <T>(page, newdata, dataProperties.First(p => p.Name == nameof(IPage.Title))); } SetNewValue <T>(page, newdata, dataProperties.First(p => p.Name == nameof(IPage.UrlTitle)), isUrl: true); PageInsertPosition.After(page.Id).CreatePageStructure(newdata as IPage, page.GetParentId()); } else { var labelProperty = typeof(T).GetProperty( DynamicTypeReflectionFacade.GetLabelPropertyName(typeof(T))); if (labelProperty != null && labelProperty.PropertyType == typeof(string)) { SetNewValue <T>(data, newdata, labelProperty); } } if (newdata is IPublishControlled) { (newdata as IPublishControlled).PublicationStatus = GenericPublishProcessController.Draft; } if (newdata is ILocalizedControlled) { (newdata as ILocalizedControlled).SourceCultureName = UserSettings.ActiveLocaleCultureInfo.Name; } newdata = DataFacade.AddNew(newdata); if (data is IPage) { CopyPageData(data as IPage, newdata as IPage); } return(newdata); }