private ExtendedArticleList GetArticles(ExtendedArticleList articlesList, bool onlyExisting) { ExtendedArticleList existingArticles; if (_importSettings.UniqueContentField == null) { var existingIds = GetExistingArticleIds(articlesList.GetBaseArticleIds()); existingArticles = articlesList.Filter(a => !onlyExisting ^ existingIds.Contains(a.Id)); } else { existingArticles = new ExtendedArticleList(articlesList); var existingIdsMap = GetExistingArticleIdsMap(_uniqueValuesList, _importSettings.UniqueContentField.Name); for (var i = 0; i < articlesList.Count; i++) { var article = articlesList[i]; var uniqueValue = _uniqueValuesList[i]; var articleExists = existingIdsMap.ContainsKey(uniqueValue); if (articleExists) { article.BaseArticle.Id = existingIdsMap[uniqueValue]; } if (!onlyExisting ^ articleExists) { existingArticles.Add(article); } } } return(existingArticles); }
public ExtendedArticleList Filter(Func <Article, bool> predicate) { var result = new ExtendedArticleList(this); result.AddRange(this.Where(article => predicate(article.BaseArticle))); return(result); }
private void InsertArticles(ExtendedArticleList articlesToInsert) { if (articlesToInsert.Any()) { var insertingArticles = InsertArticlesToDb(articlesToInsert); var insertedArticles = insertingArticles.GetBaseArticleIds(); SaveInsertedArticleIdsToSettings(insertedArticles); } }
private ExtendedArticleList UpdateArticles(ExtendedArticleList articlesToUpdate) { if (articlesToUpdate.Any()) { var updatingArticles = UpdateArticlesToDb(articlesToUpdate); var updatedArticles = updatingArticles.GetBaseArticleIds(); SaveUpdatedArticleIdsToSettings(updatedArticles); return(updatingArticles); } return(articlesToUpdate); }
private void InitFields() { _fieldsMap = _importSettings .FieldsList.Select(f => f.Value) .GroupBy(f => f.ContentId) .Select(g => g) .ToDictionary(g => g.Key, g => g.ToList()); _headersMap = _importSettings.FieldsList.ToDictionary(f => f.Value, f => _titleHeaders.FindIndex(s => s == f.Key)); _aggregatedContentsMap = ContentRepository.GetAggregatedContents(_contentId).ToDictionary(c => c.Id, c => c); _articlesListFromCsv = new ExtendedArticleList(); _uniqueValuesList = new List <string>(); }
private ExtendedArticleList InsertArticlesToDb(ExtendedArticleList articleList) { var baseArticles = articleList.GetBaseArticles(); var idsList = InsertArticlesIds(baseArticles, baseArticles.All(a => a.UniqueId.HasValue)).ToArray(); _notificationRepository.PrepareNotifications(_contentId, idsList, NotificationCode.Create); InsertArticleValues(idsList.ToArray(), baseArticles); if (_importSettings.ContainsO2MRelationOrM2MRelationFields) { SaveNewRelationsToFile(baseArticles, idsList); } for (var i = 0; i < idsList.Length; i++) { var id = idsList[i]; var article = articleList[i]; foreach (var aggregatedArticle in article.Extensions.Values.Where(ex => ex != null)) { var parent = aggregatedArticle.FieldValues.Find(fv => fv.Field.Aggregated); parent.Value = id.ToString(); } } var aggregatedArticles = articleList.GetAllAggregatedArticles().ToList(); foreach (var aggregatedArticleList in aggregatedArticles) { var aggregatedIdsList = InsertArticlesIds(aggregatedArticleList); InsertArticleValues(aggregatedIdsList.ToArray(), aggregatedArticleList); if (_importSettings.ContainsO2MRelationOrM2MRelationFields) { SaveNewRelationsToFile(aggregatedArticleList, aggregatedIdsList); } } _notificationRepository.SendNotifications(); return(articleList); }
private ExtendedArticleList UpdateArticlesToDb(ExtendedArticleList articlesList) { var existingArticles = GetArticles(articlesList, true); var extensionsMap = ContentRepository.GetAggregatedArticleIdsMap(_contentId, existingArticles.GetBaseArticleIds().ToArray()); var idsList = existingArticles.GetBaseArticleIds().ToArray(); _notificationRepository.PrepareNotifications(_contentId, idsList, NotificationCode.Update); InsertArticleValues(idsList, existingArticles.GetBaseArticles(), true); var idsToDelete = new List <int>(); var articlesToInsert = new List <Article>(); var idsToUpdate = new List <int>(); var articlesToUpdate = new List <Article>(); foreach (var article in existingArticles) { foreach (var afv in article.Extensions.Where(ex => ex.Value != null)) { var aggregatedArticle = afv.Value; var fieldId = afv.Key.Id; if (extensionsMap.ContainsKey(article.BaseArticle.Id) && extensionsMap[article.BaseArticle.Id].ContainsKey(fieldId)) { var currentId = extensionsMap[article.BaseArticle.Id][fieldId]; if (currentId != aggregatedArticle.Id) { aggregatedArticle.Id = currentId; } idsToUpdate.Add(aggregatedArticle.Id); articlesToUpdate.Add(aggregatedArticle); } else { articlesToInsert.Add(aggregatedArticle); } var parent = aggregatedArticle.FieldValues.Find(fv => fv.Field.Aggregated); parent.Value = article.BaseArticle.Id.ToString(); } } if (idsToDelete.Any()) { ArticleRepository.MultipleDelete(idsToDelete); } if (articlesToInsert.Any()) { var inserdedIds = InsertArticlesIds(articlesToInsert); if (_importSettings.ContainsO2MRelationOrM2MRelationFields) { SaveNewRelationsToFile(articlesToInsert, inserdedIds); } idsToUpdate.AddRange(inserdedIds); articlesToUpdate.AddRange(articlesToInsert); } InsertArticleValues(idsToUpdate.ToArray(), articlesToUpdate, true); _notificationRepository.SendNotifications(); return(existingArticles); }
public ExtendedArticleList(ExtendedArticleList articles) { ExtensionFields = new HashSet <Field>(articles.ExtensionFields); }