Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.bookselling.dao; import com.bookselling.domain.Name; import com.bookselling.domain.SystemInvoice; import com.bookselling.domain.statistic.GenericChartUnit; import com.bookselling.domain.statistic.StatisticData; import com.bookselling.form.filter.SystemInvoiceFilterForm; import com.bookselling.form.orderby.SortType; import com.bookselling.form.orderby.SystemInvoiceOrderType; import com.bookselling.form.searchby.SystemInvoiceFilterType; import com.bookselling.util.PaginationData; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import org.hibernate.Criteria; import org.hibernate.SQLQuery; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.springframework.stereotype.Repository; /** * * @author Phan Phat */ @Repository public class SystemInvoiceDaoImpl extends GenericDao<SystemInvoice> implements SystemInvoiceDao { @Override public Class<SystemInvoice> registeredClass() { return SystemInvoice.class; } @Override public PaginationData<SystemInvoice> filter(SystemInvoiceFilterForm form, int first, int items) { String keyword = form.getKeyword(); SystemInvoiceFilterType searchBy = form.getSearchBy(); Date fromDate = form.getFromDate(); Date toDate = form.getToDate(); SystemInvoiceOrderType orderBy = form.getOrderBy(); SortType sortType = form.getSortType(); Criteria criteria = getSession().createCriteria(SystemInvoice.class); criteria.createAlias("poster", "pst").createAlias("post", "ps").createAlias("pst.account", "acc"); if (keyword == null) { keyword = "%" + keyword + "%"; if (searchBy == SystemInvoiceFilterType.ACCOUNT) criteria.add(Restrictions.like("acc.username", keyword)); else if (searchBy == SystemInvoiceFilterType.POSTER) { Name name = new Name(); name.setName(keyword); criteria.add(Restrictions.like("pst.name", name)); } else if (searchBy == SystemInvoiceFilterType.POST_HEADER) criteria.add(Restrictions.like("ps.header", keyword)); } if (fromDate != null) criteria.add(Restrictions.ge("createdDate", fromDate)); if (toDate != null) criteria.add(Restrictions.le("createdDate", toDate)); String propertyName = null; if (orderBy == SystemInvoiceOrderType.ACCOUNT) propertyName = "acc.username"; else if (orderBy == SystemInvoiceOrderType.POSTER) propertyName = "pst.name"; else if (orderBy == SystemInvoiceOrderType.POST_HEADER) propertyName = "ps.header"; else if (orderBy == SystemInvoiceOrderType.DATE) propertyName = "createdDate"; //Ly s dng long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult(); //Ly id criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items) .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName)); List<Integer> ids = new ArrayList<>(); for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();) ids.add(temp.next()); //Criteria ph Criteria subCriteria = getSession().createCriteria(SystemInvoice.class); subCriteria.createAlias("poster", "pst").createAlias("post", "ps").createAlias("pst.account", "acc") .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1))) .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName)); Set<SystemInvoice> invoices = new LinkedHashSet<>(subCriteria.list()); HibernateInitSupport.setCls(SystemInvoice.class); for (SystemInvoice invoice : invoices) HibernateInitSupport.initDomain(invoice); PaginationData paginationData = new PaginationData(rowCount, items, first, invoices); return paginationData; } @Override public StatisticData<GenericChartUnit<Object>> countTotalProfit(Date fromDate, Date toDate) { StatisticData<GenericChartUnit<Object>> statisticData = new StatisticData<>(new HashSet<>()); statisticData.setLabel("Doanh thu ng bi"); SQLQuery sqlQuery = getSession().createSQLQuery("select DATE(createdDate) date, SUM(fee) " + "from systeminvoice " + "where createdDate >= DATE(:fromDate) and createdDate <= DATE(:toDate) " + "group by date " + "order by date asc "); sqlQuery.setDate("fromDate", fromDate).setDate("toDate", toDate); List<Object[]> rows = sqlQuery.list(); for (Object[] row : rows) { GenericChartUnit<Object> unit = new GenericChartUnit<>(((Date) row[0]).getTime(), new ArrayList<Object>()); unit.getFigures().add(row[1]); statisticData.getData().add(unit); } return statisticData; } @Override public double totalProfit() { SQLQuery sqlQuery = getSession().createSQLQuery("select SUM(FEE) " + "from systeminvoice "); Double figure = (Double) sqlQuery.uniqueResult(); return figure != null ? figure.doubleValue() : 0.0; } }