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 persistence; import entity.Notice; import entity.Order; import entity.eventType.SystemEventType; import java.math.BigInteger; import java.util.Calendar; import java.util.Date; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Restrictions; import org.springframework.stereotype.Repository; import persistence.parent.Dao; import support.FormatDate; /** * * @author Rice Pavel */ @Repository public class NoticeDao extends Dao<Notice> { public enum AuthorType { /** * ?? ? */ ONLY_AUTHOR, /** * ?? ? */ NOT_AUTHOR; } public Class getSupportedClass() { return Notice.class; } public List<Notice> getByOrder(Order order) { Criteria crit = getCriteriaDistinctRootEntity(Notice.class); crit.add(Restrictions.eq("order", order)); return crit.list(); } /** * ? * * @param noticeType ? * @param dateFrom * @param dateTo ? * @param authorType . null * @return */ public List<Notice> getAll(Notice.NoticeType noticeType, Date dateFrom, Date dateTo, AuthorType authorType) { Criteria cr = currentSession().createCriteria(getSupportedClass()); cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); if (noticeType != null) { cr.add(Restrictions.eq("noticeType", noticeType)); } if (authorType != null) { if (authorType.equals(AuthorType.ONLY_AUTHOR)) { cr.add(Restrictions.eq("authorDelivery", true)); } else if (authorType.equals(AuthorType.NOT_AUTHOR)) { cr.add(Restrictions.or(Restrictions.eq("authorDelivery", false), Restrictions.isNull("authorDelivery"))); } } if (dateFrom != null) { cr.add(Restrictions.ge("noticeDate", dateFrom)); } if (dateTo != null) { cr.add(Restrictions.le("noticeDate", dateTo)); } return cr.list(); } public List<Notice> getNotices(Order order, SystemEventType type) { Criteria crit = getCriteriaDistinctRootEntity(Notice.class); if (order != null) { crit.add(Restrictions.eq("order", order)); } if (type != null) { crit.add(Restrictions.eq("systemEventType", type)); } return crit.list(); } /*public Integer getTodayNewOrderNoticesCount(){ Calendar c = Calendar.getInstance(); Date now = c.getTime(); c.add(Calendar.DAY_OF_MONTH, -1); Date dayBefore = c.getTime(); String hql = "select count(noticeId) from Notice where systemEventType=:newOrder and authorDelivery is not null and noticeDate<:end and noticeDate>:start"; Query query = getCurrentSession().createQuery(hql); query.setParameter("newOrder", SystemEventType.NEW_ORDER); query.setParameter("end", now); query.setParameter("start", dayBefore); Long res = (Long)query.uniqueResult(); return res.intValue(); }*/ public Integer getTodayNewOrderNoticesSentCount() { Calendar c = Calendar.getInstance(); Date now = c.getTime(); c.add(Calendar.DAY_OF_MONTH, -1); Date dayBefore = c.getTime(); String hql = "select count(noticeId) from Notice where systemEventType=:newOrder and authorDelivery is not null and noticeDate<:end and noticeDate>:start and sent is not null"; Query query = getCurrentSession().createQuery(hql); query.setParameter("newOrder", SystemEventType.NEW_ORDER); query.setParameter("end", now); query.setParameter("start", dayBefore); Long res = (Long) query.uniqueResult(); return res.intValue(); } }