Search in sources :

Example 1 with PagingRequest

use of org.alfresco.query.PagingRequest in project acs-community-packaging by Alfresco.

the class BaseAssociationEditor method getAvailableOptions.

/**
 * Retrieves the available options for the current association
 *
 * @param context Faces Context
 * @param contains The contains part of the query
 */
protected void getAvailableOptions(FacesContext context, String contains) {
    AssociationDefinition assocDef = getAssociationDefinition(context);
    if (assocDef != null) {
        // find and show all the available options for the current association
        String type = assocDef.getTargetClass().getName().toString();
        if (type.equals(ContentModel.TYPE_AUTHORITY_CONTAINER.toString())) {
            UserTransaction tx = null;
            try {
                tx = Repository.getUserTransaction(context, true);
                tx.begin();
                String safeContains = null;
                if (contains != null && contains.length() > 0) {
                    safeContains = Utils.remove(contains.trim(), "\"");
                    safeContains = safeContains.toLowerCase();
                }
                // get all available groups
                AuthorityService authorityService = Repository.getServiceRegistry(context).getAuthorityService();
                Set<String> groups = authorityService.getAllAuthoritiesInZone(AuthorityService.ZONE_APP_DEFAULT, AuthorityType.GROUP);
                this.availableOptions = new ArrayList<NodeRef>(groups.size());
                // get the NodeRef for each matching group
                AuthorityDAO authorityDAO = (AuthorityDAO) FacesContextUtils.getRequiredWebApplicationContext(context).getBean("authorityDAO");
                if (authorityDAO != null) {
                    List<String> matchingGroups = new ArrayList<String>();
                    String groupDisplayName;
                    for (String group : groups) {
                        // get display name, if not present strip prefix from group id
                        groupDisplayName = authorityService.getAuthorityDisplayName(group);
                        if (groupDisplayName == null || groupDisplayName.length() == 0) {
                            groupDisplayName = group.substring(PermissionService.GROUP_PREFIX.length());
                        }
                        // otherwise just add the group name to the sorted set
                        if (safeContains != null) {
                            if (groupDisplayName.toLowerCase().indexOf(safeContains) != -1) {
                                matchingGroups.add(group);
                            }
                        } else {
                            matchingGroups.add(group);
                        }
                    }
                    // sort the group names
                    Collections.sort(matchingGroups, new SimpleStringComparator());
                    // go through the sorted set and get the NodeRef for each group
                    for (String groupName : matchingGroups) {
                        NodeRef groupRef = authorityDAO.getAuthorityNodeRefOrNull(groupName);
                        if (groupRef != null) {
                            this.availableOptions.add(groupRef);
                        }
                    }
                }
                // commit the transaction
                tx.commit();
            } catch (Throwable err) {
                Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_GENERIC), err.getMessage()), err);
                this.availableOptions = Collections.<NodeRef>emptyList();
                try {
                    if (tx != null) {
                        tx.rollback();
                    }
                } catch (Exception tex) {
                }
            }
        } else if (type.equals(ContentModel.TYPE_PERSON.toString())) {
            List<Pair<QName, String>> filter = (contains != null && contains.trim().length() > 0) ? Utils.generatePersonFilter(contains.trim()) : null;
            // Always sort by last name, then first name
            List<Pair<QName, Boolean>> sort = new ArrayList<Pair<QName, Boolean>>();
            sort.add(new Pair<QName, Boolean>(ContentModel.PROP_LASTNAME, true));
            sort.add(new Pair<QName, Boolean>(ContentModel.PROP_FIRSTNAME, true));
            // Log the filtering
            if (logger.isDebugEnabled())
                logger.debug("Query filter: " + filter);
            // How many to limit too?
            int maxResults = Application.getClientConfig(context).getSelectorsSearchMaxResults();
            if (maxResults <= 0) {
                maxResults = Utils.getPersonMaxResults();
            }
            List<PersonInfo> persons = Repository.getServiceRegistry(context).getPersonService().getPeople(filter, true, sort, new PagingRequest(maxResults, null)).getPage();
            // Save the results
            List<NodeRef> nodes = new ArrayList<NodeRef>(persons.size());
            for (PersonInfo person : persons) {
                nodes.add(person.getNodeRef());
            }
            this.availableOptions = nodes;
        } else {
            // for all other types/aspects perform a lucene search
            StringBuilder query = new StringBuilder("+TYPE:\"");
            if (assocDef.getTargetClass().isAspect()) {
                query = new StringBuilder("+ASPECT:\"");
            } else {
                query = new StringBuilder("+TYPE:\"");
            }
            query.append(type);
            query.append("\"");
            if (contains != null && contains.trim().length() != 0) {
                String safeContains = null;
                if (contains != null && contains.length() > 0) {
                    safeContains = Utils.remove(contains.trim(), "\"");
                    safeContains = safeContains.toLowerCase();
                }
                query.append(" AND +@");
                String nameAttr = Repository.escapeQName(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "name"));
                query.append(nameAttr);
                query.append(":\"*" + safeContains + "*\"");
            }
            int maxResults = Application.getClientConfig(context).getSelectorsSearchMaxResults();
            if (logger.isDebugEnabled()) {
                logger.debug("Query: " + query.toString());
                logger.debug("Max results size: " + maxResults);
            }
            SearchParameters searchParams = new SearchParameters();
            searchParams.addStore(Repository.getStoreRef());
            searchParams.setLanguage(SearchService.LANGUAGE_LUCENE);
            searchParams.setQuery(query.toString());
            if (maxResults > 0) {
                searchParams.setLimit(maxResults);
                searchParams.setLimitBy(LimitBy.FINAL_SIZE);
            }
            ResultSet results = null;
            try {
                results = Repository.getServiceRegistry(context).getSearchService().query(searchParams);
                this.availableOptions = results.getNodeRefs();
            } catch (SearcherException se) {
                logger.info("Search failed for: " + query, se);
                Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_QUERY));
            } finally {
                if (results != null) {
                    results.close();
                }
            }
        }
        if (logger.isDebugEnabled())
            logger.debug("Found " + this.availableOptions.size() + " available options");
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) QName(org.alfresco.service.namespace.QName) AuthorityService(org.alfresco.service.cmr.security.AuthorityService) ArrayList(java.util.ArrayList) AuthorityDAO(org.alfresco.repo.security.authority.AuthorityDAO) SearcherException(org.alfresco.repo.search.SearcherException) AbortProcessingException(javax.faces.event.AbortProcessingException) IOException(java.io.IOException) PagingRequest(org.alfresco.query.PagingRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) SearcherException(org.alfresco.repo.search.SearcherException) ResultSet(org.alfresco.service.cmr.search.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.alfresco.util.Pair)

