Example usage for org.hibernate Criteria setProjection

List of usage examples for org.hibernate Criteria setProjection

Introduction

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

Prototype

public Criteria setProjection(Projection projection);

Source Link

Document

Used to specify that the query results will be a projection (scalar in nature).

Usage

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

License:Open Source License

public TicketSummary getClosedTicketSummary(Terminal terminal) {

    Session session = null;//from   w ww .  j a  v a2s.  com
    TicketSummary ticketSummary = new TicketSummary();
    try {
        session = getSession();
        Criteria criteria = session.createCriteria(Ticket.class);
        criteria.add(Restrictions.eq(Ticket.PROP_CLOSED, Boolean.TRUE));
        criteria.add(Restrictions.eq(Ticket.PROP_VOIDED, Boolean.FALSE));
        criteria.add(Restrictions.eq(Ticket.PROP_REFUNDED, Boolean.FALSE));
        criteria.add(Restrictions.eq(Ticket.PROP_DRAWER_RESETTED, Boolean.FALSE));
        criteria.add(Restrictions.eq(Ticket.PROP_TERMINAL, terminal));

        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.count(Ticket.PROP_ID));
        projectionList.add(Projections.sum(Ticket.PROP_TOTAL_AMOUNT));
        criteria.setProjection(projectionList);

        List list = criteria.list();
        if (list.size() > 0) {
            Object[] o = (Object[]) list.get(0);
            ticketSummary.setTotalTicket(((Integer) o[0]).intValue());
            ticketSummary.setTotalPrice(o[1] == null ? 0 : ((Double) o[1]).doubleValue());
        }
        return ticketSummary;
    } finally {
        closeSession(session);
    }
}

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

License:Open Source License

