protected ContentModelData BuildContentModel(XmlElement xmlElement, int expandLinkDepth)
        {
            if (xmlElement == null)
            {
                return(null);
            }

            ContentModelData result = new ContentModelData();

            string        currentFieldName   = null;
            List <object> currentFieldValues = new List <object>();

            foreach (XmlElement childElement in xmlElement.SelectElements("*"))
            {
                if (childElement.Name != currentFieldName)
                {
                    // New field
                    if (currentFieldName != null)
                    {
                        result.Add(currentFieldName, GetTypedFieldValue(currentFieldValues));
                    }
                    currentFieldName   = childElement.Name;
                    currentFieldValues = new List <object>();
                }
                currentFieldValues.Add(GetFieldValue(childElement, expandLinkDepth));
            }

            if (currentFieldName != null)
            {
                result.Add(currentFieldName, GetTypedFieldValue(currentFieldValues));
            }

            return(result.Count == 0 ? null : result);
        }
Ejemplo n.º 2
0
        protected virtual EntityModelData CreateEntityModelData(Component component)
        {
            ContentModelData standardMeta = new ContentModelData();

            foreach (var meta in component.CustomMetas.Edges)
            {
                standardMeta.Add(meta.Node.Key, meta.Node.Value);
            }

            // The semantic mapping requires that some metadata fields exist. This may not be the case so we map some component meta properties onto them
            // if they don't exist.
            if (!standardMeta.ContainsKey("dateCreated"))
            {
                standardMeta.Add("dateCreated", component.LastPublishDate);
            }
            const string dateTimeFormat = "MM/dd/yyyy HH:mm:ss";

            standardMeta["dateCreated"] = DateTime.ParseExact((string)standardMeta["dateCreated"], dateTimeFormat, null);
            if (!standardMeta.ContainsKey("name"))
            {
                standardMeta.Add("name", component.Title);
            }
            return(new EntityModelData
            {
                Id = component.ItemId.ToString(),
                SchemaId = component.SchemaId.ToString(),
                Metadata = new ContentModelData {
                    { "standardMeta", standardMeta }
                }
            });
        }
        private EntityModelData CreateEntityModelData(IComponentMeta componentMeta)
        {
            ContentModelData standardMeta = new ContentModelData();

            foreach (DictionaryEntry entry in componentMeta.CustomMeta.NameValues)
            {
                standardMeta.Add(entry.Key.ToString(), ((NameValuePair)entry.Value).Value);
            }

            // The semantic mapping requires that some metadata fields exist. This may not be the case so we map some component meta properties onto them
            // if they don't exist.
            if (!standardMeta.ContainsKey("dateCreated"))
            {
                standardMeta.Add("dateCreated", componentMeta.LastPublicationDate);
            }
            if (!standardMeta.ContainsKey("name"))
            {
                standardMeta.Add("name", componentMeta.Title);
            }

            return(new EntityModelData
            {
                Id = componentMeta.Id.ToString(),
                SchemaId = componentMeta.SchemaId.ToString(),
                Metadata = new ContentModelData {
                    { "standardMeta", standardMeta }
                }
            });
        }
        /// <summary>
        /// Builds an Entity Data Model from a given CM Component Presentation on a Page.
        /// </summary>
        /// <param name="entityModelData">The Entity Data Model to build. Is <c>null</c> for the first Model Builder in the pipeline.</param>
        /// <param name="cp">The CM Component Presentation (obtained from a Page).</param>
        public void BuildEntityModel(ref EntityModelData entityModelData, ComponentPresentation cp)
        {
            // Add extension data for Context Expressions (if applicable)
            ContentModelData contextExpressions        = new ContentModelData();
            object           includeContextExpressions =
                GetContextExpressions(
                    ContextExpressionUtils.GetContextExpressions(
                        cp.Conditions.Where(c => !c.Negate).Select(c => c.TargetGroup)));
            object excludeContextExpressions =
                GetContextExpressions(
                    ContextExpressionUtils.GetContextExpressions(
                        cp.Conditions.Where(c => c.Negate).Select(c => c.TargetGroup)));

            if (includeContextExpressions != null)
            {
                Logger.Debug("Adding Context Expression Conditions (Include): " +
                             string.Join(", ", includeContextExpressions));
                contextExpressions.Add("Include", includeContextExpressions);
            }

            if (excludeContextExpressions != null)
            {
                Logger.Debug("Adding Context Expression Conditions (Exclude): " +
                             string.Join(", ", excludeContextExpressions));
                contextExpressions.Add("Exclude", excludeContextExpressions);
            }

            if (contextExpressions.Count > 0)
            {
                entityModelData.SetExtensionData("ContextExpressions", contextExpressions);
            }
        }
        protected virtual EntityModelData CreateEntityModelData(Component component)
        {
            ContentModelData standardMeta = new ContentModelData();
            var groups = component.CustomMetas.Edges.GroupBy(x => x.Node.Key).ToList();

            foreach (var group in groups)
            {
                var values = group.Select(x => x.Node.Value).ToArray();
                if (values.Length == 1)
                {
                    standardMeta.Add(group.Key, values[0]);
                }
                else
                {
                    standardMeta.Add(group.Key, values);
                }
            }

            // The semantic mapping requires that some metadata fields exist. This may not be the case so we map some component meta properties onto them
            // if they don't exist.
            const string dateCreated = "dateCreated";

            if (!standardMeta.ContainsKey(dateCreated))
            {
                standardMeta.Add(dateCreated, component.LastPublishDate);
            }
            else
            {
                if (standardMeta[dateCreated] is string[])
                {
                    standardMeta[dateCreated] = ((string[])standardMeta[dateCreated])[0];
                }
            }
            const string dateTimeFormat = "MM/dd/yyyy HH:mm:ss";

            standardMeta["dateCreated"] = DateTime.ParseExact((string)standardMeta[dateCreated], dateTimeFormat, null);
            if (!standardMeta.ContainsKey("name"))
            {
                standardMeta.Add("name", component.Title);
            }
            return(new EntityModelData
            {
                Id = component.ItemId.ToString(),
                SchemaId = component.SchemaId.ToString(),
                Metadata = new ContentModelData {
                    { "standardMeta", standardMeta }
                }
            });
        }
        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);
        }
        private static ContentModelData CreateTestContentModel(string testId, bool includeEntityModels, bool includeKeywordModels)
        {
            ContentModelData result = new ContentModelData
            {
                { "textField", "Test content for " + testId },
                { "multiValueTextField", new[] { "value1", "value2" } },
                { "richTextField", CreateTestRichText(testId + "_richTextField", includeEntityModels) },
                { "multiValueRichTextField", new[] { CreateTestRichText("multiValueRichTextField", includeEntityModels) } }
            };

            if (includeEntityModels)
            {
                result.Add("compLinkField", CreateTestEntityModel(testId + "_compLinkField"));
                result.Add("multiValueCompLinkField", new[] { CreateTestEntityModel(testId + "_multiValueCompLinkField") });
            }

            if (includeKeywordModels)
            {
                result.Add("keywordField", CreateTestKeywordModel(testId + "_keywordField"));
                result.Add("multiValueKeywordField", new [] { CreateTestKeywordModel(testId + "_multiValueKeywordField") });
            }

            return(result);
        }