Example usage for javax.persistence Query getMaxResults

List of usage examples for javax.persistence Query getMaxResults

Introduction

In this page you can find the example usage for javax.persistence Query getMaxResults.

Prototype

int getMaxResults();

Source Link

Document

The maximum number of results the query object was set to retrieve.

Usage

From source file:org.apache.ranger.biz.UserMgr.java

/**
 * @param searchCriteria//  w  w w. ja v a  2  s  . c  o m
 * @return
 */
public VXPortalUserList searchUsers(SearchCriteria searchCriteria) {

    VXPortalUserList returnList = new VXPortalUserList();
    ArrayList<VXPortalUser> objectList = new ArrayList<VXPortalUser>();
    String queryStr = "SELECT u FROM  XXPortalUser u ";
    String countQueryStr = "SELECT COUNT(u) FROM XXPortalUser u ";

    // Get total count first
    Query query = createUserSearchQuery(countQueryStr, null, searchCriteria);
    Long count = (Long) query.getSingleResult();
    int resultSize = count != null ? count.intValue() : 0;
    if (resultSize == 0) {
        return returnList;
    }

    // Get actual data

    // Add sort by
    String sortBy = searchCriteria.getSortBy();
    String querySortBy = "u.loginId";
    if (!stringUtil.isEmpty(sortBy)) {
        sortBy = sortBy.trim();
        if (sortBy.equalsIgnoreCase("userId")) {
            querySortBy = "u.id";
        } else if (sortBy.equalsIgnoreCase("loginId")) {
            querySortBy = "ua.loginId";
        } else if (sortBy.equalsIgnoreCase("emailAddress")) {
            querySortBy = "u.emailAddress";
        } else if (sortBy.equalsIgnoreCase("firstName")) {
            querySortBy = "u.firstName";
        } else if (sortBy.equalsIgnoreCase("lastName")) {
            querySortBy = "u.lastName";
        } else {
            sortBy = "loginId";
            logger.error("Invalid sortBy provided. sortBy=" + sortBy);
        }
    } else {
        sortBy = "loginId";
    }

    // Default sort field
    String sortClause = " order by " + querySortBy + " ";

    // Add sort type
    String sortType = searchCriteria.getSortType();
    String querySortType = "asc";
    if (sortType != null) {
        if (sortType.equalsIgnoreCase("asc") || sortType.equalsIgnoreCase("desc")) {
            querySortType = sortType;
        } else {
            logger.error("Invalid sortType. sortType=" + sortType);
        }
    }
    sortClause += querySortType;

    query = createUserSearchQuery(queryStr, sortClause, searchCriteria);

    // Set start index
    query.setFirstResult(searchCriteria.getStartIndex());

    searchUtil.updateQueryPageSize(query, searchCriteria);

    @SuppressWarnings("rawtypes")
    List resultList = query.getResultList();
    // Iterate over the result list and create the return list
    for (Object object : resultList) {
        XXPortalUser gjUser = (XXPortalUser) object;
        VXPortalUser userProfile = new VXPortalUser();
        gjUserToUserProfile(gjUser, userProfile);
        objectList.add(userProfile);
    }

    returnList.setResultSize(resultSize);
    returnList.setPageSize(query.getMaxResults());
    returnList.setSortBy(sortBy);
    returnList.setSortType(querySortType);
    returnList.setStartIndex(query.getFirstResult());
    returnList.setTotalCount(count.longValue());
    returnList.setVXPortalUsers(objectList);
    return returnList;
}

From source file:org.apache.ranger.service.RangerBaseModelService.java

public List<T> searchResources(SearchFilter searchCriteria, List<SearchField> searchFieldList,
        List<SortField> sortFieldList, VList vList) {

    // Get total count of the rows which meet the search criteria
    long count = -1;
    if (searchCriteria.isGetCount()) {
        count = getCountForSearchQuery(searchCriteria, searchFieldList);
        if (count == 0) {
            return Collections.emptyList();
        }/* w ww  .j a v a2 s  .  c  o m*/
    }

    String sortClause = searchUtil.constructSortClause(searchCriteria, sortFieldList);

    String q = queryStr;
    Query query = createQuery(q, sortClause, searchCriteria, searchFieldList, false);

    List<T> resultList = getDao().executeQueryInSecurityContext(tEntityClass, query);

    if (vList != null) {
        vList.setResultSize(resultList.size());
        vList.setPageSize(query.getMaxResults());
        vList.setSortBy(searchCriteria.getSortBy());
        vList.setSortType(searchCriteria.getSortType());
        vList.setStartIndex(query.getFirstResult());
        vList.setTotalCount(count);
    }
    return resultList;
}

From source file:org.apache.ranger.service.RangerBaseModelService.java

protected List<T> searchRangerObjects(SearchFilter searchCriteria, List<SearchField> searchFieldList,
        List<SortField> sortFieldList, PList<V> pList) {

    // Get total count of the rows which meet the search criteria
    long count = -1;
    if (searchCriteria.isGetCount()) {
        count = getCountForSearchQuery(searchCriteria, searchFieldList);
        if (count == 0) {
            return Collections.emptyList();
        }//from  www.  ja  v a2s .c  o  m
    }

    String sortClause = searchUtil.constructSortClause(searchCriteria, sortFieldList);

    String q = queryStr;
    Query query = createQuery(q, sortClause, searchCriteria, searchFieldList, false);

    List<T> resultList = getDao().executeQueryInSecurityContext(tEntityClass, query);

    if (pList != null) {
        pList.setResultSize(resultList.size());
        pList.setPageSize(query.getMaxResults());
        pList.setSortBy(searchCriteria.getSortBy());
        pList.setSortType(searchCriteria.getSortType());
        pList.setStartIndex(query.getFirstResult());
        pList.setTotalCount(count);
    }
    return resultList;
}

From source file:org.apache.ranger.service.RangerPluginInfoService.java

private List<XXPluginInfo> searchRangerObjects(SearchFilter searchCriteria, List<SearchField> searchFieldList,
        List<SortField> sortFieldList, PList<RangerPluginInfo> pList) {

    // Get total count of the rows which meet the search criteria
    long count = -1;
    if (searchCriteria.isGetCount()) {
        count = getCountForSearchQuery(searchCriteria, searchFieldList);
        if (count == 0) {
            return Collections.emptyList();
        }/*from  w w  w. j  a  v a2  s . c o  m*/
    }

    String sortClause = searchUtil.constructSortClause(searchCriteria, sortFieldList);

    String queryStr = "SELECT obj FROM " + XXPluginInfo.class.getName() + " obj ";
    Query query = createQuery(queryStr, sortClause, searchCriteria, searchFieldList, false);

    List<XXPluginInfo> resultList = daoManager.getXXPluginInfo()
            .executeQueryInSecurityContext(XXPluginInfo.class, query);

    if (pList != null) {
        pList.setResultSize(resultList.size());
        pList.setPageSize(query.getMaxResults());
        pList.setSortBy(searchCriteria.getSortBy());
        pList.setSortType(searchCriteria.getSortType());
        pList.setStartIndex(query.getFirstResult());
        pList.setTotalCount(count);
    }
    return resultList;
}