public void SetTags(DocumentImportDTO documentDto, Webpage webpage) { List<string> tagsToAdd = documentDto.Tags.Where( s => !webpage.Tags.Select(tag => tag.Name).Contains(s, StringComparer.InvariantCultureIgnoreCase)) .ToList(); List<Tag> tagsToRemove = webpage.Tags.Where( tag => !documentDto.Tags.Contains(tag.Name, StringComparer.InvariantCultureIgnoreCase)).ToList(); foreach (string item in tagsToAdd) { Tag tag = GetExistingTag(item); bool isNew = tag == null; if (isNew) { tag = new Tag {Name = item}; _session.Transact(session => session.Save(tag)); } if (!webpage.Tags.Contains(tag)) webpage.Tags.Add(tag); if (!tag.Documents.Contains(webpage)) tag.Documents.Add(webpage); _session.Transact(session => session.Update(tag)); } foreach (Tag tag in tagsToRemove) { webpage.Tags.Remove(tag); tag.Documents.Remove(webpage); Tag closureTag = tag; _session.Transact(session => session.Update(closureTag)); } }
public void SetTags(string taglist, Document document) { if (document == null) throw new ArgumentNullException("document"); if (taglist == null) taglist = string.Empty; var tagNames = taglist.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).Where( x => !string.IsNullOrWhiteSpace(x)); var existingTags = document.Tags.ToList(); tagNames.ForEach(name => { var tag = GetTag(name); if (tag == null) { tag = new Tag { Name = name }; _session.Transact(session => session.Save(tag)); } if (!document.Tags.Contains(tag)) { document.Tags.Add(tag); tag.Documents.Add(document); } existingTags.Remove(tag); }); existingTags.ForEach(tag => { document.Tags.Remove(tag); tag.Documents.Remove(document); }); }
public string ProcessTags(NopCommerceDataReader dataReader, NopImportContext nopImportContext) { HashSet<TagData> tagDatas = dataReader.GetTags(); foreach (TagData tagData in tagDatas) { string name = tagData.Name.Trim(); Tag tag = _session.QueryOver<Tag>() .Where(b => b.Name.IsInsensitiveLike(name, MatchMode.Exact)) .List().FirstOrDefault(); if (tag == null) { tag = new Tag { Name = name }; _session.Transact(session => session.Save(tag)); } nopImportContext.AddEntry(tagData.Id, tag); } return string.Format("{0} tags processed", tagDatas.Count); }
public void DocumentTagsAdminService_SetTags_ShouldNotRecreateTags() { var textPage = new StubWebpage(); var tag1 = new Tag {Name = "test 1"}; var tag2 = new Tag {Name = "test 2"}; textPage.Tags.Add(tag1); textPage.Tags.Add(tag2); Session.Transact(session => { session.SaveOrUpdate(textPage); session.SaveOrUpdate(tag1); session.SaveOrUpdate(tag2); }); _documentTagsAdminService.SetTags(textPage.TagList, textPage); Session.QueryOver<Tag>().RowCount().Should().Be(2); }
public void TagAdminService_Search_ShouldReturnTagsStartingWithTerm() { var tagService = new TagAdminService(Session); var tag1 = new Tag {Name = "tag-1", Site = CurrentSite}; var tag2 = new Tag {Name = "tag-2", Site = CurrentSite}; var tag3 = new Tag {Name = "not-the-same", Site = CurrentSite}; Session.Transact(session => { Session.SaveOrUpdate(tag1); Session.SaveOrUpdate(tag2); Session.SaveOrUpdate(tag3); }); IEnumerable<AutoCompleteResult> tags = tagService.Search("tag"); tags.Should().HaveCount(2); tags.First().label.Should().Be("tag-1"); tags.Skip(1).First().label.Should().Be("tag-2"); }
public void ShouldRemoveTheWebpageFromTheTagsWebpages() { var tag = new Tag { Name = "Test" }; Session.Transact(session => session.Save(tag)); GetAllTags().Should().HaveCount(1); var webpage = new BasicMappedWebpage { Tags = new HashSet<Tag> { tag } }; tag.Documents.Add(webpage); tag.Documents.Should().HaveCount(1); _updateTagsService.SetTags(new DocumentImportDTO { Tags = new List<string>() }, webpage); tag.Documents.Should().HaveCount(0); }
public void IfTagIsRemovedFromWebpageShouldNotRemoveTagFromList() { var tag = new Tag { Name = "Test" }; Session.Transact(session => session.Save(tag)); GetAllTags().Should().HaveCount(1); var webpage = new BasicMappedWebpage { Tags = new HashSet<Tag> { tag } }; _updateTagsService.SetTags(new DocumentImportDTO { Tags = new List<string> { } }, webpage); GetAllTags().Should().HaveCount(1); }
public void ShouldRemoveTagIfItIsNoLongerAssignedWebpage() { var tag = new Tag { Name = "Test" }; Session.Transact(session => session.Save(tag)); GetAllTags().Should().HaveCount(1); var webpage = new BasicMappedWebpage { Tags = new HashSet<Tag> { tag } }; _updateTagsService.SetTags(new DocumentImportDTO { Tags = new List<string> { } }, webpage); webpage.Tags.Should().HaveCount(0); }
public void DocumentTagsAdminService_SetTags_ShouldRemoveTheDocumentFromTags() { var textPage = new StubWebpage(); var tag1 = new Tag {Name = "test 1"}; var tag2 = new Tag {Name = "test 2"}; textPage.Tags.Add(tag1); textPage.Tags.Add(tag2); tag1.Documents.Add(textPage); tag2.Documents.Add(textPage); Session.Transact(session => { session.SaveOrUpdate(textPage); session.SaveOrUpdate(tag1); session.SaveOrUpdate(tag2); }); _documentTagsAdminService.SetTags("test 1", textPage); tag1.Documents.Should().HaveCount(1); tag2.Documents.Should().HaveCount(0); }