Example 2 with PagingRequest

use of org.alfresco.query.PagingRequest in project acs-community-packaging by Alfresco.

the class BaseInviteUsersWizard method getGroups.

protected Set<String> getGroups(String search, boolean includeEveryone) {
    Set<String> groups;
    if (search != null && search.startsWith("*")) {
        // if the search term starts with a wildcard use Lucene based search to find groups (results will be inconsistent)
        String term = search.trim() + "*";
        groups = getAuthorityService().findAuthorities(AuthorityType.GROUP, null, false, term, AuthorityService.ZONE_APP_DEFAULT);
    } else {
        // all other searches use the canned query so search results are consistent
        PagingResults<String> pagedResults = getAuthorityService().getAuthorities(AuthorityType.GROUP, AuthorityService.ZONE_APP_DEFAULT, search, true, true, new PagingRequest(10000));
        groups = new LinkedHashSet<String>(pagedResults.getPage());
    }
    if (includeEveryone) {
        // add the EVERYONE group to the results
        groups.addAll(getAuthorityService().getAllAuthorities(AuthorityType.EVERYONE));
    }
    return groups;
}
Also used : PagingRequest(org.alfresco.query.PagingRequest)

Example 3 with PagingRequest

use of org.alfresco.query.PagingRequest in project acs-community-packaging by Alfresco.

the class GroupsDialog method displayGroups.

/**
 * Searches for groups or lists groups of the current group context
 */
private void displayGroups() {
    // empty the list before we begin the search for a new list
    this.groupsRichList.setValue(null);
    List<String> authorities;
    if (this.group == null) {
        // Use the search criteria if we are not searching for everything
        String search = null;
        if (!this.searchAll) {
            if (this.groupsSearchCriteria == null) {
                search = null;
            } else {
                search = groupsSearchCriteria.trim();
                if (search.length() == 0) {
                    search = null;
                } else {
                    // Let's make it search on the short name/display name prefix
                    search = search + "*";
                }
            }
        }
        if (!this.searchAll && search == null) {
            // Do not allow empty searches
            this.groups = Collections.<Map<String, String>>emptyList();
            return;
        } else {
            boolean immediate = (this.filterMode.equals(FILTER_CHILDREN));
            if ((search != null && search.startsWith("*")) || immediate) {
                // if the search term starts with a wildcard or is for immediate children only then use Solr/Lucene based search query to find groups (note:
                // results will be eventually consistent if using Solr, rather than embedded Lucene)
                Set<String> results = this.authService.findAuthorities(AuthorityType.GROUP, this.group, immediate, search, AuthorityService.ZONE_APP_DEFAULT);
                authorities = new ArrayList<String>(results);
            } else {
                // all other searches use the canned query so search results are consistent
                PagingResults<String> pagedResults = this.authService.getAuthorities(AuthorityType.GROUP, AuthorityService.ZONE_APP_DEFAULT, search, true, true, new PagingRequest(10000));
                authorities = pagedResults.getPage();
            }
        }
    } else {
        // child groups of the current group
        Set<String> results = this.getAuthorityService().getContainedAuthorities(AuthorityType.GROUP, this.group, true);
        authorities = new ArrayList<String>(results);
    }
    this.groups = new ArrayList<Map<String, String>>(authorities.size());
    for (String authority : authorities) {
        Map<String, String> authMap = new HashMap<String, String>(8);
        String name = this.authService.getAuthorityDisplayName(authority);
        if (name == null) {
            name = this.authService.getShortName(name);
        }
        authMap.put("name", name);
        authMap.put("id", authority);
        authMap.put("group", authority);
        authMap.put("groupName", name);
        this.groups.add(authMap);
    }
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) PagingRequest(org.alfresco.query.PagingRequest)

