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.Address; import com.bookselling.domain.Name; import com.bookselling.domain.PhoneNumber; import com.bookselling.domain.SellerInvoice; import com.bookselling.domain.User; import com.bookselling.domain.statistic.GenericChartUnit; import com.bookselling.domain.statistic.StatisticData; import com.bookselling.form.filter.SellerInvoiceFilterForm; import com.bookselling.form.orderby.SellerInvoiceOrderType; import com.bookselling.form.orderby.SortType; import com.bookselling.form.orderby.TradeOrderType; import com.bookselling.form.searchby.SellerInvoiceFilterType; import com.bookselling.form.searchby.TradeFilterType; import com.bookselling.util.PaginationData; import java.io.Serializable; import java.math.BigDecimal; import java.math.BigInteger; import java.text.SimpleDateFormat; 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 SellerInvoiceDaoImpl extends GenericDao<SellerInvoice> implements SellerInvoiceDao { @Override public Class<SellerInvoice> registeredClass() { return SellerInvoice.class; } @Override public PaginationData<SellerInvoice> get(User user, int first, int items) { Criteria criteria = getSession().createCriteria(SellerInvoice.class); criteria.createAlias("seller", "sl").add(Restrictions.eq("sl.id", user.getId())); criteria.setFirstResult(first); criteria.setMaxResults(items); Set<SellerInvoice> sellerInvoices = new HashSet<>(criteria.list()); HibernateInitSupport.setCls(SellerInvoice.class); for (SellerInvoice invoice : sellerInvoices) HibernateInitSupport.initDomain(invoice); PaginationData paginationData = new PaginationData( (long) criteria.setProjection(Projections.rowCount()).uniqueResult(), items, first, sellerInvoices); return paginationData; } @Override public SellerInvoice get(User user, int id) { Criteria criteria = getSession().createCriteria(SellerInvoice.class); criteria.createAlias("seller", "sl").add(Restrictions.eq("sl.id", user.getId())) .add(Restrictions.eq("id", id)); Set<SellerInvoice> sellerInvoices = new HashSet<>(criteria.list()); HibernateInitSupport.setCls(SellerInvoice.class); for (SellerInvoice invoice : sellerInvoices) HibernateInitSupport.initDomain(invoice); return sellerInvoices.iterator().next(); } private Object[] filterCriteria(SellerInvoiceFilterForm form, int first, int items, int id) { String keyword = form.getKeyword(); SellerInvoiceFilterType searchBy = form.getSearchBy(); Date fromDate = form.getFromDate(); Date toDate = form.getToDate(); Double fromPrice = form.getFromPrice(); Double toPrice = form.getToPrice(); SellerInvoiceOrderType orderBy = form.getOrderBy(); SortType sortType = form.getSortType(); Criteria criteria = getSession().createCriteria(SellerInvoice.class); criteria.createAlias("seller", "sl").createAlias("buyer", "bye").createAlias("bye.account", "acc"); if (keyword != null) { keyword = "%" + keyword + "%"; if (searchBy == SellerInvoiceFilterType.BUYER) criteria.add(Restrictions.like("acc.username", keyword)); else if (searchBy == SellerInvoiceFilterType.OWNER) { Name name = new Name(); name.setName(keyword); criteria.add(Restrictions.like("bye.name", name)); } else if (searchBy == SellerInvoiceFilterType.ADDRESS) { Address address = new Address(); address.setAddress(keyword); criteria.add(Restrictions.like("contact.address", address)); } else if (searchBy == SellerInvoiceFilterType.PHONE) { PhoneNumber phone = new PhoneNumber(); phone.setPhoneNumber(keyword); criteria.add(Restrictions.like("contact.phoneNumber", phone)); } } if (fromDate != null) criteria.add(Restrictions.ge("createdDate", fromDate)); if (toDate != null) criteria.add(Restrictions.le("createdDate", toDate)); if (fromPrice != null) criteria.add(Restrictions.ge("totalPrice", fromPrice)); if (toPrice != null) criteria.add(Restrictions.le("totalPrice", toPrice)); String propertyName = null; if (orderBy == SellerInvoiceOrderType.BUYER) propertyName = "acc.username"; else if (orderBy == SellerInvoiceOrderType.OWNER) propertyName = "bye.name"; else if (orderBy == SellerInvoiceOrderType.DATE) propertyName = "createdDate"; else if (orderBy == SellerInvoiceOrderType.PRICE) propertyName = "totalPrice"; if (id != -1) criteria.add(Restrictions.eq("sl.id", id)); //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(SellerInvoice.class); subCriteria.createAlias("seller", "sl").createAlias("buyer", "bye").createAlias("bye.account", "acc") .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1))) .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName)); return new Object[] { subCriteria, rowCount }; } @Override public PaginationData<SellerInvoice> filter(SellerInvoiceFilterForm form, int first, int items) { Object[] data = filterCriteria(form, first, items, -1); Criteria criteria = (Criteria) data[0]; Set<SellerInvoice> sellerInvoices = new LinkedHashSet<>(criteria.list()); HibernateInitSupport.setCls(SellerInvoice.class); for (SellerInvoice invoice : sellerInvoices) HibernateInitSupport.initDomain(invoice); PaginationData paginationData = new PaginationData((long) data[1], items, first, sellerInvoices); return paginationData; } @Override public PaginationData<SellerInvoice> filterByUser(User user, SellerInvoiceFilterForm form, int first, int items) { Object[] data = filterCriteria(form, first, items, user.getId()); Criteria criteria = (Criteria) data[0]; Set<SellerInvoice> sellerInvoices = new LinkedHashSet<>(criteria.list()); HibernateInitSupport.setCls(SellerInvoice.class); for (SellerInvoice invoice : sellerInvoices) HibernateInitSupport.initDomain(invoice); PaginationData paginationData = new PaginationData((long) data[1], items, first, sellerInvoices); return paginationData; } @Override public StatisticData<GenericChartUnit<Object>> profitAndQuatityStatistic(User user, Date fromDate, Date toDate) { StatisticData<GenericChartUnit<Object>> statisticData = new StatisticData<>(new HashSet<>()); statisticData.setLabel("Doanh thu v doanh s"); SQLQuery sqlQuery = getSession() .createSQLQuery("select DATE(createdDate) date, sum(totalPrice) total, SUM(quatity) totalQuatity " + "from sellerinvoice sli, sellerinvoicedetail slid " + "where sli.id = slid.sellerInvoiceId and " + " createdDate >= :fromDate and createdDate <= :toDate and " + " seller_id = :id and " + " status = 'SHIPPED' " + "group by date " + "order by date asc"); List<Object[]> queryResult = sqlQuery.setDate("fromDate", fromDate).setDate("toDate", toDate) .setInteger("id", user.getId()).list(); for (Object[] row : queryResult) { GenericChartUnit<Object> unit = new GenericChartUnit<>(((Date) row[0]).getTime(), new ArrayList<Object>());//thm i tng hin th unit.getFigures().add(row[1]);//thm s liu unit.getFigures().add(row[2]); statisticData.getData().add(unit); } return statisticData; } @Override public double countTotalProfit(User user) { SQLQuery sqlQuery = getSession().createSQLQuery( "select SUM(totalPrice) profit " + "from sellerinvoicedetail slid, sellerinvoice sli " + "where slid.sellerInvoiceId = sli.id and " + " sli.seller_id = :id "); Double figure = (Double) sqlQuery.setInteger("id", user.getId()).uniqueResult(); return figure == null ? 0 : figure.doubleValue(); } @Override public int countSoldItem(User user) { SQLQuery sqlQuery = getSession() .createSQLQuery("select SUM(quatity) " + "from sellerinvoicedetail slid, sellerinvoice sli " + "where slid.sellerInvoiceId = sli.id and " + " sli.seller_id = :id "); BigDecimal figure = (BigDecimal) sqlQuery.setInteger("id", user.getId()).uniqueResult(); return figure != null ? figure.intValue() : 0; } }