Example usage for javax.persistence TypedQuery setMaxResults

List of usage examples for javax.persistence TypedQuery setMaxResults

Introduction

In this page you can find the example usage for javax.persistence TypedQuery setMaxResults.

Prototype

TypedQuery<X> setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:org.apache.openmeetings.data.user.UserManager.java

public List<User> searchUserProfile(String searchTxt, String userOffers, String userSearchs, String orderBy,
        int start, int max, boolean asc) {
    try {//from   w w w .  jav  a 2  s . c  o m

        String hql = "select c from User c " + "where c.deleted = false ";

        if (searchTxt.length() != 0 && userOffers.length() != 0 && userSearchs.length() != 0) {

            hql += "AND " + "(" + "(" + "lower(c.login) LIKE :search " + "OR lower(c.firstname) LIKE :search "
                    + "OR lower(c.lastname) LIKE :search " + "OR lower(c.adresses.email) LIKE :search "
                    + "OR lower(c.adresses.town) LIKE :search " + ")" + "AND" + "("
                    + "lower(c.userOffers) LIKE :userOffers " + ")" + "AND" + "("
                    + "lower(c.userSearchs) LIKE :userSearchs " + ")" + ")";

        } else if (searchTxt.length() != 0 && userOffers.length() != 0) {

            hql += "AND " + "(" + "(" + "lower(c.login) LIKE :search " + "OR lower(c.firstname) LIKE :search "
                    + "OR lower(c.lastname) LIKE :search " + "OR lower(c.adresses.email) LIKE :search "
                    + "OR lower(c.adresses.town) LIKE :search " + ")" + "AND" + "("
                    + "lower(c.userOffers) LIKE :userOffers " + ")" + ")";

        } else if (searchTxt.length() != 0 && userSearchs.length() != 0) {

            hql += "AND " + "(" + "(" + "lower(c.login) LIKE :search " + "OR lower(c.firstname) LIKE :search "
                    + "OR lower(c.lastname) LIKE :search " + "OR lower(c.adresses.email) LIKE :search "
                    + "OR lower(c.adresses.town) LIKE :search " + ")" + "AND" + "("
                    + "lower(c.userSearchs) LIKE :userSearchs " + ")" + ")";

        } else if (userOffers.length() != 0 && userSearchs.length() != 0) {

            hql += "AND " + "(" + "(" + "lower(c.userOffers) LIKE :userOffers " + ")" + "AND" + "("
                    + "lower(c.userSearchs) LIKE :userSearchs " + ")" + ")";

        } else if (searchTxt.length() != 0) {

            hql += "AND " + "(" + "(" + "lower(c.login) LIKE :search " + "OR lower(c.firstname) LIKE :search "
                    + "OR lower(c.lastname) LIKE :search " + "OR lower(c.adresses.email) LIKE :search "
                    + "OR lower(c.adresses.town) LIKE :search " + ")" + ")";

        } else if (userOffers.length() != 0) {

            hql += "AND " + "(" + "(" + "lower(c.userOffers) LIKE :userOffers " + ")" + ")";

        } else if (userSearchs.length() != 0) {

            hql += "AND " + "(" + "(" + "lower(c.userSearchs) LIKE :userSearchs " + ")" + ")";

        }

        hql += " ORDER BY " + orderBy;

        if (asc) {
            hql += " ASC";
        } else {
            hql += " DESC";
        }

        if (searchTxt.length() != 0) {
            searchTxt = "%" + searchTxt + "%";
        }

        if (userOffers.length() != 0) {
            userOffers = "%" + userOffers + "%";
        }

        if (userSearchs.length() != 0) {
            userSearchs = "%" + userSearchs + "%";
        }

        log.debug("hql :: " + hql);

        // get all users
        TypedQuery<User> query = em.createQuery(hql, User.class);

        if (searchTxt.length() != 0 && userOffers.length() != 0 && userSearchs.length() != 0) {

            query.setParameter("search", StringUtils.lowerCase(searchTxt));
            query.setParameter("userOffers", StringUtils.lowerCase(userOffers));
            query.setParameter("userSearchs", StringUtils.lowerCase(userSearchs));

        } else if (searchTxt.length() != 0 && userOffers.length() != 0) {

            query.setParameter("search", StringUtils.lowerCase(searchTxt));
            query.setParameter("userOffers", StringUtils.lowerCase(userOffers));

        } else if (searchTxt.length() != 0 && userSearchs.length() != 0) {

            query.setParameter("search", StringUtils.lowerCase(searchTxt));
            query.setParameter("userSearchs", StringUtils.lowerCase(userSearchs));

        } else if (userOffers.length() != 0 && userSearchs.length() != 0) {

            query.setParameter("userOffers", StringUtils.lowerCase(userOffers));
            query.setParameter("userSearchs", StringUtils.lowerCase(userSearchs));

        } else if (searchTxt.length() != 0) {

            query.setParameter("search", StringUtils.lowerCase(searchTxt));

        } else if (userOffers.length() != 0) {

            query.setParameter("userOffers", StringUtils.lowerCase(userOffers));

        } else if (userSearchs.length() != 0) {

            query.setParameter("userSearchs", StringUtils.lowerCase(userSearchs));

        }

        query.setMaxResults(max);
        query.setFirstResult(start);

        List<User> userList = query.getResultList();

        return userList;

    } catch (Exception ex2) {
        log.error("[getUsersList] ", ex2);
    }

    return null;
}

