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
This commit is contained in:
Roy Wetherall
2008-07-08 16:58:56 +00:00
parent 0d61ec6542
commit 8f1c38fcf7
13 changed files with 660 additions and 44 deletions

View File

@@ -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.
* <p>
* 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<NodeRef> 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

View File

@@ -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<NodeRef> 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

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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<String> getTags(StoreRef storeRef, String filter)
{
// TODO
return null;
Collection<ChildAssociationRef> rootCategories = this.categoryService.getRootCategories(storeRef, ContentModel.ASPECT_TAGGABLE);
List<String> result = new ArrayList<String>(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<String> tags)
{
for (String tag : tags)
{
addTag(nodeRef, tag);
}
}
/**
* Gets the node reference for a given tag.
* <p>
@@ -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<String> 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<String> tags)
{
List<NodeRef> 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<NodeRef> findTaggedNodes(String tag)
public List<NodeRef> 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<NodeRef> findTaggedNodes(String tag, NodeRef nodeRef)
public List<NodeRef> 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();

View File

@@ -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<String> setTags = new ArrayList<String>(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<TagDetails> 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<TagDetails> 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<String, Object>(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();
}
}

View File

@@ -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);

View File

@@ -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<String> 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<String> result = this.serviceRegistry.getTaggingService().getTags(storeRef, filter);
return (String[])result.toArray(new String[result.size()]);
}
}

View File

@@ -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<TagDetails> 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<TagDetails> 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;
}
}

View File

@@ -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();
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();
}

View File

@@ -36,12 +36,12 @@ public interface TagDetails extends Comparable<TagDetails>
*
* @return String tag name
*/
String getTagName();
String getName();
/**
* Get the tag count
*
* @return int tag count
*/
int getTagCount();
int getCount();
}

View File

@@ -85,6 +85,16 @@ public interface TaggingService
*/
void addTag(NodeRef nodeRef, String tag);
/**
* Adds a list of tags to a node.
* <p>
* Tags are created if they do not exist.
*
* @param nodeRef node reference
* @param tags list of tags
*/
void addTags(NodeRef nodeRef, List<String> 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<String> tags);
/**
* Get all the tags on a node
*
@@ -101,7 +119,21 @@ public interface TaggingService
*/
List<String> 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<String> 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<NodeRef> list of nodes tagged with specified tag, empty of none found
*/
List<NodeRef> findTaggedNodes(String tag);
List<NodeRef> 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<NodeRef> list of nodes tagged in the context specified, empty if none found
*/
List<NodeRef> findTaggedNodes(String tag, NodeRef nodeRef);
List<NodeRef> findTaggedNodes(StoreRef storeRef, String tag, NodeRef nodeRef);
}