Exemple #1
0
        public static void MapDefaultValues(ContentData content)
        {
            if (content == null)
            {
                return;
            }

            var propertyInfoList = content.GetType().BaseType?.GetProperties() ?? new List <PropertyInfo>().ToArray();

            foreach (var property in propertyInfoList)
            {
                var attribute = property.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault();
                if (attribute != null)
                {
                    var defaultValueAttribute = (DefaultValueAttribute)attribute;
                    if (property.PropertyType == typeof(XhtmlString))
                    {
                        content[property.Name] = new XhtmlString(defaultValueAttribute.Value.ToString());
                    }
                    else
                    {
                        content[property.Name] = defaultValueAttribute.Value;
                    }
                }
            }
        }
        /// <summary>
        /// Converts the contentdata values into an object instance.
        /// </summary>
        /// <param name="theInstance">A context bus Object Instance to be passed to the event payload.</param>
        /// <param name="contentData">The newly published Ektron CMS content data object.</param>
        private void FillObjectInstance(IContextBusInstance theInstance, ContentData contentData)
        {
            Log.WriteMessage(string.Format("-Begin {0}.FillObjectInstance.", CLASS_NAME), LogLevel.Verbose);
            try
            {
                PropertyInfo[] contentProps = typeof(ContentData).GetProperties();
                Log.WriteMessage(string.Format("-Begin {0}.FillObjectInstance enumerating properties.", CLASS_NAME), LogLevel.Verbose);

                foreach (var prop in contentProps)
                {
                    try
                    {
                        var field = theInstance.Fields.Find(x => (x.Id.Split('|').Last()) == prop.Name);
                        if (field != null)
                        {
                            object o = contentData.GetType().GetProperty(prop.Name).GetGetMethod().Invoke(contentData, null);
                            if (o.GetType().IsEnum)
                            {
                                //enums are integers under the sheets and most DxH Object Instances are primitive types.
                                //get the value from the passed in content data and fill the field value.
                                field.Value = (int)contentData.GetType().GetProperty(prop.Name).GetGetMethod().Invoke(contentData, null);
                            }
                            else
                            {
                                //get the value from the passed in content data and fill the field value.
                                field.Value = contentData.GetType().GetProperty(prop.Name).GetGetMethod().Invoke(contentData, null);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteError(string.Format("{0}.FillObjectInstance failed on property: {1}, with error: {2} .",
                            CLASS_NAME, prop.Name, ex.Message));
                    }
                }
                if (contentData.MetaData != null)
                {

                    foreach (var metaDataType in contentData.MetaData)
                    {
                        try
                        {
                            var field = theInstance.Fields.Find(x => x.Id == "DxH|ContentHtml|MetaData|" + metaDataType.Id.ToString().Trim());
                            if (field != null)
                            {
                                field.Value = metaDataType.Text;
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(string.Format("{0}.FillObjectInstance failed on metadata property property: {1}, with error: {2} .",
                                CLASS_NAME, metaDataType.Name, ex.Message));
                        }
                    }

                }
                Log.WriteMessage(string.Format("+Finish {0}.FillObjectInstance enumerating properties.", CLASS_NAME), LogLevel.Verbose);
            }
            catch (Exception ex)
            {
                Log.WriteError(string.Format("{0}.FillObjectInstance failed: {1}.", CLASS_NAME, ex.Message));
            }
            Log.WriteMessage(string.Format("+Finish {0}.FillObjectInstance.", CLASS_NAME), LogLevel.Verbose);
        }
        /// <summary>
        /// Method used to pull serachable content from ContentAreas and lists of ContentReferences
        /// </summary>
        /// <param name="contentData"></param>
        public List <CrawlerContent> ProcessContentReferences(ContentData contentData)
        {
            var list = new List <CrawlerContent>();

            if (contentData == null)
            {
                return(list);
            }

            var pageProps = contentData.GetType().GetProperties();


            foreach (var pageProp in pageProps)
            {
                try {
                    if (pageProp.Name == "ContentLink")
                    {
                        continue;
                    }

                    if (pageProp.Name == "Item")
                    {
                        continue;
                    }

                    //_logger.Debug(string.Format("Processing {0}", pageProp.Name));

                    var propValue = pageProp.GetValue(contentData);

                    if (propValue == null)
                    {
                        continue;
                    }

                    switch (pageProp.PropertyType.Name)
                    {
                    case "ContentArea":

                        var refContentArea = propValue as ContentArea;

                        if (refContentArea == null)
                        {
                            continue;
                        }

                        foreach (var contentRef in refContentArea.Items)
                        {
                            var blockData = Repository.Get <IContent>(contentRef.ContentLink) as BlockData;

                            if (blockData != null)
                            {
                                var fields = ParseBlockData(blockData);

                                if (fields.Any())
                                {
                                    list.AddRange(fields);
                                }
                            }
                        }

                        break;


                    case "IList`1":

                        var refList = propValue as IList <ContentReference>;

                        if (refList == null)
                        {
                            continue;
                        }

                        foreach (var contentRef in refList)
                        {
                            var blockData = Repository.Get <IContent>(contentRef) as BlockData;

                            if (blockData != null)
                            {
                                var fields = ParseBlockData(blockData);

                                if (fields.Any())
                                {
                                    list.AddRange(fields);
                                }
                            }
                        }

                        break;

                    case "ContentReference":

                        var blockRef = propValue as ContentReference;

                        var blockData1 = Repository.Get <IContent>(blockRef) as BlockData;

                        if (blockData1 != null)
                        {
                            var fields = ParseBlockData(blockData1);

                            if (fields.Any())
                            {
                                list.AddRange(fields);
                            }
                        }

                        break;


                    default:

                        var blockData2 = propValue as BlockData;

                        if (blockData2 != null)
                        {
                            var blockFields = ParseBlockData(blockData2);

                            if (blockFields.Any())
                            {
                                list.AddRange(blockFields);
                            }
                        }


                        break;
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("{0} \"{1}\" {2}", ex.Message, pageProp.Name, ex.StackTrace));
                }
            }


            return(list);
        }