Example usage for org.hibernate Query setMaxResults

List of usage examples for org.hibernate Query setMaxResults

Introduction

In this page you can find the example usage for org.hibernate Query setMaxResults.

Prototype

@Override
    Query<R> setMaxResults(int maxResult);

Source Link

Usage

From source file:com.edgenius.wiki.dao.hibernate.NotificationDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Notification> getResourceMessages(final User user, final boolean sysAdmin,
        final List<Resource> spaceResList, final List<Resource> adminResList, final int start,
        final int retCount) {
    StringBuffer sql = new StringBuffer(GET_MESSAGES_PREFIX);

    Map<Integer, String> queryParams = new HashMap<Integer, String>();
    int queryPos = 0;
    //if users is instance admin, then get all messages... is it good????
    if (!sysAdmin) {
        //first, get messages to all users and specified to given user
        sql.append(" where n.targetType=").append(SharedConstants.MSG_TARGET_ALL_USERS).append(" or ");
        sql.append(" (n.targetType=").append(SharedConstants.MSG_TARGET_USER).append(" and n.targetName=:p")
                .append(queryPos).append(") ");
        queryParams.put(queryPos++, user.getUsername());

        if (adminResList != null && adminResList.size() > 0) {
            sql.append(" or ");

            for (int idx = 0; idx < adminResList.size(); idx++) {
                Resource resource = adminResList.get(idx);
                if (SecurityValues.RESOURCE_TYPES.SPACE.equals(resource.getType())) {
                    //spaces admin permissions
                    sql.append(" (n.targetType=").append(SharedConstants.MSG_TARGET_SPACE_ADMIN_ONLY)
                            .append(" and ");
                    sql.append(" n.targetName=:p").append(queryPos + idx).append(") or ");
                    queryParams.put(queryPos + idx, resource.getResource());
                }/*w  w  w .j  a  v a 2s .  c o m*/
            }
            //remove last "or"
            sql.delete(sql.length() - 3, sql.length());
        }

        if (spaceResList != null && spaceResList.size() > 0) {
            sql.append(" or ");
            queryPos = queryParams.size();
            for (int idx = 0; idx < spaceResList.size(); idx++) {
                Resource resource = spaceResList.get(idx);
                if (SecurityValues.RESOURCE_TYPES.SPACE.equals(resource.getType())) {
                    //spaces all users
                    sql.append(" (n.targetType=").append(SharedConstants.MSG_TARGET_SPACE_CONTRIBUTE_USERS)
                            .append(" and ");
                    sql.append(" n.targetName=:p").append(queryPos + idx).append(") or ");
                    queryParams.put(queryPos + idx, resource.getResource());
                }
            }
            //remove last "or"
            sql.delete(sql.length() - 3, sql.length());
        }

    }

    sql.append(GET_MESSAGES_SUFFIX);
    Query query = getCurrentSesssion().createQuery(sql.toString());
    for (Entry<Integer, String> entry : queryParams.entrySet()) {
        query.setString("p" + entry.getKey(), entry.getValue());
    }

    if (start > 0)
        query.setFirstResult(start);
    if (retCount > 0)
        query.setMaxResults(retCount);

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getRecentPages(final String spaceUname, final int start, final int count,
        final boolean sortByModify) {
    if (StringUtils.isBlank(spaceUname)) {
        String sql;// www. j  av  a  2  s. c om
        if (!sortByModify) {
            sql = GET_INSTANCE_CURRENT_PAGES + "order by p.createdDate  desc";
        } else {
            sql = GET_INSTANCE_CURRENT_PAGES + "order by p.modifiedDate desc";
        }
        Query query = getCurrentSesssion().createQuery(sql);
        if (start > 0)
            query.setFirstResult(start);
        return query.setMaxResults(count).list();
    } else {
        String sql;
        if (!sortByModify) {
            sql = GET_SPACE_CURRNET_PAGES + "order by p.createdDate  desc";
        } else {
            sql = GET_SPACE_CURRNET_PAGES + "order by p.modifiedDate desc";
        }
        Query query = getCurrentSesssion().createQuery(sql);
        query.setString("spaceUname", spaceUname);
        if (start > 0)
            query.setFirstResult(start);
        return query.setMaxResults(count).list();
    }
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getUserUpdatedPagesInSpace(final String spaceUname, final User user, final int returnNum) {

    Query query;
    if (user == null || user.isAnonymous()) {
        query = getCurrentSesssion().createQuery(GET_ANONYMOUS_PAGES);
        query.setString(0, spaceUname);/*w  w w.ja  va  2s.c  o  m*/
    } else {
        query = getCurrentSesssion().createQuery(GET_USER_PAGES);
        query.setEntity(0, user);
        query.setEntity(1, user);
        query.setString(2, spaceUname);
    }
    if (returnNum > 0)
        query.setMaxResults(returnNum);

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getPagesInSpace(final String spaceUname, final Date touchedDate, final int returnNum) {
    Query query;
    if (touchedDate == null) {
        query = getCurrentSesssion().createQuery(GET_PAGES);
        query.setString(0, spaceUname);//  www .  ja v  a  2  s. c  o m
    } else {
        query = getCurrentSesssion().createQuery(GET_PAGES_AFTER_TOUCHED);
        query.setString(0, spaceUname);
        //DON'T user query.setDate()!!! It only compare Date rather than time, it means the time part is truncated!
        //or use query.setTimestamp();
        query.setParameter(1, touchedDate);
    }
    if (returnNum > 0)
        query.setMaxResults(returnNum);

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getUserContributedPages(final User user, final int limit) {
    Query query;
    if (user == null || user.isAnonymous()) {
        query = getCurrentSesssion().createQuery(GET_ANONYMOUS_CONTRIBUTED_PAGES);
    } else {//  w  w  w  .ja va 2s.co m
        query = getCurrentSesssion().createQuery(GET_USER_CONTRIBUTED_PAGES);
        query.setEntity("user", user);
    }
    if (limit > 0)
        query.setMaxResults(limit);
    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getPageForSitemap(Date lastModifiedDate, final int start, final int returnNum) {
    Query query = getCurrentSesssion().createQuery(GET_FOR_SITE_MAP);
    query.setParameter("lastModifiedDate", lastModifiedDate);

    if (start > 0)
        query.setFirstResult(start);/*from www.  ja  v a  2 s  .  c o  m*/

    List<Object[]> list;
    if (returnNum > 0) {
        list = query.setMaxResults(returnNum).list();
    } else {
        list = query.list();
    }
    if (list == null) {
        return null;
    }

    List<Page> pageList = new ArrayList<Page>(list.size());
    for (Object[] objects : list) {
        Page page = new Page();
        int idx = 0;
        //pageUuid, title, spaceUid, and content
        page.setUid((Integer) objects[idx++]);
        page.setPageUuid((String) objects[idx++]);
        page.setTitle((String) objects[idx++]);
        page.setModifiedDate((Date) objects[idx++]);

        Space space = new Space();
        space.setUnixName((String) objects[idx++]);
        page.setSpace(space);

        pageList.add(page);
    }
    return pageList;

}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getPageForIndexing(final int start, final int returnNum) {
    Query query = getCurrentSesssion().createQuery(GET_PAGE_FOR_INDEXING);
    if (start > 0)
        query.setFirstResult(start);/*from   w  w w .j  ava 2  s.c  o m*/

    List<Object[]> list;
    if (returnNum > 0) {
        list = query.setMaxResults(returnNum).list();
    } else {
        list = query.list();
    }

    if (list == null) {
        return null;
    }

    List<Page> pageList = new ArrayList<Page>(list.size());
    for (Object[] objects : list) {
        Page page = new Page();
        int idx = 0;
        //pageUuid, title, spaceUid, and content
        page.setPageUuid((String) objects[idx++]);
        page.setTitle((String) objects[idx++]);
        page.setModifiedDate((Date) objects[idx++]);

        Space space = new Space();
        space.setUid((Integer) objects[idx++]);

        PageContent content = new PageContent();
        content.setContent((String) objects[idx++]);

        page.setSpace(space);
        page.setContent(content);

        pageList.add(page);
    }
    return pageList;

}

From source file:com.edgenius.wiki.dao.hibernate.SpaceDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Space> getSpaces(final int start, final int returnSize, final String sortBy, final String filter,
        final boolean sortByDesc) {
    String orderBy = "";
    String filterWith = "";
    if (!StringUtils.isBlank(filter)) {
        filterWith = "where s.name like :filter or s.unixName like :filter or s.description like :filter ";
    }// w  ww.  java  2  s.c  o  m
    if (!StringUtils.isBlank(sortBy)) {
        StringBuffer orderSb = new StringBuffer(" order by ");
        String[] sortStr = sortBy.split("\\|");
        String seq = (sortByDesc ? " desc" : " asc");
        for (String str : sortStr) {
            int sort = NumberUtils.toInt(str, -1);
            if (sort == -1)
                continue;
            if (sort == Space.SORT_BY_PAGE_SCORE)
                orderSb.append("s.score ").append(seq).append(",");
            else if (sort == Space.SORT_BY_CREATEON)
                orderSb.append("s.createdDate ").append(seq).append(",");
            else if (sort == Space.SORT_BY_CREATEBY)
                orderSb.append("s.creator ").append(seq).append(",");
            else if (sort == Space.SORT_BY_SPACE_TITLE)
                orderSb.append("s.name ").append(seq).append(",");
            else if (sort == Space.SORT_BY_SPACEKEY)
                orderSb.append("s.unixName ").append(seq).append(",");
        }
        if (orderSb.length() > 0) {
            orderBy = orderSb.toString();
            if (orderBy.endsWith(",")) {
                //remove last ","
                orderBy = orderBy.substring(0, orderBy.length() - 1);
            }
        }
    } else {
        //default order
        orderBy = " order by s.score desc, s.createdDate desc";
    }

    Query query = getCurrentSesssion().createQuery(GET_SPACES + filterWith + orderBy);
    if (!StringUtils.isBlank(filter)) {
        query.setString("filter", "%" + filter.trim() + "%");
    }
    query.setFirstResult(start);
    if (returnSize > 0)
        query.setMaxResults(returnSize);

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.SpaceDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Space> getUserCreatedSpaces(final String username, final int limit) {
    Query query = getCurrentSesssion().createQuery(GET_USER_AUTHORED_SPACES);
    query.setString("name", username);
    if (limit > 0)
        query.setMaxResults(limit);
    return query.list();
}

From source file:com.egs.blog.backend.dao.PostDAOImpl.java

@Override
public List<Post> getPostList(Integer start, Integer max) {
    List<Post> finalList = null;
    try {/*from  ww  w.ja  v  a2s .c o m*/
        Query query = getSession().createQuery("SELECT p FROM Post p WHERE p.id > 0 ORDER BY p.id DESC");
        if (start != null) {
            query.setFirstResult(start);
        }
        if (max != null) {
            query.setMaxResults(max);
        }
        finalList = query.list();

        if (finalList == null) {
            return null;
        }
        //   System.out.println("finalList " + finalList.size());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return finalList;
}