Search in sources :

Example 26 with PagingRequest

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

the class BaseReassignDialog method pickerCallback.

/**
 * Query callback method executed by the Generic Picker component.
 * This method is part of the contract to the Generic Picker, it is up to the backing bean
 * to execute whatever query is appropriate and return the results.
 *
 * @param filterIndex        Index of the filter drop-down selection
 * @param contains           Text from the contains textbox
 *
 * @return An array of SelectItem objects containing the results to display in the picker.
 */
public SelectItem[] pickerCallback(int filterIndex, String contains) {
    FacesContext context = FacesContext.getCurrentInstance();
    // quick exit if not enough characters entered for a search
    String search = contains.trim();
    int searchMin = Application.getClientConfig(context).getPickerSearchMinimum();
    if (search.length() < searchMin) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, MSG_SEARCH_MINIMUM), searchMin));
        return new SelectItem[0];
    }
    SelectItem[] items;
    UserTransaction tx = null;
    ResultSet resultSet = null;
    try {
        tx = Repository.getUserTransaction(context, true);
        tx.begin();
        int maxResults = Application.getClientConfig(context).getInviteUsersMaxResults();
        if (maxResults <= 0) {
            maxResults = Utils.getPersonMaxResults();
        }
        List<PersonInfo> persons = getPersonService().getPeople(Utils.generatePersonFilter(contains.trim()), true, Utils.generatePersonSort(), new PagingRequest(maxResults, null)).getPage();
        ArrayList<SelectItem> itemList = new ArrayList<SelectItem>(persons.size());
        for (PersonInfo person : persons) {
            String username = person.getUserName();
            if (AuthenticationUtil.getGuestUserName().equals(username) == false) {
                String firstName = person.getFirstName();
                String lastName = person.getLastName();
                String name = (firstName != null ? firstName : "") + ' ' + (lastName != null ? lastName : "");
                SelectItem item = new SortableSelectItem(username, name + " [" + username + "]", lastName != null ? lastName : username);
                itemList.add(item);
            }
        }
        items = new SelectItem[itemList.size()];
        itemList.toArray(items);
        Arrays.sort(items);
        // commit the transaction
        tx.commit();
    } catch (Throwable err) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
        items = new SelectItem[0];
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }
    }
    return items;
}
Also used : UserTransaction(javax.transaction.UserTransaction) FacesContext(javax.faces.context.FacesContext) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) ArrayList(java.util.ArrayList) SortableSelectItem(org.alfresco.web.ui.common.SortableSelectItem) PagingRequest(org.alfresco.query.PagingRequest) SortableSelectItem(org.alfresco.web.ui.common.SortableSelectItem) SelectItem(javax.faces.model.SelectItem) ResultSet(org.alfresco.service.cmr.search.ResultSet)

Example 27 with PagingRequest

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

the class DeleteUserDialog method search.

