From 097482c3eec13abc6cc3e1f6577eb169a6668d16 Mon Sep 17 00:00:00 2001 From: Gavin Cornwell Date: Wed, 13 Jul 2011 14:54:40 +0000 Subject: [PATCH] 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 --- .../script/ScriptAuthorityService.java | 83 +++++++++++++++++++ .../script/ScriptAuthorityServiceTest.java | 28 +++++++ .../authority/script/ScriptGroup.java | 29 +++++++ 3 files changed, 140 insertions(+) diff --git a/source/java/org/alfresco/repo/security/authority/script/ScriptAuthorityService.java b/source/java/org/alfresco/repo/security/authority/script/ScriptAuthorityService.java index fee372974f..d9edfc905e 100644 --- a/source/java/org/alfresco/repo/security/authority/script/ScriptAuthorityService.java +++ b/source/java/org/alfresco/repo/security/authority/script/ScriptAuthorityService.java @@ -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 shortName or displayName 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 shortName, displayName or + * authorityName, 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 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 diff --git a/source/java/org/alfresco/repo/security/authority/script/ScriptAuthorityServiceTest.java b/source/java/org/alfresco/repo/security/authority/script/ScriptAuthorityServiceTest.java index 906ebdf120..5b5928ca48 100644 --- a/source/java/org/alfresco/repo/security/authority/script/ScriptAuthorityServiceTest.java +++ b/source/java/org/alfresco/repo/security/authority/script/ScriptAuthorityServiceTest.java @@ -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 diff --git a/source/java/org/alfresco/repo/security/authority/script/ScriptGroup.java b/source/java/org/alfresco/repo/security/authority/script/ScriptGroup.java index 52b6311e94..d43602dbd0 100644 --- a/source/java/org/alfresco/repo/security/authority/script/ScriptGroup.java +++ b/source/java/org/alfresco/repo/security/authority/script/ScriptGroup.java @@ -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 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 groupNames = pagedGroups.getPage(); + ScriptGroup[] groups = new ScriptGroup[groupNames.size()]; + for (int i=0; i