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")); }
/// <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"); }
public string[] GetXmlForProducts(Article[] articles, IArticleFilter filter) { var token = CallMethod("GetXmlForProducts", articles); var result = _xmlProductService.GetXmlForProducts(articles, filter); EndMethod(token, "XML"); return(result); }
public string GetProductXml(Article article, IArticleFilter filter) { var token = CallMethod("GetProductXml", article); var result = _xmlProductService.GetProductXml(article, filter); EndMethod(token, result); return(result); }
public Article GetItem(IArticleFilter filter) { if (filter == null || filter.Matches(Item)) { return(Item); } return(null); }
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); } }
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); }
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]); } }
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); }
/// <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, })); }
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; } } }
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; }
public string GetProductXml(Article article, IArticleFilter filter) { return(string.Empty); }
public string[] GetXmlForProducts(Article[] articles, IArticleFilter filter) { return(articles.Select(x => ProcessProductWithTags(filter, x).ToString()).ToArray()); }
public string GetSingleXmlForProducts(Article[] articles, IArticleFilter filter) { return(ProcessProductWithTags(filter, articles)); }
public string GetProductXml(Article article, IArticleFilter filter) { return(ProcessProductWithTags(filter, article)); }
public IEnumerable <Article> GetArticles(IArticleFilter filter) { return(this.Cast <Article>()); }
public static Article[] FilterArticles(this IArticleFilter filter, params Article[] articles) { return(filter .Filter(articles) .ToArray()); }
public string[] GetXmlForProducts(Article[] articles, IArticleFilter filter) { return(articles.Select(a => string.Empty).ToArray()); }
public IEnumerable <Article> GetArticlesSorted(IArticleFilter filter) { var values = Items.Keys.OrderBy(n => n).Select(n => Items[n]); return(filter == null ? values : filter.Filter(values)); }
public IEnumerable <Article> GetArticles(IArticleFilter filter) { return(filter == null ? Items.Values : filter.Filter(Items.Values)); }
string IXmlProductService.GetProductXml(Article article, IArticleFilter filter) { return(GetProductXml(article, filter)); }
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(); }
public string Serialize(Article product, IArticleFilter filter, bool includeRegionTags) { string json = _jsonProductService.SerializeProduct(product, filter, includeRegionTags); return(json); }
public string GetSingleXmlForProducts(Article[] articles, IArticleFilter filter) { return(string.Empty); }
public string Serialize(Article product, IArticleFilter filter, bool includeRegionTags) { return(_xmlProductService.GetProductXml(product, filter)); }
public CallContext(DBConnector cnn, IArticleFilter filter) { Cnn = cnn; Filter = filter; }
public string Serialize(Article product, IArticleFilter filter, bool includeRegionTags) { return(Serialize(product)); }
string[] IXmlProductService.GetXmlForProducts(Article[] articles, IArticleFilter filter) { return(GetXmlForProducts(articles, filter)); }