public String search() {
    if (this.searchCriteria == null || this.searchCriteria.length() == 0) {
        this.users = Collections.<Node>emptyList();
    } else {
        FacesContext context = FacesContext.getCurrentInstance();
        UserTransaction tx = null;
        try {
            tx = Repository.getUserTransaction(context, true);
            tx.begin();
            // define the query to find people by their first or last name
            String search = ISO9075.encode(this.searchCriteria);
            List<Pair<QName, String>> filter = Utils.generatePersonFilter(search);
            if (logger.isDebugEnabled()) {
                logger.debug("Query filter: " + filter);
            }
            List<PersonInfo> persons = getPersonService().getPeople(filter, true, Utils.generatePersonSort(), new PagingRequest(Utils.getPersonMaxResults(), null)).getPage();
            if (logger.isDebugEnabled()) {
                logger.debug("Found " + persons.size() + " users");
            }
            this.users = new ArrayList<Node>(persons.size());
            for (PersonInfo person : persons) {
                // create our Node representation
                MapNode node = new MapNode(person.getNodeRef());
                // set data binding properties
                // this will also force initialisation of the props now during the UserTransaction
                // it is much better for performance to do this now rather than during page bind
                Map<String, Object> props = node.getProperties();
                props.put("fullName", ((String) props.get("firstName")) + ' ' + ((String) props.get("lastName")));
                NodeRef homeFolderNodeRef = (NodeRef) props.get("homeFolder");
                if (homeFolderNodeRef != null) {
                    props.put("homeSpace", homeFolderNodeRef);
                }
                this.users.add(node);
            }
            // commit the transaction
            tx.commit();
        } catch (InvalidNodeRefException refErr) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_NODEREF), new Object[] { "root" }));
            this.users = Collections.<Node>emptyList();
            try {
                if (tx != null) {
                    tx.rollback();
                }
            } catch (Exception tex) {
            }
        } catch (Exception err) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_GENERIC), err.getMessage()), err);
            this.users = Collections.<Node>emptyList();
            try {
                if (tx != null) {
                    tx.rollback();
                }
            } catch (Exception tex) {
            }
        }
    }
    // return null to stay on the same page
    return null;
}
Also used : UserTransaction(javax.transaction.UserTransaction) FacesContext(javax.faces.context.FacesContext) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) Node(org.alfresco.web.bean.repository.Node) MapNode(org.alfresco.web.bean.repository.MapNode) MapNode(org.alfresco.web.bean.repository.MapNode) PagingRequest(org.alfresco.query.PagingRequest) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) ReportedException(org.alfresco.web.ui.common.ReportedException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) Pair(org.alfresco.util.Pair)

Example 28 with PagingRequest

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

the class AddUsersDialog method pickerCallback.

// ------------------------------------------------------------------------------
// Helpers
/**
 * Query callback method executed by the Generic Picker component. This
 * method is part of the contract to the Generic Picker, it is up to the
 * backing bean to execute whatever query is appropriate and return the
 * results.
 *
 * @param filterIndex Index of the filter drop-down selection
 * @param contains Text from the contains textbox
 * @return An array of SelectItem objects containing the results to display
 *         in the picker.
 */
public SelectItem[] pickerCallback(int filterIndex, final String contains) {
    final FacesContext context = FacesContext.getCurrentInstance();
    UserTransaction tx = null;
    try {
        // getUserTransaction(context);
        RetryingTransactionHelper txHelper = Repository.getRetryingTransactionHelper(context);
        return txHelper.doInTransaction(new RetryingTransactionCallback<SelectItem[]>() {

            public SelectItem[] execute() throws Exception {
                SelectItem[] items = new SelectItem[0];
                // Use the Person Service to retrieve user details
                String term = contains.trim();
                if (term.length() != 0) {
                    List<PersonInfo> persons = getPersonService().getPeople(Utils.generatePersonFilter(contains.trim()), true, Utils.generatePersonSort(), new PagingRequest(Utils.getPersonMaxResults(), null)).getPage();
                    ArrayList<SelectItem> itemList = new ArrayList<SelectItem>(persons.size());
                    for (PersonInfo person : persons) {
                        String username = person.getUserName();
                        if (AuthenticationUtil.getGuestUserName().equals(username) == false) {
                            String firstName = person.getFirstName();
                            String lastName = person.getLastName();
                            // build a sensible label for display
                            String name = (firstName != null ? firstName : "") + ' ' + (lastName != null ? lastName : "");
                            SelectItem item = new SortableSelectItem(username, name + " [" + username + "]", lastName != null ? lastName : username);
                            itemList.add(item);
                        }
                    }
                    items = new SelectItem[itemList.size()];
                    itemList.toArray(items);
                }
                return items;
            }
        });
    } catch (BooleanQuery.TooManyClauses clauses) {
        Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), "too_many_users"));
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
        return new SelectItem[0];
    } catch (Exception err) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
        return new SelectItem[0];
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) FacesContext(javax.faces.context.FacesContext) BooleanQuery(org.apache.lucene.search.BooleanQuery) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ArrayList(java.util.ArrayList) SortableSelectItem(org.alfresco.web.ui.common.SortableSelectItem) PagingRequest(org.alfresco.query.PagingRequest) SortableSelectItem(org.alfresco.web.ui.common.SortableSelectItem) SelectItem(javax.faces.model.SelectItem) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with PagingRequest

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

