예제 #1
0
        public Dictionary<string, object> ConvertArticle(Article article, IArticleFilter filter)
        {
            if (article == null || !article.Visible || article.Archived || !filter.Matches(article))
            {
                return null;
            }

            var dict = new Dictionary<string, object> { { IdProp, article.Id } };

            foreach (ArticleField field in article.Fields.Values)
            {
                if (field is ExtensionArticleField extensionArticleField)
                {
                    MergeExtensionFields(dict, extensionArticleField, filter);
                }
                else
                {
                    object value = ConvertField(field, filter);

                    if (value != null && !(value is string && string.IsNullOrEmpty((string)value)))
                    {
                        dict[field.FieldName] = value;
                    }
                }
            }

            return dict;
        }
        public ActionResult LoadPartialProduct(
            [FromBody] LoadPartialProductRequest request, bool isLive = false)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            Content rootContent = _contentDefinitionService
                                  .GetDefinitionById(request.ProductDefinitionId, isLive);

            Content partialContent = _editorPartialContentService
                                     .FindContentByPath(rootContent, request.ContentPath);

            Article[] articles = _productService
                                 .GetProductsByIds(partialContent, request.ArticleIds, isLive);

            IArticleFilter filter = isLive ? ArticleFilter.LiveFilter : ArticleFilter.DefaultFilter;

            var visited = new HashSet <Article>();

            ArticleObject[] articleObjects = articles
                                             .Select(article => _editorDataService.ConvertArticle(article, filter, visited))
                                             .ToArray();

            string productsJson = JsonConvert.SerializeObject(articleObjects);

            return(Content(productsJson, "application/json"));
        }
예제 #3
0
        /// <exception cref="NotSupportedException" />
        private object ConvertField(ArticleField field, IArticleFilter filter)
        {
            if (field is PlainArticleField plainArticleField)
            {
                return GetPlainArticleFieldValue(plainArticleField);
            }
            if (field is SingleArticleField singleArticleField)
            {
                return ConvertArticle(singleArticleField.GetItem(filter), filter);
            }
            if (field is MultiArticleField multiArticleField)
            {
                var articles = multiArticleField
                    .GetArticlesSorted(filter)
                    .Select(x => ConvertArticle(x, filter))
                    .ToArray();

                return articles.Length == 0 ? null : articles;
            }
            if (field is VirtualArticleField virtualArticleField)
            {
                return virtualArticleField.Fields
                    .Select(x => new { fieldName = x.FieldName, value = ConvertField(x, filter) })
                    .ToDictionary(x => x.fieldName, x => x.value);
            }
            if (field is VirtualMultiArticleField virtualMultiArticleField)
            {
                return virtualMultiArticleField.VirtualArticles.Select(x => ConvertField(x, filter));
            }

            throw new NotSupportedException($"Field type {field.GetType()} is not supported");
        }
예제 #4
0
        public string[] GetXmlForProducts(Article[] articles, IArticleFilter filter)
        {
            var token  = CallMethod("GetXmlForProducts", articles);
            var result = _xmlProductService.GetXmlForProducts(articles, filter);

            EndMethod(token, "XML");
            return(result);
        }
예제 #5
0
        public string GetProductXml(Article article, IArticleFilter filter)
        {
            var token  = CallMethod("GetProductXml", article);
            var result = _xmlProductService.GetProductXml(article, filter);

            EndMethod(token, result);
            return(result);
        }
예제 #6
0
        public Article GetItem(IArticleFilter filter)
        {
            if (filter == null || filter.Matches(Item))
            {
                return(Item);
            }

            return(null);
        }
