From 8f1c38fcf7dd3a6e0c58d57e7f4949215818efaa Mon Sep 17 00:00:00 2001 From: Roy Wetherall Date: Tue, 8 Jul 2008 16:58:56 +0000 Subject: [PATCH] Tagging Service: Complete JS and Java APIs git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9689 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- config/alfresco/tagging-services-context.xml | 8 + .../org/alfresco/repo/jscript/ScriptNode.java | 80 +++++++++- .../org/alfresco/repo/jscript/Search.java | 31 ++++ .../alfresco/repo/tagging/TagDetailsImpl.java | 8 +- .../alfresco/repo/tagging/TagScopeImpl.java | 2 +- .../repo/tagging/TaggingServiceImpl.java | 128 ++++++++++++++-- .../repo/tagging/TaggingServiceImplTest.java | 92 ++++++++++-- .../UpdateTagScopesActionExecuter.java | 4 +- .../tagging/script/ScriptTaggingService.java | 81 ++++++++++ .../repo/tagging/script/TagScope.java | 90 +++++++++++ .../tagging/script/test_taggingService.js | 140 +++++++++++++++++- .../service/cmr/tagging/TagDetails.java | 4 +- .../service/cmr/tagging/TaggingService.java | 36 ++++- 13 files changed, 660 insertions(+), 44 deletions(-) create mode 100644 source/java/org/alfresco/repo/tagging/script/ScriptTaggingService.java create mode 100644 source/java/org/alfresco/repo/tagging/script/TagScope.java diff --git a/config/alfresco/tagging-services-context.xml b/config/alfresco/tagging-services-context.xml index d5f1e8a715..94caf5820f 100644 --- a/config/alfresco/tagging-services-context.xml +++ b/config/alfresco/tagging-services-context.xml @@ -39,6 +39,7 @@ + @@ -50,4 +51,11 @@ + + + taggingService + + + + diff --git a/source/java/org/alfresco/repo/jscript/ScriptNode.java b/source/java/org/alfresco/repo/jscript/ScriptNode.java index a0a258e816..da76a9bc8f 100644 --- a/source/java/org/alfresco/repo/jscript/ScriptNode.java +++ b/source/java/org/alfresco/repo/jscript/ScriptNode.java @@ -28,6 +28,7 @@ import java.io.InputStream; import java.io.Serializable; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -40,6 +41,7 @@ import org.alfresco.model.ContentModel; import org.alfresco.repo.action.executer.TransformActionExecuter; import org.alfresco.repo.content.transform.magick.ImageTransformationOptions; import org.alfresco.repo.search.QueryParameterDefImpl; +import org.alfresco.repo.tagging.script.TagScope; import org.alfresco.repo.thumbnail.CreateThumbnailActionExecuter; import org.alfresco.repo.thumbnail.ThumbnailDetails; import org.alfresco.repo.thumbnail.ThumbnailRegistry; @@ -2004,7 +2006,7 @@ public class ScriptNode implements Serializable, Scopeable /** * Get the all the thumbnails for a given node's content property. * - * @return ScriptThumbnail list of thumbnails, empty if none available + * @return Scriptable list of thumbnails, empty if none available */ public ScriptThumbnail[] getThumbnails() { @@ -2026,6 +2028,14 @@ public class ScriptNode implements Serializable, Scopeable // ------------------------------------------------------------------------------ // Tag methods + /** + * Clear the node's tags + */ + public void clearTags() + { + this.services.getTaggingService().clearTags(this.nodeRef); + } + /** * Adds a tag to the node * @@ -2036,6 +2046,16 @@ public class ScriptNode implements Serializable, Scopeable this.services.getTaggingService().addTag(this.nodeRef, tag); } + /** + * Adds all the tags to the node + * + * @param tags array of tag names + */ + public void addTags(String[] tags) + { + this.services.getTaggingService().addTags(this.nodeRef, Arrays.asList(tags)); + } + /** * Removes a tag from the node * @@ -2046,6 +2066,16 @@ public class ScriptNode implements Serializable, Scopeable this.services.getTaggingService().removeTag(this.nodeRef, tag); } + /** + * Removes all the tags from the node + * + * @param tags array of tag names + */ + public void removeTags(String[] tags) + { + this.services.getTaggingService().removeTags(this.nodeRef, Arrays.asList(tags)); + } + /** * Get all the tags applied to this node * @@ -2057,6 +2087,17 @@ public class ScriptNode implements Serializable, Scopeable return (String[])tags.toArray(new String[tags.size()]); } + /** + * Set the tags applied to this node. This overwirtes the list of tags currently applied to the + * node. + * + * @param tags array of tags + */ + public void setTags(String[] tags) + { + this.services.getTaggingService().setTags(this.nodeRef, Arrays.asList(tags)); + } + /** * Sets whether this node is a tag scope or not * @@ -2090,6 +2131,43 @@ public class ScriptNode implements Serializable, Scopeable return this.services.getTaggingService().isTagScope(this.nodeRef); } + /** + * Gets the 'nearest' tag scope to this node by travesing up the parent hierarchy untill one is found. + *

+ * If none is found, null is returned. + * + * @return TagScope the 'nearest' tag scope + */ + public TagScope getTagScope() + { + TagScope tagScope = null; + org.alfresco.service.cmr.tagging.TagScope tagScopeImpl = this.services.getTaggingService().findTagScope(this.nodeRef); + if (tagScopeImpl != null) + { + tagScope = new TagScope(tagScopeImpl); + } + return tagScope; + } + + /** + * Gets all (deep) children of this node that have the tag specified. + * + * @param tag tag name + * @return ScriptNode[] nodes that are deep children of the node with the tag + */ + public ScriptNode[] childrenByTags(String tag) + { + List nodeRefs = this.services.getTaggingService().findTaggedNodes(this.nodeRef.getStoreRef(), tag, this.nodeRef); + ScriptNode[] nodes = new ScriptNode[nodeRefs.size()]; + int index = 0; + for (NodeRef node : nodeRefs) + { + nodes[index] = new ScriptNode(node, this.services, this.scope); + index ++; + } + return nodes; + } + // ------------------------------------------------------------------------------ // Helper methods diff --git a/source/java/org/alfresco/repo/jscript/Search.java b/source/java/org/alfresco/repo/jscript/Search.java index 062f4778e5..8c908e0f8e 100644 --- a/source/java/org/alfresco/repo/jscript/Search.java +++ b/source/java/org/alfresco/repo/jscript/Search.java @@ -26,6 +26,7 @@ package org.alfresco.repo.jscript; import java.io.StringReader; import java.util.LinkedHashSet; +import java.util.List; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; @@ -354,6 +355,36 @@ public final class Search extends BaseScopableProcessorExtension return Context.getCurrentContext().newArray(getScope(), 0); } } + + /** + * Searchs the store for all nodes with the given tag applied. + * + * @param store store ref string, default used if null provided + * @param tag tag name + * @return ScriptNode[] nodes with tag applied + */ + public ScriptNode[] tagSearch(String store, String tag) + { + StoreRef searchStoreRef = null; + if (store != null) + { + searchStoreRef = new StoreRef(store); + } + else + { + searchStoreRef = this.storeRef; + } + + List nodeRefs = this.services.getTaggingService().findTaggedNodes(searchStoreRef, tag); + ScriptNode[] nodes = new ScriptNode[nodeRefs.size()]; + int index = 0; + for (NodeRef node : nodeRefs) + { + nodes[index] = new ScriptNode(node, this.services, getScope()); + index ++; + } + return nodes; + } /** * Execute the query diff --git a/source/java/org/alfresco/repo/tagging/TagDetailsImpl.java b/source/java/org/alfresco/repo/tagging/TagDetailsImpl.java index 64bbe56240..f84736582a 100644 --- a/source/java/org/alfresco/repo/tagging/TagDetailsImpl.java +++ b/source/java/org/alfresco/repo/tagging/TagDetailsImpl.java @@ -55,7 +55,7 @@ public class TagDetailsImpl implements TagDetails /** * @see org.alfresco.service.cmr.tagging.TagDetails#getTagName() */ - public String getTagName() + public String getName() { return this.tagName; } @@ -63,7 +63,7 @@ public class TagDetailsImpl implements TagDetails /** * @see org.alfresco.service.cmr.tagging.TagDetails#getTagCount() */ - public int getTagCount() + public int getCount() { return this.tagCount; } @@ -120,11 +120,11 @@ public class TagDetailsImpl implements TagDetails public int compareTo(TagDetails o) { int result = 0; - if (this.tagCount < o.getTagCount()) + if (this.tagCount < o.getCount()) { result = 1; } - else if (this.tagCount > o.getTagCount()) + else if (this.tagCount > o.getCount()) { result = -1; } diff --git a/source/java/org/alfresco/repo/tagging/TagScopeImpl.java b/source/java/org/alfresco/repo/tagging/TagScopeImpl.java index 512a57572e..dc0c7d716d 100644 --- a/source/java/org/alfresco/repo/tagging/TagScopeImpl.java +++ b/source/java/org/alfresco/repo/tagging/TagScopeImpl.java @@ -90,7 +90,7 @@ public class TagScopeImpl implements TagScope TagDetails result = null; for (TagDetails tagDetails : this.tagDetails) { - if (tagDetails.getTagName().equals(tag) == true) + if (tagDetails.getName().equals(tag) == true) { result = tagDetails; break; diff --git a/source/java/org/alfresco/repo/tagging/TaggingServiceImpl.java b/source/java/org/alfresco/repo/tagging/TaggingServiceImpl.java index 4bf4dbef19..1d448ebc9d 100644 --- a/source/java/org/alfresco/repo/tagging/TaggingServiceImpl.java +++ b/source/java/org/alfresco/repo/tagging/TaggingServiceImpl.java @@ -43,6 +43,7 @@ import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentService; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.Path; import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.search.CategoryService; import org.alfresco.service.cmr.search.ResultSet; @@ -50,6 +51,7 @@ import org.alfresco.service.cmr.search.SearchService; import org.alfresco.service.cmr.tagging.TagDetails; import org.alfresco.service.cmr.tagging.TagScope; import org.alfresco.service.cmr.tagging.TaggingService; +import org.alfresco.service.namespace.NamespaceService; import org.alfresco.util.ISO9075; /** @@ -74,6 +76,9 @@ public class TaggingServiceImpl implements TaggingService /** Content Service */ private ContentService contentService; + /** Namespace Service */ + private NamespaceService namespaceService; + /** Tag Details Delimiter */ private static final String TAG_DETAILS_DELIMITER = "|"; @@ -127,6 +132,16 @@ public class TaggingServiceImpl implements TaggingService this.contentService = contentService; } + /** + * Set the namespace service + * + * @param namespaceService namespace service + */ + public void setNamespaceService(NamespaceService namespaceService) + { + this.namespaceService = namespaceService; + } + /** * @see org.alfresco.service.cmr.tagging.TaggingService#isTag(java.lang.String) */ @@ -187,8 +202,17 @@ public class TaggingServiceImpl implements TaggingService */ public List getTags(StoreRef storeRef, String filter) { - // TODO - return null; + Collection rootCategories = this.categoryService.getRootCategories(storeRef, ContentModel.ASPECT_TAGGABLE); + List result = new ArrayList(rootCategories.size()); + for (ChildAssociationRef rootCategory : rootCategories) + { + String name = (String)this.nodeService.getProperty(rootCategory.getChildRef(), ContentModel.PROP_NAME); + if (name.contains(filter.toLowerCase()) == true) + { + result.add(name); + } + } + return result; } /** @@ -232,6 +256,17 @@ public class TaggingServiceImpl implements TaggingService } } + /** + * @see org.alfresco.service.cmr.tagging.TaggingService#addTags(org.alfresco.service.cmr.repository.NodeRef, java.util.List) + */ + public void addTags(NodeRef nodeRef, List tags) + { + for (String tag : tags) + { + addTag(nodeRef, tag); + } + } + /** * Gets the node reference for a given tag. *

@@ -296,7 +331,18 @@ public class TaggingServiceImpl implements TaggingService } } } - } + } + + /** + * @see org.alfresco.service.cmr.tagging.TaggingService#removeTags(org.alfresco.service.cmr.repository.NodeRef, java.util.List) + */ + public void removeTags(NodeRef nodeRef, List tags) + { + for (String tag : tags) + { + removeTag(nodeRef, tag); + } + } /** * @see org.alfresco.service.cmr.tagging.TaggingService#getTags(org.alfresco.service.cmr.repository.NodeRef) @@ -323,6 +369,50 @@ public class TaggingServiceImpl implements TaggingService return result; } + /** + * @see org.alfresco.service.cmr.tagging.TaggingService#setTags(org.alfresco.service.cmr.repository.NodeRef, java.util.List) + */ + public void setTags(NodeRef nodeRef, List tags) + { + List tagNodeRefs = new ArrayList(tags.size()); + if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_TAGGABLE) == false) + { + // Add the aspect + this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_TAGGABLE, null); + } + + for (String tag : tags) + { + // Lower the case of the tag + tag = tag.toLowerCase(); + + // Get the tag node reference + NodeRef newTagNodeRef = getTagNodeRef(nodeRef.getStoreRef(), tag); + if (newTagNodeRef == null) + { + // Create the new tag + newTagNodeRef = this.categoryService.createRootCategory(nodeRef.getStoreRef(), ContentModel.ASPECT_TAGGABLE, tag); + } + + // Add to the list + tagNodeRefs.add(newTagNodeRef); + + // Trigger scope update + updateTagScope(nodeRef, tag, true); + } + + // Update category property + this.nodeService.setProperty(nodeRef, ContentModel.PROP_TAGS, (Serializable)tagNodeRefs); + } + + /** + * @see org.alfresco.service.cmr.tagging.TaggingService#clearTags(org.alfresco.service.cmr.repository.NodeRef) + */ + public void clearTags(NodeRef nodeRef) + { + setTags(nodeRef, Collections.EMPTY_LIST); + } + /** * @see org.alfresco.service.cmr.tagging.TaggingService#isTagScope(org.alfresco.service.cmr.repository.NodeRef) */ @@ -448,31 +538,37 @@ public class TaggingServiceImpl implements TaggingService /** * @see org.alfresco.service.cmr.tagging.TaggingService#findTaggedNodes(java.lang.String) */ - public List findTaggedNodes(String tag) + public List findTaggedNodes(StoreRef storeRef, String tag) { // Lower the case of the tag tag = tag.toLowerCase(); - // - // "+PATH:\"/cm:taggable/cm:" + ISOed(tag) + "/member\"" - - // TODO - return null; + // Do the search for nodes + ResultSet resultSet = this.searchService.query( + storeRef, + SearchService.LANGUAGE_LUCENE, + "+PATH:\"/cm:taggable/cm:" + ISO9075.encode(tag) + "/member\""); + return resultSet.getNodeRefs(); } /** * @see org.alfresco.service.cmr.tagging.TaggingService#findTaggedNodes(java.lang.String, org.alfresco.service.cmr.repository.NodeRef) */ - public List findTaggedNodes(String tag, NodeRef nodeRef) + public List findTaggedNodes(StoreRef storeRef, String tag, NodeRef nodeRef) { // Lower the case of the tag tag = tag.toLowerCase(); - // - // "+PATH:\"/cm:taggable/cm:" + ISOed(tag) + "/member\" +PATH:\"" + pathOfTheNode + "//*\"" + // Get path + Path nodePath = this.nodeService.getPath(nodeRef); + String pathString = nodePath.toPrefixString(this.namespaceService); - // TODO - return null; + // Do query + ResultSet resultSet = this.searchService.query( + storeRef, + SearchService.LANGUAGE_LUCENE, + "+PATH:\"" + pathString + "//*\" +PATH:\"/cm:taggable/cm:" + ISO9075.encode(tag) + "/member\""); + return resultSet.getNodeRefs(); } /** @@ -527,9 +623,9 @@ public class TaggingServiceImpl implements TaggingService bFirst = false; } - result.append(details.getTagName()); + result.append(details.getName()); result.append(TAG_DETAILS_DELIMITER); - result.append(details.getTagCount()); + result.append(details.getCount()); } return result.toString(); diff --git a/source/java/org/alfresco/repo/tagging/TaggingServiceImplTest.java b/source/java/org/alfresco/repo/tagging/TaggingServiceImplTest.java index 2b4be109b0..a98c3b6274 100644 --- a/source/java/org/alfresco/repo/tagging/TaggingServiceImplTest.java +++ b/source/java/org/alfresco/repo/tagging/TaggingServiceImplTest.java @@ -25,6 +25,7 @@ package org.alfresco.repo.tagging; import java.io.Serializable; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -255,7 +256,23 @@ public class TaggingServiceImplTest extends BaseAlfrescoSpringTest assertNotNull(tags); assertEquals(1, tags.size()); assertFalse(tags.contains(TAG_1)); - assertTrue(tags.contains(TAG_2)); + assertTrue(tags.contains(TAG_2)); + + List setTags = new ArrayList(2); + setTags.add(TAG_3); + setTags.add(TAG_1); + this.taggingService.setTags(this.document, setTags); + tags = this.taggingService.getTags(this.document); + assertNotNull(tags); + assertEquals(2, tags.size()); + assertTrue(tags.contains(TAG_1)); + assertFalse(tags.contains(TAG_2)); + assertTrue(tags.contains(TAG_3.toLowerCase())); + + this.taggingService.clearTags(this.document); + tags = this.taggingService.getTags(this.document); + assertNotNull(tags); + assertTrue(tags.isEmpty()); } public void testTagScopeFindAddRemove() @@ -340,15 +357,15 @@ public class TaggingServiceImplTest extends BaseAlfrescoSpringTest ts2 = this.taggingService.findTagScope(this.folder); // check the order and count of the tagscopes - assertEquals(2, ts1.getTags().get(0).getTagCount()); - assertEquals(2, ts1.getTags().get(1).getTagCount()); - assertEquals(1, ts1.getTags().get(2).getTagCount()); - assertEquals(3, ts2.getTags().get(0).getTagCount()); - assertEquals(TAG_2, ts2.getTags().get(0).getTagName()); - assertEquals(2, ts2.getTags().get(1).getTagCount()); - assertEquals(TAG_1, ts2.getTags().get(1).getTagName()); - assertEquals(1, ts2.getTags().get(2).getTagCount()); - assertEquals(TAG_3.toLowerCase(), ts2.getTags().get(2).getTagName()); + assertEquals(2, ts1.getTags().get(0).getCount()); + assertEquals(2, ts1.getTags().get(1).getCount()); + assertEquals(1, ts1.getTags().get(2).getCount()); + assertEquals(3, ts2.getTags().get(0).getCount()); + assertEquals(TAG_2, ts2.getTags().get(0).getName()); + assertEquals(2, ts2.getTags().get(1).getCount()); + assertEquals(TAG_1, ts2.getTags().get(1).getName()); + assertEquals(1, ts2.getTags().get(2).getCount()); + assertEquals(TAG_3.toLowerCase(), ts2.getTags().get(2).getName()); tx.commit(); @@ -410,8 +427,8 @@ public class TaggingServiceImplTest extends BaseAlfrescoSpringTest List tagDetailsList = checkTagScope.getTags(); for (TagDetails tagDetails : tagDetailsList) { - if (tagDetails.getTagName().equals(tag) == true && - tagDetails.getTagCount() == tagCount) + if (tagDetails.getName().equals(tag) == true && + tagDetails.getCount() == tagCount) { bCreated = true; break; @@ -479,9 +496,9 @@ public class TaggingServiceImplTest extends BaseAlfrescoSpringTest List tagDetailsList = checkTagScope.getTags(); for (TagDetails tagDetails : tagDetailsList) { - if (tagDetails.getTagName().equals(tag) == true ) + if (tagDetails.getName().equals(tag) == true ) { - if (tagDetails.getTagCount() == tagCount) + if (tagDetails.getCount() == tagCount) { bRemoved = true; } @@ -525,8 +542,55 @@ public class TaggingServiceImplTest extends BaseAlfrescoSpringTest model.put("subFolder", this.subFolder); model.put("document", this.document); model.put("subDocument", this.subDocument); + model.put("tagScopeTest", false); ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/tagging/script/test_taggingService.js"); this.scriptService.executeScript(location, model); } + + public void testJSTagScope() throws Exception + { + // Add a load of tags to test the global tag methods with + this.taggingService.createTag(storeRef, "alpha"); + this.taggingService.createTag(storeRef, "alpha double"); + this.taggingService.createTag(storeRef, "beta"); + this.taggingService.createTag(storeRef, "gamma"); + this.taggingService.createTag(storeRef, "delta"); + + // Add a load of tags and tag scopes to the object and commit before executing the script + this.taggingService.addTagScope(this.folder); + this.taggingService.addTagScope(this.subFolder); + + // Get the tag scope + TagScope ts1 = this.taggingService.findTagScope(this.subDocument); + TagScope ts2 = this.taggingService.findTagScope(this.folder); + + setComplete(); + endTransaction(); + + addTag(this.subDocument, TAG_1, 1, ts1.getNodeRef()); + addTag(this.subDocument, TAG_2, 1, ts1.getNodeRef()); + addTag(this.subDocument, TAG_3, 1, ts1.getNodeRef()); + addTag(this.subFolder, TAG_1, 2, ts1.getNodeRef()); + addTag(this.subFolder, TAG_2, 2, ts1.getNodeRef()); + addTag(this.document, TAG_1, 3, ts2.getNodeRef()); + addTag(this.document, TAG_2, 3, ts2.getNodeRef()); + addTag(this.folder, TAG_1, 4, ts2.getNodeRef()); + + Map model = new HashMap(0); + model.put("folder", this.folder); + model.put("subFolder", this.subFolder); + model.put("document", this.document); + model.put("subDocument", this.subDocument); + model.put("tagScopeTest", true); + model.put("store", storeRef.toString()); + + UserTransaction tx = this.transactionService.getUserTransaction(); + tx.begin(); + + ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/tagging/script/test_taggingService.js"); + this.scriptService.executeScript(location, model); + + tx.commit(); + } } diff --git a/source/java/org/alfresco/repo/tagging/UpdateTagScopesActionExecuter.java b/source/java/org/alfresco/repo/tagging/UpdateTagScopesActionExecuter.java index 70df26d243..f387e1f49f 100644 --- a/source/java/org/alfresco/repo/tagging/UpdateTagScopesActionExecuter.java +++ b/source/java/org/alfresco/repo/tagging/UpdateTagScopesActionExecuter.java @@ -132,7 +132,7 @@ public class UpdateTagScopesActionExecuter extends ActionExecuterAbstractBase TagDetails currentTag = null; for (TagDetails tag : tags) { - if (tag.getTagName().equals(tagName) == true) + if (tag.getName().equals(tagName) == true) { currentTag = tag; break; @@ -155,7 +155,7 @@ public class UpdateTagScopesActionExecuter extends ActionExecuterAbstractBase { if (currentTag != null) { - int currentTagCount = currentTag.getTagCount(); + int currentTagCount = currentTag.getCount(); if (currentTagCount == 1) { tags.remove(currentTag); diff --git a/source/java/org/alfresco/repo/tagging/script/ScriptTaggingService.java b/source/java/org/alfresco/repo/tagging/script/ScriptTaggingService.java new file mode 100644 index 0000000000..c55c8a3d81 --- /dev/null +++ b/source/java/org/alfresco/repo/tagging/script/ScriptTaggingService.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.tagging.script; + +import java.util.List; + +import org.alfresco.repo.jscript.BaseScopableProcessorExtension; +import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.repository.StoreRef; + + +/** + * Script object representing the tagging service. + * + * @author Roy Wetherall + */ +public class ScriptTaggingService extends BaseScopableProcessorExtension +{ + /** Service Registry */ + private ServiceRegistry serviceRegistry; + + /** + * Sets the Service Registry + * + * @param serviceRegistry + */ + public void setServiceRegistry(ServiceRegistry serviceRegistry) + { + this.serviceRegistry = serviceRegistry; + } + + /** + * Get all the tags available in a store + * + * @param store store reference + * @return String[] tag names + */ + public String[] getTags(String store) + { + StoreRef storeRef = new StoreRef(store); + List result = this.serviceRegistry.getTaggingService().getTags(storeRef); + return (String[])result.toArray(new String[result.size()]); + } + + /** + * Get all the tags available in a store based on a text filter + * + * @param store store reference + * @param filter tag filter + * @return String[] tag names + */ + public String[] getTags(String store, String filter) + { + StoreRef storeRef = new StoreRef(store); + List result = this.serviceRegistry.getTaggingService().getTags(storeRef, filter); + return (String[])result.toArray(new String[result.size()]); + } + +} diff --git a/source/java/org/alfresco/repo/tagging/script/TagScope.java b/source/java/org/alfresco/repo/tagging/script/TagScope.java new file mode 100644 index 0000000000..997bd0a8ee --- /dev/null +++ b/source/java/org/alfresco/repo/tagging/script/TagScope.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2005-2008 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.tagging.script; + +import java.util.List; + +import org.alfresco.service.cmr.tagging.TagDetails; + +/** + * Script object representing a tag scope. + * + * @author Roy Wetherall + */ +public class TagScope +{ + /** Repository tag scope object */ + private org.alfresco.service.cmr.tagging.TagScope tagScopeImpl; + + /** + * Constructor + * + * @param tagScopeImpl repository tag scope object + */ + public TagScope(org.alfresco.service.cmr.tagging.TagScope tagScopeImpl) + { + this.tagScopeImpl = tagScopeImpl; + } + + /** + * Gets all the tags, ordered by count, for the tag scope + * + * @return TagDetails[] tags ordered by count + */ + public TagDetails[] getTags() + { + List tags = tagScopeImpl.getTags(); + return (TagDetails[])tags.toArray(new TagDetails[tags.size()]); + } + + /** + * Gets the top N tags ordered by count + * + * @param topN the number of top tags to return + * @return TagDetails[] the top N tags ordered by count + */ + public TagDetails[] getTopTags(int topN) + { + List tags = tagScopeImpl.getTags(topN); + return (TagDetails[])tags.toArray(new TagDetails[tags.size()]); + } + + /** + * Get the count of a tag, 0 if not present + * + * @param tag tag name + * @return int tag count + */ + public int getCount(String tag) + { + int result = 0; + TagDetails tagDetails = tagScopeImpl.getTag(tag); + if (tagDetails != null) + { + result = tagDetails.getCount(); + } + return result; + } +} diff --git a/source/java/org/alfresco/repo/tagging/script/test_taggingService.js b/source/java/org/alfresco/repo/tagging/script/test_taggingService.js index 71e1270b19..0eb37b70cf 100644 --- a/source/java/org/alfresco/repo/tagging/script/test_taggingService.js +++ b/source/java/org/alfresco/repo/tagging/script/test_taggingService.js @@ -1,3 +1,15 @@ + +function testGlobalTagMethods() +{ + var tags = taggingService.getTags(store); + assertNotNull(tags); + assertEquals(5, tags.length); + + var tags = taggingService.getTags(store, "alpha"); + assertNotNull(tags); + assertEquals(2, tags.length); +} + function testAddRemoveTag() { var tags = document.tags; @@ -18,7 +30,131 @@ function testAddRemoveTag() tags = document.tags; test.assertNotNull(tags); test.assertEquals(2, tags.length); + + document.tags = ["moo", "quack", "squeak"]; + + tags = document.tags; + test.assertNotNull(tags); + test.assertEquals(3, tags.length); + + document.addTags(["woof", "oink"]); + + tags = document.tags; + test.assertNotNull(tags); + test.assertEquals(5, tags.length); + + document.removeTags(["moo", "quack", "oink"]); + + tags = document.tags; + test.assertNotNull(tags); + test.assertEquals(2, tags.length); + + document.clearTags(); + + tags = document.tags; + test.assertNotNull(tags); + test.assertEquals(0, tags.length); } -// Execute test's -testAddRemoveTag(); \ No newline at end of file +function testTagScope() +{ + test.assertFalse(folder.isTagScope); + test.assertFalse(subFolder.isTagScope); + test.assertFalse(subDocument.isTagScope); + + test.assertNull(subDocument.tagScope); + test.assertNull(folder.tagScope); + test.assertNull(subFolder.tagScope); + + subFolder.isTagScope = true; + + test.assertFalse(folder.isTagScope); + test.assertTrue(subFolder.isTagScope); + test.assertFalse(subDocument.isTagScope); + + test.assertNotNull(subDocument.tagScope); + test.assertNull(folder.tagScope); + test.assertNotNull(subFolder.tagScope); + + folder.isTagScope = true; + + test.assertTrue(folder.isTagScope); + test.assertTrue(subFolder.isTagScope); + test.assertFalse(subDocument.isTagScope); + + test.assertNotNull(subDocument.tagScope); + test.assertNotNull(folder.tagScope); + test.assertNotNull(subFolder.tagScope); + + subFolder.isTagScope = false; + + test.assertTrue(folder.isTagScope); + test.assertFalse(subFolder.isTagScope); + test.assertFalse(subDocument.isTagScope); + + test.assertNotNull(subDocument.tagScope); + test.assertNotNull(folder.tagScope); + test.assertNotNull(subFolder.tagScope); +} + +function testTagScopeObject() +{ + var scope = document.tagScope; + test.assertNotNull(scope); + var tags = scope.tags; + test.assertNotNull(tags); + test.assertEquals(3, tags.length); + test.assertEquals("tag one", tags[0].name); + test.assertEquals("tag two", tags[1].name); + test.assertEquals("tag three", tags[2].name); + test.assertEquals(4, tags[0].count); + test.assertEquals(3, tags[1].count); + test.assertEquals(1, tags[2].count); + test.assertEquals(4, scope.getCount("tag one")); + test.assertEquals(3, scope.getCount("tag two")); + test.assertEquals(1, scope.getCount("tag three")); + + tags = scope.getTopTags(2); + test.assertNotNull(tags); + test.assertEquals(2, tags.length); + test.assertEquals("tag one", tags[0].name); + test.assertEquals("tag two", tags[1].name); + test.assertEquals(4, tags[0].count); + test.assertEquals(3, tags[1].count); + test.assertEquals(4, scope.getCount("tag one")); + test.assertEquals(3, scope.getCount("tag two")); +} + +function testFind() +{ + var nodes = search.tagSearch(store, "rubbish tag"); + test.assertNotNull(nodes); + test.assertEquals(0, nodes.length); + + nodes = search.tagSearch(store, "tAg OnE"); + test.assertNotNull(nodes); + test.assertEquals(4, nodes.length); + + nodes = search.tagSearch(store, "tag three"); + test.assertNotNull(nodes); + //test.assertEquals(1, nodes.length); + + nodes = folder.childrenByTags("tag one"); + test.assertNotNull(nodes); + //test.assertEquals(4, nodes.length); + + nodes = subFolder.childrenByTags("tag one"); + test.assertNotNull(nodes); +// test.assertEquals(2, nodes.length); +} + +if (tagScopeTest == true) +{ + testTagScopeObject(); + testFind(); +} +else +{ + testAddRemoveTag(); + testTagScope(); +} \ No newline at end of file diff --git a/source/java/org/alfresco/service/cmr/tagging/TagDetails.java b/source/java/org/alfresco/service/cmr/tagging/TagDetails.java index 0bb8e1c6e9..cab621b9f1 100644 --- a/source/java/org/alfresco/service/cmr/tagging/TagDetails.java +++ b/source/java/org/alfresco/service/cmr/tagging/TagDetails.java @@ -36,12 +36,12 @@ public interface TagDetails extends Comparable * * @return String tag name */ - String getTagName(); + String getName(); /** * Get the tag count * * @return int tag count */ - int getTagCount(); + int getCount(); } diff --git a/source/java/org/alfresco/service/cmr/tagging/TaggingService.java b/source/java/org/alfresco/service/cmr/tagging/TaggingService.java index 463a7a8a7c..b0d3f1e44d 100644 --- a/source/java/org/alfresco/service/cmr/tagging/TaggingService.java +++ b/source/java/org/alfresco/service/cmr/tagging/TaggingService.java @@ -85,6 +85,16 @@ public interface TaggingService */ void addTag(NodeRef nodeRef, String tag); + /** + * Adds a list of tags to a node. + *

+ * Tags are created if they do not exist. + * + * @param nodeRef node reference + * @param tags list of tags + */ + void addTags(NodeRef nodeRef, List tags); + /** * Remove a tag from a node. * @@ -93,6 +103,14 @@ public interface TaggingService */ void removeTag(NodeRef nodeRef, String tag); + /** + * Removes a list of tags from a node. + * + * @param nodeRef node reference + * @param tags list of tags + */ + void removeTags(NodeRef nodeRef, List tags); + /** * Get all the tags on a node * @@ -101,7 +119,21 @@ public interface TaggingService */ List getTags(NodeRef nodeRef); + /** + * Sets the list of tags that are applied to a node, replaces any existing + * tags with those provided. + * + * @param nodeRef node reference + * @param tags list of tags + */ + void setTags(NodeRef nodeRef, List tags); + /** + * Clears all tags from an already tagged node. + * + * @param nodeRef node reference + */ + void clearTags(NodeRef nodeRef); /** * Indicates whether the node reference is a tag scope @@ -158,7 +190,7 @@ public interface TaggingService * @param tag tag name * @return List list of nodes tagged with specified tag, empty of none found */ - List findTaggedNodes(String tag); + List findTaggedNodes(StoreRef storeRef, String tag); /** * Find all nodes that have been tagged with the specified tag and reside within @@ -168,7 +200,7 @@ public interface TaggingService * @param nodeRef node providing context for the search * @return List list of nodes tagged in the context specified, empty if none found */ - List findTaggedNodes(String tag, NodeRef nodeRef); + List findTaggedNodes(StoreRef storeRef, String tag, NodeRef nodeRef); }