public List<Ticket> findTickets(PaginatedTableModel tableModel, Date start, Date end, boolean filter) {
    Session session = null;/*from   w w  w.  jav a  2  s. co m*/
    Criteria criteria = null;

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

        if (filter) {
            updateCriteriaFilters(criteria);
        }

        criteria.setFirstResult(0);
        criteria.setMaxResults(tableModel.getPageSize());
        if (start != null)
            criteria.add(Restrictions.ge(Ticket.PROP_DELIVERY_DATE, start));

        if (end != null)
            criteria.add(Restrictions.le(Ticket.PROP_DELIVERY_DATE, end));

        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, Date start, Date end, boolean filter) {
    Session session = null;/*from  www .ja  v  a2s .co  m*/
    Criteria criteria = null;

    try {
        int nextIndex = tableModel.getNextRowIndex();

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

        if (filter) {
            updateCriteriaFilters(criteria);
        }

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

        if (start != null)
            criteria.add(Restrictions.ge(Ticket.PROP_DELIVERY_DATE, start));

        if (end != null)
            criteria.add(Restrictions.le(Ticket.PROP_DELIVERY_DATE, end));

        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, Date start, Date end, boolean filter) {
    Session session = null;//from  w ww  .  j  a v a 2s .  c  o m
    Criteria criteria = null;
    try {

        int previousIndex = tableModel.getPreviousRowIndex();

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

        if (filter) {
            updateCriteriaFilters(criteria);
        }

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

        if (start != null)
            criteria.add(Restrictions.ge(Ticket.PROP_DELIVERY_DATE, start));

        if (end != null)
            criteria.add(Restrictions.le(Ticket.PROP_DELIVERY_DATE, end));

        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> getTicketsWithSpecificFields(String... fields) {
    Session session = null;//from   ww  w. java 2s.c o m
    Criteria criteria = null;
    User currentUser = Application.getCurrentUser();
    boolean filterUser = !currentUser.isAdministrator() || !currentUser.isManager();
    try {
        session = createNewSession();
        criteria = session.createCriteria(Ticket.class);
        ProjectionList projectionList = Projections.projectionList();
        for (String field : fields) {
            projectionList.add(Projections.property(field));
        }
        criteria.add(Restrictions.eq(Ticket.PROP_CLOSED, Boolean.FALSE));
        if (filterUser) {
            criteria.createAlias(Ticket.PROP_OWNER, "u");
            criteria.add(Restrictions.eq("u.userId", currentUser.getUserId()));
        }
        ResultTransformer transformer = new ResultTransformer() {

            public Object transformTuple(Object[] row, String[] arg1) {
                Ticket ticket = new Ticket();
                ticket.setId(Integer.valueOf("" + row[0]));
                ticket.setDueAmount(Double.valueOf("" + row[1]));
                return ticket;
            }

            public List transformList(List arg0) {
                return arg0;
            }
        };
        criteria.setProjection(projectionList).setResultTransformer(transformer);
        return criteria.list();
    } finally {
        closeSession(session);
    }
}

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

License:Open Source License

public int getNumTickets(Date start, Date end) {
    Session session = null;/*from ww  w .  j  av a 2s  . c  o m*/
    Criteria criteria = null;
    try {
        session = createNewSession();
        criteria = session.createCriteria(getReferenceClass());
        criteria.add(Restrictions.eq(Ticket.PROP_CLOSED, Boolean.FALSE));
        if (start != null)
            criteria.add(Restrictions.ge(Ticket.PROP_DELIVERY_DATE, start));

        if (end != null)
            criteria.add(Restrictions.le(Ticket.PROP_DELIVERY_DATE, end));

        criteria.add(Restrictions.isNotNull(Ticket.PROP_DELIVERY_DATE));
        criteria.setProjection(Projections.rowCount());
        Number rowCount = (Number) criteria.uniqueResult();
        if (rowCount != null) {
            return rowCount.intValue();
        }
        return 0;
    } finally {
        closeSession(session);
    }
}

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

License:Open Source License

public Integer findUserWithMaxId() {
    Session session = null;/*from   w  w  w  .ja v a  2 s  .  c  o  m*/

    try {
        session = getSession();
        Criteria criteria = session.createCriteria(getReferenceClass());
        criteria.setProjection(Projections.max(User.PROP_USER_ID));

        List list = criteria.list();
        if (list != null && list.size() > 0) {
            return (Integer) list.get(0);
        }

        return null;
    } finally {
        if (session != null) {
            closeSession(session);
        }
    }
}

From source file:com.floreantpos.report.service.ReportService.java

License:Open Source License

public MenuUsageReport getMenuUsageReport(Date fromDate, Date toDate) {
    GenericDAO dao = new GenericDAO();
    MenuUsageReport report = new MenuUsageReport();
    Session session = null;/*  w ww.jav a2  s . c om*/

    try {

        session = dao.getSession();

        Criteria criteria = session.createCriteria(MenuCategory.class);
        List<MenuCategory> categories = criteria.list();
        MenuCategory miscCategory = new MenuCategory();
        miscCategory.setName(com.floreantpos.POSConstants.MISC_BUTTON_TEXT);
        categories.add(miscCategory);

        for (MenuCategory category : categories) {
            criteria = session.createCriteria(TicketItem.class, "item"); //$NON-NLS-1$
            criteria.createCriteria("ticket", "t"); //$NON-NLS-1$ //$NON-NLS-2$
            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.sum(TicketItem.PROP_ITEM_COUNT));
            projectionList.add(Projections.sum(TicketItem.PROP_SUBTOTAL_AMOUNT));
            projectionList.add(Projections.sum(TicketItem.PROP_DISCOUNT_AMOUNT));
            criteria.setProjection(projectionList);
            criteria.add(Restrictions.eq("item." + TicketItem.PROP_CATEGORY_NAME, category.getName())); //$NON-NLS-1$
            criteria.add(Restrictions.ge("t." + Ticket.PROP_CREATE_DATE, fromDate)); //$NON-NLS-1$
            criteria.add(Restrictions.le("t." + Ticket.PROP_CREATE_DATE, toDate)); //$NON-NLS-1$
            criteria.add(Restrictions.eq("t." + Ticket.PROP_PAID, Boolean.TRUE)); //$NON-NLS-1$

            List datas = criteria.list();
            if (datas.size() > 0) {
                Object[] objects = (Object[]) datas.get(0);

                MenuUsageReportData data = new MenuUsageReportData();
                data.setCategoryName(category.getName());

                if (objects.length > 0 && objects[0] != null)
                    data.setCount(((Number) objects[0]).intValue());

                if (objects.length > 1 && objects[1] != null)
                    data.setGrossSales(((Number) objects[1]).doubleValue());

                if (objects.length > 2 && objects[2] != null)
                    data.setDiscount(((Number) objects[2]).doubleValue());

                data.calculate();
                report.addReportData(data);
            }
        }

        return report;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:com.floreantpos.report.service.ReportService.java

License:Open Source License

public ServerProductivityReport getServerProductivityReport(Date fromDate, Date toDate) {
    GenericDAO dao = new GenericDAO();
    ServerProductivityReport report = new ServerProductivityReport();
    Session session = null;/*from  w ww .  jav  a2 s  .  co m*/

    try {

        session = dao.getSession();

        Criteria criteria = session.createCriteria(User.class);
        //criteria.add(Restrictions.eq(User.PROP_USER_TYPE, User.USER_TYPE_SERVER));
        List<User> servers = criteria.list();

        criteria = session.createCriteria(MenuCategory.class);
        List<MenuCategory> categories = criteria.list();
        MenuCategory miscCategory = new MenuCategory();
        miscCategory.setName(com.floreantpos.POSConstants.MISC_BUTTON_TEXT);
        categories.add(miscCategory);

        for (User server : servers) {
            ServerProductivityReportData data = new ServerProductivityReportData();
            data.setServerName(server.getUserId() + "/" + server.toString()); //$NON-NLS-1$
            criteria = session.createCriteria(Ticket.class);
            criteria.add(Restrictions.eq(Ticket.PROP_OWNER, server));
            criteria.add(Restrictions.eq(Ticket.PROP_PAID, Boolean.TRUE));
            criteria.add(Restrictions.eq(Ticket.PROP_VOIDED, Boolean.FALSE)); //$NON-NLS-1$
            criteria.add(Restrictions.eq(Ticket.PROP_REFUNDED, Boolean.FALSE)); //$NON-NLS-1$
            criteria.add(Restrictions.ge(Ticket.PROP_CREATE_DATE, fromDate));
            criteria.add(Restrictions.le(Ticket.PROP_CREATE_DATE, toDate));

            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.rowCount());
            projectionList.add(Projections.sum(Ticket.PROP_NUMBER_OF_GUESTS));
            projectionList.add(Projections.sum(TicketItem.PROP_TOTAL_AMOUNT));

            criteria.setProjection(projectionList);

            Object[] o = (Object[]) criteria.uniqueResult();
            int totalCheckCount = 0;
            double totalServerSale = 0;
            if (o != null) {
                if (o.length > 0 && o[0] != null) {
                    int i = ((Number) o[0]).intValue();
                    data.setTotalCheckCount(totalCheckCount = i);
                }
                if (o.length > 1 && o[1] != null) {
                    int i = ((Number) o[1]).intValue();
                    data.setTotalGuestCount(i);
                }
                if (o.length > 2 && o[2] != null) {
                    totalServerSale = ((Number) o[2]).doubleValue();
                    data.setTotalSales(totalServerSale);
                }
            }

            data.calculate();
            report.addReportData(data);

            for (MenuCategory category : categories) {
                data = new ServerProductivityReportData();
                data.setServerName(server.getUserId() + "/" + server.toString()); //$NON-NLS-1$

                criteria = session.createCriteria(TicketItem.class, "item"); //$NON-NLS-1$
                criteria.createCriteria(TicketItem.PROP_TICKET, "t"); //$NON-NLS-1$

                projectionList = Projections.projectionList();
                criteria.setProjection(projectionList);
                projectionList.add(Projections.sum(TicketItem.PROP_ITEM_COUNT));
                projectionList.add(Projections.sum(TicketItem.PROP_SUBTOTAL_AMOUNT));
                projectionList.add(Projections.sum("t." + Ticket.PROP_DISCOUNT_AMOUNT)); //$NON-NLS-1$
                projectionList.add(Projections.rowCount());

                criteria.add(Restrictions.eq("item." + TicketItem.PROP_CATEGORY_NAME, category.getName())); //$NON-NLS-1$
                criteria.add(Restrictions.ge("t." + Ticket.PROP_CREATE_DATE, fromDate)); //$NON-NLS-1$
                criteria.add(Restrictions.le("t." + Ticket.PROP_CREATE_DATE, toDate)); //$NON-NLS-1$
                criteria.add(Restrictions.eq("t." + Ticket.PROP_OWNER, server)); //$NON-NLS-1$
                criteria.add(Restrictions.eq("t." + Ticket.PROP_PAID, Boolean.TRUE)); //$NON-NLS-1$
                criteria.add(Restrictions.eq("t." + Ticket.PROP_VOIDED, Boolean.FALSE)); //$NON-NLS-1$
                criteria.add(Restrictions.eq("t." + Ticket.PROP_REFUNDED, Boolean.FALSE)); //$NON-NLS-1$

                List datas = criteria.list();
                if (datas.size() > 0) {
                    Object[] objects = (Object[]) datas.get(0);

                    data.setCategoryName(category.getName());
                    data.setTotalCheckCount(totalCheckCount);
                    if (objects.length > 0 && objects[0] != null) {
                        int i = ((Number) objects[0]).intValue();
                        data.setCheckCount(i);
                    }

                    if (objects.length > 1 && objects[1] != null) {
                        double d = ((Number) objects[1]).doubleValue();
                        data.setGrossSales(d);
                    }

                    if (objects.length > 2 && objects[2] != null) {
                        double d = ((Number) objects[2]).doubleValue();
                        if (d > 0)
                            data.setSalesDiscount(d);
                    }
                    data.setAllocation((data.getGrossSales() / totalServerSale) * 100.0);
                    data.calculate();
                    report.addReportData(data);
                }
            }
        }
        return report;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:com.floreantpos.report.service.ReportService.java

License:Open Source License

private void calculateDrawerPullAmount(Session session, SalesBalanceReport report, Date fromDate, Date toDate,
        User user) {//ww w  .  j ava  2s .c om
    Criteria criteria = session.createCriteria(DrawerPullReport.class);
    criteria.add(Restrictions.ge(DrawerPullReport.PROP_REPORT_TIME, fromDate));
    criteria.add(Restrictions.le(DrawerPullReport.PROP_REPORT_TIME, toDate));

    if (user != null) {
        criteria.add(Restrictions.eq(DrawerPullReport.PROP_ASSIGNED_USER, user));
    }

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.sum(DrawerPullReport.PROP_DRAWER_ACCOUNTABLE));
    projectionList.add(Projections.sum(DrawerPullReport.PROP_BEGIN_CASH));
    criteria.setProjection(projectionList);

    Object[] o = (Object[]) criteria.uniqueResult();
    if (o.length > 0 && o[0] instanceof Number) {
        double amount = ((Number) o[0]).doubleValue();
        report.setDrawerPullsAmount(amount);
    }
    if (o.length > 1 && o[1] instanceof Number) {
        double amount = ((Number) o[1]).doubleValue();
        report.setDrawerPullsAmount(report.getDrawerPullsAmount() - amount);
    }
}