protected static ContentModelData MergeFields(ContentModelData primaryFields, ContentModelData secondaryFields, out string[] duplicateFieldNames) { List <string> duplicates = new List <string>(); ContentModelData result; if (secondaryFields == null) { result = primaryFields; } else if (primaryFields == null) { result = secondaryFields; } else { result = primaryFields; foreach (KeyValuePair <string, object> field in secondaryFields) { if (result.ContainsKey(field.Key)) { duplicates.Add(field.Key); } else { result.Add(field.Key, field.Value); } } } duplicateFieldNames = duplicates.ToArray(); return(result); }
protected virtual void MapSemanticProperties(ViewModel viewModel, MappingData mappingData) { Type modelType = viewModel.GetType(); IDictionary <string, List <SemanticProperty> > propertySemanticsMap = ModelTypeRegistry.GetPropertySemantics(modelType); IDictionary <string, string> xpmPropertyMetadata = new Dictionary <string, string>(); Validation validation = mappingData.PropertyValidation; foreach (KeyValuePair <string, List <SemanticProperty> > propertySemantics in propertySemanticsMap) { PropertyInfo modelProperty = modelType.GetProperty(propertySemantics.Key); List <SemanticProperty> semanticProperties = propertySemantics.Value; bool isFieldMapped = false; string fieldXPath = null; foreach (SemanticProperty semanticProperty in semanticProperties) { // To do : we need to make this more generic to collect all fields of a given type (e.g. [SemtanticProperty("_all", typeof(Keyword)]) if (semanticProperty.PropertyName == SemanticProperty.AllFields) { if (typeof(IDictionary <string, string>).IsAssignableFrom(modelProperty.PropertyType)) { modelProperty.SetValue(viewModel, GetAllFieldsAsDictionary <string>(mappingData)); } else if (typeof(IDictionary <string, KeywordModel>).IsAssignableFrom(modelProperty.PropertyType)) { modelProperty.SetValue(viewModel, GetAllFieldsAsDictionary <KeywordModel>(mappingData) as IDictionary <string, KeywordModel>); } isFieldMapped = true; break; } if ((semanticProperty.PropertyName == SemanticProperty.Self) && validation.MainSchema.HasSemanticType(semanticProperty.SemanticType)) { try { object mappedSelf = MapComponentLink((EntityModelData)mappingData.SourceViewModel, modelProperty.PropertyType, mappingData.Localization); modelProperty.SetValue(viewModel, mappedSelf); isFieldMapped = true; break; } catch (Exception ex) { Log.Debug($"Self mapping failed for {modelType.Name}.{modelProperty.Name}: {ex.Message}"); continue; } } FieldSemantics fieldSemantics = new FieldSemantics( semanticProperty.SemanticType.Vocab, semanticProperty.SemanticType.EntityName, semanticProperty.PropertyName, null); SemanticSchemaField semanticSchemaField = (mappingData.EmbeddedSemanticSchemaField == null) ? ValidateField(validation, fieldSemantics) : mappingData.EmbeddedSemanticSchemaField.FindFieldBySemantics(fieldSemantics); if (semanticSchemaField == null) { // No matching Semantic Schema Field found for this Semantic Property; maybe another one will match. continue; } // Matching Semantic Schema Field found fieldXPath = IsFieldFromMainSchema(validation, fieldSemantics) ? semanticSchemaField.GetXPath(mappingData.ContextXPath) : null; ContentModelData fields = semanticSchemaField.IsMetadata ? mappingData.MetadataFields : mappingData.Fields; object fieldValue = FindFieldValue(semanticSchemaField, fields, mappingData.EmbedLevel); if (fieldValue == null) { // No field value found; maybe we will find a value for another Semantic Property. continue; } try { object mappedPropertyValue = MapField(fieldValue, modelProperty.PropertyType, semanticSchemaField, mappingData); modelProperty.SetValue(viewModel, mappedPropertyValue); } catch (Exception ex) { throw new DxaException( $"Unable to map field '{semanticSchemaField.Name}' to property {modelType.Name}.{modelProperty.Name} of type '{modelProperty.PropertyType.FullName}'.", ex); } isFieldMapped = true; break; } if (fieldXPath != null) { xpmPropertyMetadata.Add(modelProperty.Name, fieldXPath); } else if (!isFieldMapped && Log.IsDebugEnabled) { string formattedSemanticProperties = string.Join(", ", semanticProperties.Select(sp => sp.ToString())); Log.Debug( $"Property {modelType.Name}.{modelProperty.Name} cannot be mapped to a CM field of {validation.MainSchema}. Semantic properties: {formattedSemanticProperties}."); } } EntityModel entityModel = viewModel as EntityModel; if ((entityModel != null) && mappingData.Localization.IsXpmEnabled) { entityModel.XpmPropertyMetadata = xpmPropertyMetadata; } }
private static object FindFieldValue(SemanticSchemaField semanticSchemaField, ContentModelData fields, int embedLevel) { string[] pathSegments = semanticSchemaField.Path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); object fieldValue = null; foreach (string pathSegment in pathSegments.Skip(embedLevel + 1)) { if ((fields == null) || !fields.TryGetValue(pathSegment, out fieldValue)) { return(null); } if (fieldValue is ContentModelData[]) { fields = ((ContentModelData[])fieldValue)[0]; } else { fields = fieldValue as ContentModelData; } } return(fieldValue); }