From source file:org.apache.openmeetings.db.dao.user.AbstractUserDao.java

public List<User> get(String search, int start, int count, String sort, boolean isAdmin, long currentUserId) {
    TypedQuery<User> q = em.createQuery(DaoHelper.getSearchQuery("User", "u", search, true, false,
            getAdditionalWhere(isAdmin), sort, searchFields), User.class);
    q.setFirstResult(start);/*  w  w  w  .j  a v  a  2  s.  c  o m*/
    q.setMaxResults(count);
    setAdditionalParams(q, isAdmin, currentUserId);
    return q.getResultList();
}

From source file:org.apache.openmeetings.db.dao.user.PrivateMessageDao.java

public List<PrivateMessage> get(Long ownerId, Long folderId, String search, String orderBy, boolean asc,
        int start, int max) {
    TypedQuery<PrivateMessage> query = em.createQuery(getQuery(false, search, orderBy, asc),
            PrivateMessage.class);
    query.setParameter("ownerId", ownerId);
    query.setParameter("folderId", folderId);
    if (!StringUtils.isEmpty(search)) {
        query.setParameter("search", StringUtils.lowerCase("%" + search + "%"));
    }//from w  w w  . ja va2 s  .c o  m
    query.setFirstResult(start);
    query.setMaxResults(max);
    return query.getResultList();
}

From source file:org.apache.openmeetings.db.dao.user.UserDao.java

@Override
public List<User> get(int first, int count) {
    TypedQuery<User> q = em.createNamedQuery("getNondeletedUsers", User.class);
    q.setFirstResult(first);/*from   w  w  w. j av  a  2  s.co m*/
    q.setMaxResults(count);
    return q.getResultList();
}

From source file:org.apache.openmeetings.db.dao.user.UserDao.java

public List<User> get(String search, int start, int count, String sort, boolean filterContacts,
        Long currentUserId) {/*from w w w .  j  a  v  a2  s  .c o m*/
    Map<String, Object> params = new HashMap<String, Object>();
    TypedQuery<User> q = em.createQuery(
            DaoHelper.getSearchQuery("User", "u", getAdditionalJoin(filterContacts), search, true, true, false,
                    getAdditionalWhere(filterContacts, currentUserId, params), sort, searchFields),
            User.class);
    q.setFirstResult(start);
    q.setMaxResults(count);
    setAdditionalParams(q, params);
    return q.getResultList();
}

From source file:org.apache.openmeetings.db.dao.user.UserDao.java

public List<User> get(String search, boolean excludeContacts, int first, int count) {
    Map<String, Object> params = new HashMap<String, Object>();
    TypedQuery<User> q = em.createQuery(DaoHelper.getSearchQuery("User", "u", null, search, true, true, false,
            getAdditionalWhere(excludeContacts, params), null, searchFields), User.class);
    setAdditionalParams(q, params);//from ww w.j  a  v  a2s . co m
    q.setFirstResult(first);
    q.setMaxResults(count);
    return q.getResultList();
}

From source file:org.apache.openmeetings.db.util.DaoHelper.java

public static <T> TypedQuery<T> setLimits(TypedQuery<T> q, Long first, Long max) {
    if (first != null) {
        q.setFirstResult(first.intValue());
    }//from   w  w w .  java2 s .co m
    if (max != null) {
        q.setMaxResults(max.intValue());
    }
    return q;
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAAccessTokenDAO.java

@Transactional(readOnly = true)
@Override/*  w  ww.ja v  a 2  s .c o m*/
public List<AccessToken> findAll(final int page, final int itemsPerPage,
        final List<OrderByClause> orderByClauses) {
    StringBuilder queryString = buildFindAllQuery().append(toOrderByStatement(orderByClauses));

    TypedQuery<AccessToken> query = entityManager().createQuery(queryString.toString(), AccessToken.class);

    query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));

    if (itemsPerPage > 0) {
        query.setMaxResults(itemsPerPage);
    }

    return query.getResultList();
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAAnyObjectDAO.java

@Override
public List<AnyObject> findAll(final int page, final int itemsPerPage) {
    TypedQuery<AnyObject> query = entityManager().createQuery(
            "SELECT e FROM  " + JPAAnyObject.class.getSimpleName() + " e ORDER BY e.id", AnyObject.class);
    query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));
    query.setMaxResults(itemsPerPage);

    return query.getResultList();
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAGroupDAO.java

@Override
public List<Group> findAll(final int page, final int itemsPerPage) {
    TypedQuery<Group> query = entityManager()
            .createQuery("SELECT e FROM  " + JPAGroup.class.getSimpleName() + " e ORDER BY e.id", Group.class);
    query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));
    query.setMaxResults(itemsPerPage);

    return query.getResultList();
}