Example usage for org.hibernate Criteria setMaxResults

List of usage examples for org.hibernate Criteria setMaxResults

Introduction

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

Prototype

public Criteria setMaxResults(int maxResults);

Source Link

Document

Set a limit upon the number of objects to be retrieved.

Usage

From source file:com.fich.wafproject.dao.UserDaoImpl.java

public List<Users> findAll(int pageNumber, String[] targets, String[] names, String[] values,
        boolean pagination) {
    int pageSize = 4;
    Criteria crit = this.createEntityCriteria();
    crit.setProjection(Projections.distinct(Projections.property("id")));
    String dateFrom = "", dateTo = "", targetDate = "";
    if (names != null) {
        for (String alias : names) {
            crit.createAlias(alias, alias);
        }/*from   w  w w.j  a va  2s. co m*/
    }
    int count = 0;
    if (values != null) {
        for (String value : values) {
            if (!value.equals("") && value != null) {
                if (targets[count].contains("date")) {
                    if (dateFrom != "") {
                        dateTo = value;
                    } else {
                        dateFrom = value;
                        targetDate = targets[count];
                    }
                } else {
                    crit.add(Restrictions.like(targets[count], "%" + value + "%"));
                }
            }
            count++;
        }
        if (targetDate != "") {
            if (dateFrom != "" && dateTo == "") {
                dateTo = dateFrom;
            }
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
            try {
                Date dateF = format.parse(dateFrom);
                Date dateT = format.parse(dateTo);
                crit.add(Restrictions.between(targetDate, dateF, dateT));
            } catch (ParseException ex) {
                Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    if (pagination) {
        crit.setFirstResult((pageNumber - 1) * pageSize);
        crit.setMaxResults(pageSize);
    }
    List<Users> users = new ArrayList<Users>();
    for (Object idEvent : crit.list()) {
        //            System.out.println(idEvent);
        users.add(this.findById((Long) idEvent));
    }
    return (List<Users>) users;
}

From source file:com.fich.wafproject.dao.UserHistoryDaoImpl.java

@SuppressWarnings("unchecked")
public List<UsersHistory> findAll(int pageNumber) {
    int pageSize = 6;
    Criteria crit = createEntityCriteria();
    crit.setProjection(Projections.distinct(Projections.property("id")));
    crit.setFirstResult((pageNumber - 1) * pageSize);
    crit.setMaxResults(pageSize);

    List<UsersHistory> events = new ArrayList<UsersHistory>();
    for (Object idUsersHistory : crit.list()) {
        //            System.out.println(idUsersHistory);
        events.add(this.findById((Long) idUsersHistory));
    }//  ww w.  j a v  a2 s  .c  o m
    return events;
}

From source file:com.fich.wafproject.dao.UserHistoryDaoImpl.java

public List<UsersHistory> filter(String[] values, String[] names, String[] targets, int pageNumber,
        String role) {//from   w ww.j  a  v  a 2s  .co m
    int pageSize = 6;
    int count = 0;
    boolean filterByUserProperty = false, filterByUserName = false;
    Criteria crit = this.createEntityCriteria();//.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    crit.setProjection(Projections.distinct(Projections.property("id")));
    String dateFrom = "", dateTo = "", targetDate = "";
    if (names != null) {
        for (String alias : names) {
            crit.createAlias(alias, alias);
            if (alias.equals("user"))
                filterByUserProperty = true;
        }
    }
    if (!filterByUserProperty && !role.equals(""))
        crit.createAlias("user", "user");
    if (values != null) {
        for (String value : values) {
            if (!value.equals("") && value != null) {
                if (targets[count].contains("date")) {
                    if (dateFrom != "") {
                        dateTo = value;
                    } else {
                        dateFrom = value;
                        targetDate = targets[count];
                    }
                } else {
                    if (targets[count].contains("userName"))
                        filterByUserName = true;
                    crit.add(Restrictions.like(targets[count], "%" + value + "%"));
                }
            }
            count++;
        }
        if (targetDate != "") {
            if (dateFrom != "" && dateTo == "") {
                dateTo = dateFrom;
            }
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
            try {
                Date dateF = format.parse(dateFrom);
                Date dateT = format.parse(dateTo);
                crit.add(Restrictions.between(targetDate, dateF, dateT));
            } catch (ParseException ex) {
                Logger.getLogger(UserHistoryDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    if (!filterByUserName && !role.equals(""))
        crit.add(Restrictions.like("user.userName", role));
    crit.setFirstResult((pageNumber - 1) * pageSize);
    crit.setMaxResults(pageSize);
    List<UsersHistory> events = new ArrayList<UsersHistory>();
    for (Object idEvent : crit.list()) {
        events.add(this.findById((Long) idEvent));
    }
    return (List<UsersHistory>) events;
}

From source file:com.floreantpos.model.dao.KitchenTicketDAO.java

License:Open Source License

public List<Ticket> findNextKitchenTickets(PaginatedTableModel tableModel) {
    Session session = null;/*from  www  .j av a2s  . c  o  m*/
    Criteria criteria = null;

    try {
        int nextIndex = tableModel.getNextRowIndex();

        session = createNewSession();
        criteria = session.createCriteria(getReferenceClass());

        criteria.setFirstResult(nextIndex);
        criteria.setMaxResults(tableModel.getPageSize());

        List kitchenTicketList = criteria.list();

        criteria.setProjection(Projections.rowCount());
        Integer rowCount = (Integer) criteria.uniqueResult();
        if (rowCount != null) {
            tableModel.setNumRows(rowCount);

        }
        tableModel.setCurrentRowIndex(nextIndex);

        return kitchenTicketList;

    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.KitchenTicketDAO.java

License:Open Source License

public List<Ticket> findPreviousKitchenTickets(PaginatedTableModel tableModel) {
    Session session = null;/* w w w  .  ja  v a 2  s .  com*/
    Criteria criteria = null;
    try {

        int previousIndex = tableModel.getPreviousRowIndex();

        session = createNewSession();
        criteria = session.createCriteria(getReferenceClass());

        criteria.setFirstResult(previousIndex);
        criteria.setMaxResults(tableModel.getPageSize());

        List kitchenTicketList = criteria.list();

        criteria.setProjection(Projections.rowCount());
        Integer rowCount = (Integer) criteria.uniqueResult();
        if (rowCount != null) {
            tableModel.setNumRows(rowCount);

        }

        tableModel.setCurrentRowIndex(previousIndex);

        return kitchenTicketList;

    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.TicketDAO.java

License:Open Source License

public List<Ticket> findTickets(PaginatedTableModel tableModel) {
    Session session = null;/*from w  ww . j a  v a2  s .  co m*/
    Criteria criteria = null;

    try {
        session = createNewSession();
        criteria = session.createCriteria(getReferenceClass());
        updateCriteriaFilters(criteria);

        criteria.setFirstResult(0);
        criteria.setMaxResults(tableModel.getPageSize());

        List ticketList = criteria.list();

        criteria.setProjection(Projections.rowCount());
        Integer rowCount = (Integer) criteria.uniqueResult();
        if (rowCount != null) {
            tableModel.setNumRows(rowCount);

        }

        tableModel.setCurrentRowIndex(0);

        return ticketList;

    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.TicketDAO.java

License:Open Source License

public List<Ticket> findNextTickets(PaginatedTableModel tableModel) {
    Session session = null;//from  ww w.ja v  a  2 s. c  o  m
    Criteria criteria = null;

    try {
        int nextIndex = tableModel.getNextRowIndex();

        session = createNewSession();
        criteria = session.createCriteria(getReferenceClass());
        updateCriteriaFilters(criteria);

        criteria.setFirstResult(nextIndex);
        criteria.setMaxResults(tableModel.getPageSize());

        List ticketList = criteria.list();

        criteria.setProjection(Projections.rowCount());
        Integer rowCount = (Integer) criteria.uniqueResult();
        if (rowCount != null) {
            tableModel.setNumRows(rowCount);

        }

        tableModel.setCurrentRowIndex(nextIndex);

        return ticketList;

    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.TicketDAO.java

License:Open Source License

public List<Ticket> findPreviousTickets(PaginatedTableModel tableModel) {
    Session session = null;//from  www  .j  a  va2 s. c o  m
    Criteria criteria = null;
    try {

        int previousIndex = tableModel.getPreviousRowIndex();

        session = createNewSession();
        criteria = session.createCriteria(getReferenceClass());
        updateCriteriaFilters(criteria);

        criteria.setFirstResult(previousIndex);
        criteria.setMaxResults(tableModel.getPageSize());

        List ticketList = criteria.list();

        criteria.setProjection(Projections.rowCount());
        Integer rowCount = (Integer) criteria.uniqueResult();
        if (rowCount != null) {
            tableModel.setNumRows(rowCount);

        }

        tableModel.setCurrentRowIndex(previousIndex);

        return ticketList;

    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.TicketDAO.java

License:Open Source License

public List<Ticket> findCustomerTickets(Integer customerId, PaginatedTableModel tableModel) {
    Session session = null;//from   ww  w  .  j  a  v a 2s.co  m
    Criteria criteria = null;

    try {
        session = createNewSession();
        criteria = session.createCriteria(getReferenceClass());
        criteria.add(Restrictions.eq(Ticket.PROP_CUSTOMER_ID, customerId));

        /*if (filter.equals(PaymentStatusFilter.OPEN.toString())) {
           criteria.add(Restrictions.eq(Ticket.PROP_PAID, Boolean.FALSE));
           criteria.add(Restrictions.eq(Ticket.PROP_CLOSED, Boolean.FALSE));
        }*/

        criteria.setFirstResult(0);
        criteria.setMaxResults(tableModel.getPageSize());

        List ticketList = criteria.list();

        criteria.setProjection(Projections.rowCount());
        Integer rowCount = (Integer) criteria.uniqueResult();
        if (rowCount != null) {
            tableModel.setNumRows(rowCount);

        }

        tableModel.setCurrentRowIndex(0);

        return ticketList;

    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.TicketDAO.java

License:Open Source License

public List<Ticket> findNextCustomerTickets(Integer customerId, PaginatedTableModel tableModel, String filter) {
    Session session = null;/*from   w  ww  .j av  a 2  s .c o m*/
    Criteria criteria = null;

    try {
        int nextIndex = tableModel.getNextRowIndex();

        session = createNewSession();
        criteria = session.createCriteria(getReferenceClass());
        criteria.add(Restrictions.eq(Ticket.PROP_CUSTOMER_ID, customerId));

        if (filter.equals(PaymentStatusFilter.OPEN)) {
            criteria.add(Restrictions.eq(Ticket.PROP_PAID, Boolean.FALSE));
            criteria.add(Restrictions.eq(Ticket.PROP_CLOSED, Boolean.FALSE));
        }

        criteria.setFirstResult(nextIndex);
        criteria.setMaxResults(tableModel.getPageSize());

        List ticketList = criteria.list();

        criteria.setProjection(Projections.rowCount());
        Integer rowCount = (Integer) criteria.uniqueResult();
        if (rowCount != null) {
            tableModel.setNumRows(rowCount);

        }

        tableModel.setCurrentRowIndex(nextIndex);

        return ticketList;

    } finally {
        closeSession(session);
    }
}