From 2bfc0e7f993ee7f1ca5cec709300aeffd9588748 Mon Sep 17 00:00:00 2001 From: Roy Wetherall Date: Tue, 29 Jul 2008 11:44:16 +0000 Subject: [PATCH] Tagging Service: make sure that tag scopes are updated correctly when a tag array is 'set' on a node reference git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@10095 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../repo/tagging/TaggingServiceImpl.java | 30 ++++- .../repo/tagging/TaggingServiceImplTest.java | 113 +++++++++++++++++- 2 files changed, 137 insertions(+), 6 deletions(-) diff --git a/source/java/org/alfresco/repo/tagging/TaggingServiceImpl.java b/source/java/org/alfresco/repo/tagging/TaggingServiceImpl.java index 1d448ebc9d..0baaa02ca5 100644 --- a/source/java/org/alfresco/repo/tagging/TaggingServiceImpl.java +++ b/source/java/org/alfresco/repo/tagging/TaggingServiceImpl.java @@ -381,6 +381,9 @@ public class TaggingServiceImpl implements TaggingService this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_TAGGABLE, null); } + // Get the current list of tags + List oldTags = getTags(nodeRef); + for (String tag : tags) { // Lower the case of the tag @@ -394,11 +397,28 @@ public class TaggingServiceImpl implements TaggingService newTagNodeRef = this.categoryService.createRootCategory(nodeRef.getStoreRef(), ContentModel.ASPECT_TAGGABLE, tag); } - // Add to the list - tagNodeRefs.add(newTagNodeRef); - - // Trigger scope update - updateTagScope(nodeRef, tag, true); + if (tagNodeRefs.contains(newTagNodeRef) == false) + { + // Add to the list + tagNodeRefs.add(newTagNodeRef); + + // Trigger scope update + if (oldTags.contains(tag) == false) + { + updateTagScope(nodeRef, tag, true); + } + else + { + // Remove the tag from the old list + oldTags.remove(tag); + } + } + } + + // Remove the old tags from the tag scope + for (String oldTag : oldTags) + { + updateTagScope(nodeRef, oldTag, false); } // Update category property diff --git a/source/java/org/alfresco/repo/tagging/TaggingServiceImplTest.java b/source/java/org/alfresco/repo/tagging/TaggingServiceImplTest.java index a98c3b6274..a51ba0b50f 100644 --- a/source/java/org/alfresco/repo/tagging/TaggingServiceImplTest.java +++ b/source/java/org/alfresco/repo/tagging/TaggingServiceImplTest.java @@ -387,6 +387,41 @@ public class TaggingServiceImplTest extends BaseAlfrescoSpringTest tx.commit(); } + public void xtestTagScopeSetUpdate() + throws Exception + { + // Set up tag scope + this.taggingService.addTagScope(this.folder); + TagScope ts1 = this.taggingService.findTagScope(this.folder); + + setComplete(); + endTransaction(); + + addTag(this.folder, TAG_1, 1, ts1.getNodeRef()); + addTag(this.document, TAG_1, 2, ts1.getNodeRef()); + addTag(this.document, TAG_2, 1, ts1.getNodeRef()); + addTag(this.subDocument, TAG_1, 3, ts1.getNodeRef()); + addTag(this.subDocument, TAG_2, 2, ts1.getNodeRef()); + addTag(this.subDocument, TAG_3, 1, ts1.getNodeRef()); + addTag(this.subFolder, TAG_1, 4, ts1.getNodeRef()); + + UserTransaction tx = this.transactionService.getUserTransaction(); + tx.begin(); + + ts1 = this.taggingService.findTagScope(this.folder); + assertEquals(4, ts1.getTag(TAG_1).getCount()); + assertEquals(2, ts1.getTag(TAG_2).getCount()); + //assertEquals(1, ts1.getTag(TAG_3).getCount()); + + tx.commit(); + + List tags = new ArrayList(3); + tags.add(TAG_2); + tags.add(TAG_3); + + setTags(this.subDocument, tags, new String[]{TAG_1,TAG_2,TAG_3}, new int[]{3,2,1}, ts1.getNodeRef()); + } + private void addTag(NodeRef nodeRef, String tag, int tagCount, NodeRef tagScopeNodeRef) throws Exception { @@ -423,7 +458,7 @@ public class TaggingServiceImplTest extends BaseAlfrescoSpringTest } assertNotNull(checkTagScope); - // Check that tag scopes are in the correct order + // Check whether the tag scope has been updated List tagDetailsList = checkTagScope.getTags(); for (TagDetails tagDetails : tagDetailsList) { @@ -533,6 +568,82 @@ public class TaggingServiceImplTest extends BaseAlfrescoSpringTest } } + private void setTags(NodeRef nodeRef, List tags, String[] expectedTags, int[] expectedTagCount, NodeRef tagScopeNodeRef) + throws Exception + { + UserTransaction tx = this.transactionService.getUserTransaction(); + tx.begin(); + + // Add some tags + this.taggingService.setTags(nodeRef, tags); + + tx.commit(); + + // Wait a bit cos we want the background threads to kick in and update the tag scope + int count = 0; + boolean bCreated = true; + while (true) + { + UserTransaction tx2 = this.transactionService.getUserTransaction(); + tx2.begin(); + + try + { + // Get the tag scope + List tagScopes = this.taggingService.findAllTagScopes(nodeRef); + TagScope checkTagScope = null; + for (TagScope tagScope : tagScopes) + { + if (tagScope.getNodeRef().equals(tagScopeNodeRef) == true) + { + checkTagScope = tagScope; + break; + } + } + assertNotNull(checkTagScope); + + // Check whether the tag scope has been updated + List tagDetailsList = checkTagScope.getTags(); + if (tagDetailsList.size() == expectedTags.length) + { + int index = 0; + for (TagDetails tagDetails : tagDetailsList) + { + if (tagDetails.getName().equals(expectedTags[index]) == false || + tagDetails.getCount() != expectedTagCount[index]) + { + bCreated = false; + break; + } + index ++; + } + } + else + { + bCreated = false; + } + + if (bCreated == true) + { + break; + } + + // Wait to give the threads a chance to execute + Thread.sleep(1000); + + if (count == 10) + { + fail("The background task to update the tag scope after set tag failed"); + } + count ++; + } + finally + { + tx2.commit(); + } + } + } + // == Test the JavaScript API == public void testJSAPI() throws Exception