SVC60: Review the use of findAuthorities() within ScriptAuthorityService (ALF-9247)

ScriptGroup i.e. the "groups" root object now has a getGroups method that behaves similarly to People.getPeople wherein depending on the filter provided determines whether consistent results are returned. Any filter that can be done with a canned query i.e. startsWith queries or "*" will go to the the AuthorityService.getAuthorities method otherwise the lucene based AuthorityService.findAuthorities is used.

The UI also no longer sends a leading *, it it still supported but the user must add it themselves.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28983 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gavin Cornwell
2011-07-13 14:54:40 +00:00
parent 7392670c01
commit 097482c3ee
3 changed files with 140 additions and 0 deletions

View File

@@ -39,6 +39,7 @@ import org.alfresco.service.cmr.security.PersonService.PersonInfo;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.alfresco.util.ScriptPagingDetails;
import org.mozilla.javascript.Scriptable;
/**
* Script object representing the authority service.
@@ -223,6 +224,88 @@ public class ScriptAuthorityService extends BaseScopableProcessorExtension
return makeScriptGroups(authorities, paging, sortBy, serviceRegistry, this.getScope());
}
/**
* Retreives groups matching the given filter from all zones.
*
* NOTE: If the filter is null, an empty string or * all groups found will be returned.
* If the filter starts with * or contains a ? character results returned could be inconsistent.
*
* @param filter Pattern to filter groups by
* @param paging Paging details
* @return Array of mathcing groups
* @since 4.0
*/
public ScriptGroup[] getGroups(String filter, ScriptPagingDetails paging)
{
return getGroupsInZone(filter, null, paging, null);
}
/**
* Retreives groups matching the given filter from all zones.
*
* NOTE: If the filter is null, an empty string or * all groups found will be returned.
* If the filter starts with * or contains a ? character results returned could be inconsistent.
*
* @param filter Pattern to filter groups by
* @param paging Paging details
* @param sortBy Field to sort by, can be <code>shortName</code> or <code>displayName</code> otherwise
* the results are ordered by the authorityName
* @return Array of mathcing groups
* @since 4.0
*/
public ScriptGroup[] getGroups(String filter, ScriptPagingDetails paging, String sortBy)
{
return getGroupsInZone(filter, null, paging, sortBy);
}
/**
* Retreives groups matching the given filter from the given zone.
*
* NOTE: If the filter is null, an empty string or * all groups found will be returned.
* If the filter starts with * or contains a ? character results returned could be inconsistent.
*
* @param filter Pattern to filter groups by
* @param zone The zone in which to search for groups
* @param paging Paging details
* @param sortBy Field to sort by, can be <code>shortName</code>, <code>displayName</code> or
* <code>authorityName</code>, the default is displayName
* @return Array of mathcing groups
* @since 4.0
*/
public ScriptGroup[] getGroupsInZone(String filter, String zone, ScriptPagingDetails paging, String sortBy)
{
// reset filter if necessary
if (filter != null && (filter.length() == 0 || filter.equals("*")))
{
filter = null;
}
if (filter != null && (filter.startsWith("*") || filter.indexOf("?") != -1))
{
// contains and ? wildcard queries are not supported by canned queries so use search
return searchGroupsInZone(filter, zone, paging, sortBy);
}
else
{
try
{
// for backwards compatibility request a total count of found items, once the UI can deal with
// results that do not specify the total number of results this can be removed
paging.setRequestTotalCountMax(10000);
// get the paged results (NOTE: we can only sort by display name currently)
PagingResults<String> groups = authorityService.getAuthorities(AuthorityType.GROUP, zone, filter, true, true, paging);
// create ScriptGroup array from paged results
return makeScriptGroups(groups, paging, serviceRegistry, this.getScope());
}
catch (UnknownAuthorityException e)
{
return new ScriptGroup[] {};
}
}
}
/**
* Get a group given its short name
* @param shortName, the shortName of the group

View File

@@ -225,6 +225,34 @@ public class ScriptAuthorityServiceTest extends TestCase
{
}
public void testGetGroups()
{
// find all the groups that start with "test"
ScriptGroup[] groups = service.getGroupsInZone("test", AuthorityService.ZONE_APP_DEFAULT, new ScriptPagingDetails(10,0), null);
assertEquals(3, groups.length);
// find a certain group
groups = service.getGroupsInZone(GROUP_A, AuthorityService.ZONE_APP_DEFAULT, new ScriptPagingDetails(10,0), null);
assertEquals(1, groups.length);
// make sure a contains query falls back to lucene
groups = service.getGroupsInZone("*Group", AuthorityService.ZONE_APP_DEFAULT, new ScriptPagingDetails(10,0), null);
assertEquals(3, groups.length);
// make sure a ? wildcard query falls back to lucene
groups = service.getGroupsInZone("t?st", AuthorityService.ZONE_APP_DEFAULT, new ScriptPagingDetails(10,0), null);
assertEquals(3, groups.length);
// make sure we support getting all results
groups = service.getGroupsInZone("*", AuthorityService.ZONE_APP_DEFAULT, new ScriptPagingDetails(10,0), null);
assertEquals(5, groups.length);
// ensure paging works, query for all results but just return 1 per page
groups = service.getGroupsInZone("test", AuthorityService.ZONE_APP_DEFAULT, new ScriptPagingDetails(2,2), "displayName");
assertEquals(1, groups.length);
assertEquals(GROUP_C, groups[0].getShortName());
}
public void testFindGroups()
{
// Put one group inside another

View File

@@ -31,6 +31,7 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.alfresco.query.PagingResults;
import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
@@ -611,4 +612,32 @@ public class ScriptGroup implements Authority, Serializable
}
return groups;
}
/**
* Returns an array of ScriptGroup objects representing the given paged results.
*
* @param groups The paged results
* @param paging Object representing the paging details
* @param serviceRegistry
* @param scope
* @return Array of ScriptGroup objects
*
* @since 4.0
*/
public static ScriptGroup[] makeScriptGroups(PagingResults<String> pagedGroups, ScriptPagingDetails paging,
ServiceRegistry serviceRegistry, Scriptable scope)
{
// set the total on the paging object
paging.setTotalItems(pagedGroups);
// retrive the page of results and create a ScriptGroup for each one
List<String> groupNames = pagedGroups.getPage();
ScriptGroup[] groups = new ScriptGroup[groupNames.size()];
for (int i=0; i<groups.length; i++)
{
groups[i] = new ScriptGroup(groupNames.get(i), serviceRegistry, scope);
}
return groups;
}
}