1 25 26 package com.j2biz.blogunity.dao; 27 28 import java.io.Serializable ; 29 import java.util.Collections ; 30 import java.util.Comparator ; 31 import java.util.List ; 32 33 import net.sf.hibernate.Hibernate; 34 import net.sf.hibernate.HibernateException; 35 import net.sf.hibernate.Query; 36 import net.sf.hibernate.Session; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 import com.j2biz.blogunity.exception.BlogunityException; 42 import com.j2biz.blogunity.i18n.I18N; 43 import com.j2biz.blogunity.i18n.I18NStatusFactory; 44 import com.j2biz.blogunity.pojo.Blog; 45 import com.j2biz.blogunity.pojo.Referer; 46 import com.j2biz.blogunity.util.HibernateUtil; 47 48 public class RefererDAO extends AbstractDAO { 49 52 private static final Log log = LogFactory.getLog(RefererDAO.class); 53 54 57 public RefererDAO() { 58 super(); 59 } 60 61 public Referer getRefererByID(long id) throws BlogunityException { 62 63 Referer referer = getRefererByID(new Long (id)); 64 return referer; 65 } 66 67 public Referer getRefererByID(Long id) throws BlogunityException { 68 69 Session session = HibernateUtil.getSession(); 70 Referer referer = null; 71 try { 72 referer = (Referer) session.load(Referer.class, id); 73 } catch (HibernateException ex) { 74 log.error("getRefererByID(Long)", ex); 75 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID, 76 "Referer", ex)); 77 } 78 79 return referer; 80 } 81 82 public Serializable createReferer(Referer referer) throws BlogunityException { 83 84 Session session = HibernateUtil.getSession(); 85 try { 86 Serializable returnSerializable = session.save(referer); 87 return returnSerializable; 88 } catch (HibernateException e) { 89 log.error("createReferer(Referer)", e); 90 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE, "Referer", e)); 91 } 92 } 93 94 public void deleteReferer(Referer referer) throws BlogunityException { 95 96 Session session = HibernateUtil.getSession(); 97 try { 98 session.delete(referer); 99 } catch (HibernateException e) { 100 log.error("deleteReferer(Referer)", e); 101 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, "Referer", e)); 102 } 103 104 } 105 106 public void deleteRefererOlderThan(long millis) throws BlogunityException { 107 108 Session session = HibernateUtil.getSession(); 109 try { 110 if (log.isDebugEnabled()) { 111 log.debug("delete from Referer as referer where referer.refererTimeInMillis <= " 112 + millis); 113 } 114 session.delete("from Referer as referer where referer.refererTimeInMillis <= :d", 115 new Long (millis), Hibernate.LONG); 116 117 126 } catch (HibernateException e) { 127 log.error("deleteRefererOlderThan(Date)", e); 128 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, "Referer", e)); 129 } 130 } 131 132 public void updateReferer(Referer referer) throws BlogunityException { 133 134 Session session = HibernateUtil.getSession(); 135 try { 136 session.update(referer); 137 } catch (HibernateException e) { 138 log.error("updateReferer(Referer)", e); 139 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE, "Referer", e)); 140 } 141 142 } 143 144 public List getTodayReferers(Blog b) throws BlogunityException { 145 Session session = HibernateUtil.getSession(); 146 try { 147 Query q = session 148 .createQuery("select new com.j2biz.blogunity.pojo.Referer( referer.referer, count(*)) " 149 + "from Referer referer where referer.blog = :blog group by referer.referer"); 150 q.setParameter("blog", b); 151 154 List results = q.list(); 156 157 Collections.sort(results, new Comparator () { 158 public int compare(Object o1, Object o2) { 159 Referer ref1 = (Referer) o1; 160 Referer ref2 = (Referer) o2; 161 return ref2.getNumberOfReferers() - ref1.getNumberOfReferers(); 162 } 163 }); 164 165 return results; 167 168 } catch (HibernateException e) { 169 log.error("getTodayReferers(Blog)", e); 170 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST, 171 "Referers", e)); 172 173 } 174 } 175 176 } | Popular Tags |