예제 #7
0
        private string ProcessProductWithTags(IArticleFilter filter, params Article[] content)
        {
            using (
                var cs =
                    new QPConnectionScope(_customer.ConnectionString, (DatabaseType)_customer.DatabaseType))
            {
                var ctx = new CallContext(new DBConnector(cs.DbConnection), filter);

                string[] exceptions;
                var      doc = ProcessProduct(ctx, out exceptions, content);
                if (doc == null)
                {
                    return(string.Empty);
                }

                var xml = XmlToString(doc);

                //региональные замены
                if (content == null || !content.Any() || content[0] == null)
                {
                    return(xml);
                }

                //замены региональных тегов (одинаковых для любого региона, не одинаковые в exceptions)
                var regionId = 0;

                foreach (var item in content)
                {
                    var regionsField = item.GetField("Regions") as MultiArticleField;

                    if (regionsField == null || regionsField.Items.Keys.Count == 0)
                    {
                        continue;
                    }

                    regionId = regionsField.Items.Keys.FirstOrDefault();

                    break;
                }

                if (regionId == 0)
                {
                    return(xml);
                }

                return(EnableRegionTagReplacement
                                        ? _regionTagReplaceService.Replace(xml, regionId, exceptions)
                                        : xml);
            }
        }
예제 #8
0
        private static Article CopyIfNeeded(Article sourceArticle, IArticleFilter filter, Dictionary <Article, Article> copiedArticles)
        {
            if (copiedArticles.ContainsKey(sourceArticle))
            {
                return(copiedArticles[sourceArticle]);
            }

            var copiedArticle = _mapper.Map <Article, Article>(sourceArticle);

            copiedArticles[sourceArticle] = copiedArticle;

            copiedArticle.Fields = CopyArticleFields(sourceArticle.Fields, filter, copiedArticles);

            return(copiedArticle);
        }
예제 #9
0
        private IEnumerable <Article> GetChildArticles(ArticleField field, IArticleFilter filter)
        {
            if (field is IGetArticle getArticle)
            {
                Article childArticle = getArticle.GetItem(filter);

                return(childArticle != null ? new[] { childArticle } : new Article[0]);
            }
            else if (field is IGetArticles getArticles)
            {
                return(getArticles.GetArticles(filter));
            }
            else
            {
                return(new Article[0]);
            }
        }
예제 #10
0
        protected ArticleObject LoadProductGraph(Content content, int articleId, bool isLive)
        {
            var productDefinition = new ProductDefinition {
                StorageSchema = content
            };

            Article article = _productService.GetProductById(articleId, isLive, productDefinition);

            if (article == null)
            {
                return(null);
            }

            IArticleFilter filter = isLive ? ArticleFilter.LiveFilter : ArticleFilter.DefaultFilter;

            ArticleObject articleObject = _editorDataService.ConvertArticle(article, filter, new HashSet <Article>());

            return(articleObject);
        }
예제 #11
0
        /// <summary>
        /// Преобразовать DPC-Article в формат редактора продуктов
        /// </summary>
        /// <exception cref="InvalidOperationException" />
        /// <exception cref="NotSupportedException" />
        public ArticleObject ConvertArticle(Article article, IArticleFilter filter, HashSet <Article> visited)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }
            if (visited == null)
            {
                throw new ArgumentNullException(nameof(visited));
            }

            _contentService.LoadStructureCache();

            return(ConvertArticle(article, new ArticleContext
            {
                Filter = filter,
                Visited = visited,
            }));
        }
예제 #12
0
        private void MergeExtensionFields(
            Dictionary<string, object> dict, ExtensionArticleField field, IArticleFilter filter)
        {
            if (field.Item == null)
            {
                return;
            }

            dict[field.FieldName] = field.Item.ContentName;

            foreach (ArticleField childField in field.Item.Fields.Values)
            {
                object value = ConvertField(childField, filter);

                if (value != null && !(value is string && string.IsNullOrEmpty((string)value)))
                {
                    dict[childField.FieldName] = value;
                }
            }
        }
