Пример #1
0
        public IEnumerable <ILinkedEntity> GetLinkedEntities(object propertyValue)
        {
            if (propertyValue == null)
            {
                return(Enumerable.Empty <ILinkedEntity>());
            }

            var entities     = new List <ILinkedEntity>();
            var contentTypes = new Dictionary <string, IContentType>();
            var dataTypes    = new Dictionary <int, IDataTypeDefinition>();

            try
            {
                var items = JsonConvert.DeserializeObject <EmbeddedContentItem[]>(propertyValue.ToString());

                foreach (EmbeddedContentItem item in items)
                {
                    if (false == contentTypes.TryGetValue(item.ContentTypeAlias, out IContentType contentType))
                    {
                        contentTypes[item.ContentTypeAlias] = contentType = _contentTypeService.GetContentType(item.ContentTypeAlias);
                    }
                    if (contentType == null)
                    {
                        continue;
                    }
                    foreach (PropertyType propertyType in contentType.PropertyTypes)
                    {
                        if (false == item.Properties.TryGetValue(propertyType.Alias, out object value))
                        {
                            continue;
                        }

                        if (false == dataTypes.TryGetValue(propertyType.DataTypeDefinitionId, out IDataTypeDefinition dataTypeDefinition))
                        {
                            dataTypes[propertyType.DataTypeDefinitionId] = dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId);
                        }
                        if (dataTypeDefinition == null)
                        {
                            continue;
                        }
                        IPropertyParser parser = PropertyParserResolver.Current.Parsers.FirstOrDefault(x => x.IsParserFor(dataTypeDefinition));

                        if (parser != null)
                        {
                            entities.AddRange(parser.GetLinkedEntities(value));
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.Error <EmbeddedContentParser>("Error parsing embedded content", exception);
            }
            return(entities);
        }