[ACS-4636] Added count support for creation of tags to a node

This commit is contained in:
suneet.gupta
2023-03-23 18:35:12 +05:30
parent f2d858f911
commit 626e5b34ef
4 changed files with 39 additions and 33 deletions

View File

@@ -37,7 +37,7 @@ import org.alfresco.service.cmr.repository.StoreRef;
public interface Tags public interface Tags
{ {
List<Tag> addTags(String nodeId, List<Tag> tags); List<Tag> addTags(String nodeId, List<Tag> tags, Parameters parameters);
Tag getTag(StoreRef storeRef, String tagId); Tag getTag(StoreRef storeRef, String tagId);
void deleteTag(String nodeId, String tagId); void deleteTag(String nodeId, String tagId);
CollectionWithPagingInfo<Tag> getTags(StoreRef storeRef, Parameters params); CollectionWithPagingInfo<Tag> getTags(StoreRef storeRef, Parameters params);

View File

@@ -115,7 +115,7 @@ public class TagsImpl implements Tags
this.authorityService = authorityService; this.authorityService = authorityService;
} }
public List<Tag> addTags(String nodeId, final List<Tag> tags) public List<Tag> addTags(String nodeId, final List<Tag> tags, final Parameters parameters)
{ {
NodeRef nodeRef = nodes.validateOrLookupNode(nodeId, null); NodeRef nodeRef = nodes.validateOrLookupNode(nodeId, null);
if (!typeConstraint.matches(nodeRef)) if (!typeConstraint.matches(nodeRef))
@@ -128,9 +128,15 @@ public class TagsImpl implements Tags
{ {
List<Pair<String, NodeRef>> tagNodeRefs = taggingService.addTags(nodeRef, tagValues); List<Pair<String, NodeRef>> tagNodeRefs = taggingService.addTags(nodeRef, tagValues);
List<Tag> ret = new ArrayList<>(tags.size()); List<Tag> ret = new ArrayList<>(tags.size());
List<Pair<String, Integer>> tagsCountPairList = taggingService.findTaggedNodesAndCountByTagName(nodeRef.getStoreRef());
Map<String, Integer> tagsCountMap = tagsCountPairList.stream().collect(Collectors.toMap(Pair::getFirst,Pair::getSecond));
for (Pair<String, NodeRef> pair : tagNodeRefs) for (Pair<String, NodeRef> pair : tagNodeRefs)
{ {
ret.add(new Tag(pair.getSecond(), pair.getFirst())); Tag createdTag = new Tag(pair.getSecond(), pair.getFirst());
if (parameters.getInclude().contains(PARAM_INCLUDE_COUNT)) {
createdTag.setCount(Optional.ofNullable(tagsCountMap.get(createdTag.getTag())).orElse(0) + 1);
}
ret.add(createdTag);
} }
return ret; return ret;
} }

View File

@@ -61,7 +61,7 @@ public class NodeTagsRelation implements RelationshipResourceAction.Create<Tag>,
@WebApiDescription(title="Adds one or more tags to the node with id 'nodeId'.") @WebApiDescription(title="Adds one or more tags to the node with id 'nodeId'.")
public List<Tag> create(String nodeId, List<Tag> tagsToCreate, Parameters parameters) public List<Tag> create(String nodeId, List<Tag> tagsToCreate, Parameters parameters)
{ {
return tags.addTags(nodeId, tagsToCreate); return tags.addTags(nodeId, tagsToCreate, parameters);
} }
@Override @Override

View File

@@ -429,7 +429,7 @@ public class TagsImplTest
List<Tag> tags = tagNames.stream().map(name -> Tag.builder().tag(name).create()).collect(toList()); List<Tag> tags = tagNames.stream().map(name -> Tag.builder().tag(name).create()).collect(toList());
given(taggingServiceMock.addTags(CONTENT_NODE_REF, tagNames)).willReturn(pairs); given(taggingServiceMock.addTags(CONTENT_NODE_REF, tagNames)).willReturn(pairs);
List<Tag> actual = objectUnderTest.addTags(CONTENT_NODE_ID, tags); List<Tag> actual = objectUnderTest.addTags(CONTENT_NODE_ID, tags, parametersMock);
List<Tag> expected = pairs.stream().map(pair -> new Tag(pair.getSecond(), pair.getFirst())).collect(toList()); List<Tag> expected = pairs.stream().map(pair -> new Tag(pair.getSecond(), pair.getFirst())).collect(toList());
assertEquals("Unexpected tags returned.", expected, actual); assertEquals("Unexpected tags returned.", expected, actual);
@@ -441,7 +441,7 @@ public class TagsImplTest
given(nodesMock.validateOrLookupNode(CONTENT_NODE_ID, null)).willThrow(new InvalidArgumentException()); given(nodesMock.validateOrLookupNode(CONTENT_NODE_ID, null)).willThrow(new InvalidArgumentException());
List<Tag> tags = List.of(Tag.builder().tag("tag1").create()); List<Tag> tags = List.of(Tag.builder().tag("tag1").create());
objectUnderTest.addTags(CONTENT_NODE_ID, tags); objectUnderTest.addTags(CONTENT_NODE_ID, tags, parametersMock);
} }
@Test(expected = UnsupportedResourceOperationException.class) @Test(expected = UnsupportedResourceOperationException.class)
@@ -452,7 +452,7 @@ public class TagsImplTest
List<Tag> tags = List.of(Tag.builder().tag("tag1").create()); List<Tag> tags = List.of(Tag.builder().tag("tag1").create());
objectUnderTest.addTags(CONTENT_NODE_ID, tags); objectUnderTest.addTags(CONTENT_NODE_ID, tags, parametersMock);
} }
@Test @Test