[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

@@ -96,7 +96,7 @@ public class TagsImpl implements Tags
this.typeConstraint = typeConstraint; this.typeConstraint = typeConstraint;
} }
public void setNodes(Nodes nodes) public void setNodes(Nodes nodes)
{ {
this.nodes = nodes; this.nodes = nodes;
} }
@@ -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

@@ -1,28 +1,28 @@
/* /*
* #%L * #%L
* Alfresco Remote API * Alfresco Remote API
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.rest.api.nodes; package org.alfresco.rest.api.nodes;
import java.util.List; import java.util.List;
@@ -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