the class GroupsImpl method getAuthoritiesInfo.

private PagingResults<AuthorityInfo> getAuthoritiesInfo(AuthorityType authorityType, Boolean isRootParam, String zoneFilter, Set<String> rootAuthorities, Pair<String, Boolean> sortProp, Paging paging) {
    PagingResults<AuthorityInfo> pagingResult;
    if (isRootParam != null) {
        List<AuthorityInfo> groupList;
        if (isRootParam) {
            // Limit the post processing work by using the already loaded
            // list of root authorities.
            List<AuthorityInfo> authorities = rootAuthorities.stream().map(this::getAuthorityInfo).filter(auth -> zonePredicate(auth.getAuthorityName(), zoneFilter)).collect(Collectors.toList());
            groupList = new ArrayList<>(rootAuthorities.size());
            groupList.addAll(authorities);
            // Post process sorting - this should be moved to service
            // layer. It is done here because sorting is not supported at
            // service layer.
            AuthorityInfoComparator authorityComparator = new AuthorityInfoComparator(sortProp.getFirst(), sortProp.getSecond());
            Collections.sort(groupList, authorityComparator);
        } else {
            PagingRequest pagingNoMaxItems = new PagingRequest(CannedQueryPageDetails.DEFAULT_PAGE_SIZE);
            // Get authorities using canned query but without using
            // the requested paginating now because we need to filter out
            // the root authorities.
            PagingResults<AuthorityInfo> nonPagingResult = authorityService.getAuthoritiesInfo(authorityType, zoneFilter, null, sortProp.getFirst(), sortProp.getSecond(), pagingNoMaxItems);
            // Post process filtering - this should be moved to service
            // layer. It is done here because filtering by "isRoot" is not
            // supported at service layer.
            groupList = nonPagingResult.getPage();
            if (groupList != null) {
                for (Iterator<AuthorityInfo> i = groupList.iterator(); i.hasNext(); ) {
                    AuthorityInfo authorityInfo = i.next();
                    if (!isRootParam.equals(isRootAuthority(rootAuthorities, authorityInfo.getAuthorityName()))) {
                        i.remove();
                    }
                }
            }
        }
        // Post process paging - this should be moved to service layer.
        pagingResult = Util.wrapPagingResults(paging, groupList);
    } else {
        PagingRequest pagingRequest = Util.getPagingRequest(paging);
        // Get authorities using canned query.
        pagingResult = authorityService.getAuthoritiesInfo(authorityType, zoneFilter, null, sortProp.getFirst(), sortProp.getSecond(), pagingRequest);
    }
    return pagingResult;
}
Also used : CannedQueryPageDetails(org.alfresco.query.CannedQueryPageDetails) Arrays(java.util.Arrays) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) MapBasedQueryWalkerOrSupported(org.alfresco.rest.workflow.api.impl.MapBasedQueryWalkerOrSupported) Query(org.alfresco.rest.framework.resource.parameters.where.Query) AuthorityService(org.alfresco.service.cmr.security.AuthorityService) Paging(org.alfresco.rest.framework.resource.parameters.Paging) AuthenticationUtil.runAsSystem(org.alfresco.repo.security.authentication.AuthenticationUtil.runAsSystem) AbstractList(java.util.AbstractList) HashMap(java.util.HashMap) PagingRequest(org.alfresco.query.PagingRequest) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WhereClauseParser(org.alfresco.rest.antlr.WhereClauseParser) GroupMember(org.alfresco.rest.api.model.GroupMember) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) PagingResults(org.alfresco.query.PagingResults) PermissionService(org.alfresco.service.cmr.security.PermissionService) People(org.alfresco.rest.api.People) Map(java.util.Map) QueryHelper(org.alfresco.rest.framework.resource.parameters.where.QueryHelper) AuthorityDAO(org.alfresco.repo.security.authority.AuthorityDAO) Group(org.alfresco.rest.api.model.Group) Collator(java.text.Collator) UnknownAuthorityException(org.alfresco.repo.security.authority.UnknownAuthorityException) AuthorityType(org.alfresco.service.cmr.security.AuthorityType) Iterator(java.util.Iterator) SortColumn(org.alfresco.rest.framework.resource.parameters.SortColumn) Set(java.util.Set) Pair(org.alfresco.util.Pair) AuthorityInfo(org.alfresco.repo.security.authority.AuthorityInfo) Collectors(java.util.stream.Collectors) EmptyPagingResults(org.alfresco.query.EmptyPagingResults) AlfrescoCollator(org.alfresco.util.AlfrescoCollator) List(java.util.List) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) MapBasedQueryWalker(org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker) I18NUtil(org.springframework.extensions.surf.util.I18NUtil) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) Groups(org.alfresco.rest.api.Groups) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) Comparator(java.util.Comparator) Collections(java.util.Collections) AuthorityException(org.alfresco.repo.security.authority.AuthorityException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Parameters(org.alfresco.rest.framework.resource.parameters.Parameters) AuthorityInfo(org.alfresco.repo.security.authority.AuthorityInfo) PagingRequest(org.alfresco.query.PagingRequest)

Example 30 with PagingRequest

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

the class PeopleImpl method getPeople.

@Override
public CollectionWithPagingInfo<Person> getPeople(final Parameters parameters) {
    Paging paging = parameters.getPaging();
    PagingRequest pagingRequest = Util.getPagingRequest(paging);
    List<Pair<QName, Boolean>> sortProps = getSortProps(parameters);
    // For now the results are not filtered
    // please see REPO-555
    final PagingResults<PersonService.PersonInfo> pagingResult = personService.getPeople(null, null, sortProps, pagingRequest);
    final List<PersonService.PersonInfo> page = pagingResult.getPage();
    int totalItems = pagingResult.getTotalResultCount().getFirst();
    final String personId = AuthenticationUtil.getFullyAuthenticatedUser();
    List<Person> people = new AbstractList<Person>() {

        @Override
        public Person get(int index) {
            PersonService.PersonInfo personInfo = page.get(index);
            Person person = getPersonWithProperties(personInfo.getUserName(), parameters.getInclude());
            return person;
        }

        @Override
        public int size() {
            return page.size();
        }
    };
    return CollectionWithPagingInfo.asPaged(paging, people, pagingResult.hasMoreItems(), totalItems);
}
Also used : AbstractList(java.util.AbstractList) Paging(org.alfresco.rest.framework.resource.parameters.Paging) PagingRequest(org.alfresco.query.PagingRequest) Person(org.alfresco.rest.api.model.Person) Pair(org.alfresco.util.Pair)

Aggregations

PagingRequest (org.alfresco.query.PagingRequest)36 ArrayList (java.util.ArrayList)18 Pair (org.alfresco.util.Pair)12 NodeRef (org.alfresco.service.cmr.repository.NodeRef)11 HashMap (java.util.HashMap)10 JSONObject (org.json.simple.JSONObject)10 Paging (org.alfresco.rest.framework.resource.parameters.Paging)8 Map (java.util.Map)7 Date (java.util.Date)6 UserTransaction (javax.transaction.UserTransaction)6 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)6 AbstractList (java.util.AbstractList)5 FacesContext (javax.faces.context.FacesContext)5 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)5 TopicInfo (org.alfresco.service.cmr.discussion.TopicInfo)5 PersonInfo (org.alfresco.service.cmr.security.PersonService.PersonInfo)5 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)4 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)4 HashSet (java.util.HashSet)3 List (java.util.List)3