예제 #13
0
        public string SerializeProduct(Article article, IArticleFilter filter, bool includeRegionTags = false)
        {
            var sw = new Stopwatch();
            sw.Start();
            Dictionary<string, object> convertedArticle = ConvertArticle(article, filter);
            sw.Stop();
            _logger.Debug("Product {1} conversion took {0} sec", sw.Elapsed.TotalSeconds, article.Id);
            
            sw.Reset();
            sw.Start();
            string productJson = JsonConvert.SerializeObject(convertedArticle, Formatting.Indented);
            sw.Stop();
            _logger.Debug("Product {1} serializing took {0} sec", sw.Elapsed.TotalSeconds, article.Id);

            string result;
            if (includeRegionTags && article.GetField("Regions") is MultiArticleField regionField)
            {
                sw.Reset();
                sw.Start();

                int[] regionIds = regionField.Items.Keys.ToArray();

                TagWithValues[] tags = _regionTagReplaceService.GetRegionTagValues(productJson, regionIds);

                string tagsJson = JsonConvert.SerializeObject(
                    tags,
                    Formatting.Indented,
                    new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

                sw.Stop();
                _logger.Debug("Product {1} enrichment with regional tags took {0} sec", sw.Elapsed.TotalSeconds, article.Id);

                result = $"{{ \"{ProductProp}\" : {productJson}, \"{RegionTagsProp}\" : {tagsJson} }}";
            }
            else
            {
                result = $"{{ \"{ProductProp}\" : {productJson} }}";
            }

            return result;
        }
예제 #14
0
 public string GetProductXml(Article article, IArticleFilter filter)
 {
     return(string.Empty);
 }
예제 #15
0
 public string[] GetXmlForProducts(Article[] articles, IArticleFilter filter)
 {
     return(articles.Select(x => ProcessProductWithTags(filter, x).ToString()).ToArray());
 }
예제 #16
0
 public string GetSingleXmlForProducts(Article[] articles, IArticleFilter filter)
 {
     return(ProcessProductWithTags(filter, articles));
 }
예제 #17
0
 public string GetProductXml(Article article, IArticleFilter filter)
 {
     return(ProcessProductWithTags(filter, article));
 }
예제 #18
0
 public IEnumerable <Article> GetArticles(IArticleFilter filter)
 {
     return(this.Cast <Article>());
 }
예제 #19
0
 public static Article[] FilterArticles(this IArticleFilter filter, params Article[] articles)
 {
     return(filter
            .Filter(articles)
            .ToArray());
 }
예제 #20
0
 public string[] GetXmlForProducts(Article[] articles, IArticleFilter filter)
 {
     return(articles.Select(a => string.Empty).ToArray());
 }
예제 #21
0
        public IEnumerable <Article> GetArticlesSorted(IArticleFilter filter)
        {
            var values = Items.Keys.OrderBy(n => n).Select(n => Items[n]);

            return(filter == null ? values : filter.Filter(values));
        }
예제 #22
0
 public IEnumerable <Article> GetArticles(IArticleFilter filter)
 {
     return(filter == null ? Items.Values : filter.Filter(Items.Values));
 }
예제 #23
0
 string IXmlProductService.GetProductXml(Article article, IArticleFilter filter)
 {
     return(GetProductXml(article, filter));
 }
예제 #24
0
        public static async Task WriteAsync(this IArticleFormatter formatter, Stream stream, Article product, IArticleFilter filter, bool includeRegionTags)
        {
            string data = formatter.Serialize(product, filter, includeRegionTags);

            var writer = new StreamWriter(stream);
            await writer.WriteAsync(data);

            await writer.FlushAsync();
        }
예제 #25
0
        public string Serialize(Article product, IArticleFilter filter, bool includeRegionTags)
        {
            string json = _jsonProductService.SerializeProduct(product, filter, includeRegionTags);

            return(json);
        }
예제 #26
0
 public string GetSingleXmlForProducts(Article[] articles, IArticleFilter filter)
 {
     return(string.Empty);
 }
예제 #27
0
 public string Serialize(Article product, IArticleFilter filter, bool includeRegionTags)
 {
     return(_xmlProductService.GetProductXml(product, filter));
 }
예제 #28
0
 public CallContext(DBConnector cnn, IArticleFilter filter)
 {
     Cnn    = cnn;
     Filter = filter;
 }
예제 #29
0
 public string Serialize(Article product, IArticleFilter filter, bool includeRegionTags)
 {
     return(Serialize(product));
 }
예제 #30
0
 string[] IXmlProductService.GetXmlForProducts(Article[] articles, IArticleFilter filter)
 {
     return(GetXmlForProducts(articles, filter));
 }