Example 4 with PagingRequest

use of org.alfresco.query.PagingRequest in project records-management by Alfresco.

the class ExtendedSecurityServiceImpl method findIPRGroup.

/**
 * Given a group name prefix and the authorities, finds the exact match existing group.
 * <p>
 * If the group does not exist then the group returned is null and the index shows the next available
 * group index for creation.
 *
 * @param groupPrefix             group name prefix
 * @param authorities             authorities
 * @return Pair<String, Integer>  where first is the name of the found group, null if none found and second
 *                                if the next available create index
 */
private Pair<String, Integer> findIPRGroup(String groupPrefix, Set<String> authorities) {
    String iprGroup = null;
    int nextGroupIndex = 0;
    boolean hasMoreItems = true;
    int pageCount = 0;
    // determine the short name prefix
    String groupShortNamePrefix = getIPRGroupPrefixShortName(groupPrefix, authorities);
    // iterate over the authorities to find a match
    while (hasMoreItems == true) {
        // get matching authorities
        PagingResults<String> results = authorityService.getAuthorities(AuthorityType.GROUP, RMAuthority.ZONE_APP_RM, groupShortNamePrefix, false, false, new PagingRequest(MAX_ITEMS * pageCount, MAX_ITEMS));
        // record the total count
        nextGroupIndex = nextGroupIndex + results.getPage().size();
        // see if any of the matching groups exactly match
        for (String group : results.getPage()) {
            // if exists and matches we have found our group
            if (isIPRGroupTrueMatch(group, authorities)) {
                iprGroup = group;
                break;
            }
        }
        // determine if there are any more pages to inspect
        hasMoreItems = results.hasMoreItems();
        pageCount++;
    }
    return new Pair<String, Integer>(iprGroup, nextGroupIndex);
}
Also used : PagingRequest(org.alfresco.query.PagingRequest) Pair(org.alfresco.util.Pair)

Example 5 with PagingRequest

use of org.alfresco.query.PagingRequest in project alfresco-remote-api by Alfresco.

the class PublicApiAlfrescoCmisService method getRepositoryInfos.

@Override
public List<RepositoryInfo> getRepositoryInfos(ExtensionsData extension) {
    // for currently authenticated user
    PagingResults<Network> networks = networksService.getNetworks(new PagingRequest(0, Integer.MAX_VALUE));
    List<Network> page = networks.getPage();
    final List<RepositoryInfo> repoInfos = new ArrayList<RepositoryInfo>(page.size() + 1);
    for (Network network : page) {
        repoInfos.add(getRepositoryInfo(network));
    }
    return repoInfos;
}
Also used : RepositoryInfo(org.apache.chemistry.opencmis.commons.data.RepositoryInfo) Network(org.alfresco.repo.tenant.Network) ArrayList(java.util.ArrayList) PagingRequest(org.alfresco.query.PagingRequest)

Aggregations

PagingRequest (org.alfresco.query.PagingRequest)38 ArrayList (java.util.ArrayList)20 Pair (org.alfresco.util.Pair)14 HashMap (java.util.HashMap)13 NodeRef (org.alfresco.service.cmr.repository.NodeRef)13 Map (java.util.Map)10 Paging (org.alfresco.rest.framework.resource.parameters.Paging)10 AbstractList (java.util.AbstractList)8 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)8 JSONObject (org.json.simple.JSONObject)8 HashSet (java.util.HashSet)6 List (java.util.List)6 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)6 SortColumn (org.alfresco.rest.framework.resource.parameters.SortColumn)6 Query (org.alfresco.rest.framework.resource.parameters.where.Query)6 MapBasedQueryWalker (org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker)6 Date (java.util.Date)5 Set (java.util.Set)5 FacesContext (javax.faces.context.FacesContext)5 UserTransaction (javax.transaction.UserTransaction)5