KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > dao > RefererDAO


1 /*
2  * $Id: RefererDAO.java,v 1.4 2005/01/17 21:36:10 michelson Exp $
3  *
4  * Copyright (c) 2005 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.dao;
27
28 import java.io.Serializable JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Comparator JavaDoc;
31 import java.util.List JavaDoc;
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     /**
50      * Logger for this class
51      */

52     private static final Log log = LogFactory.getLog(RefererDAO.class);
53
54     /**
55      *
56      */

57     public RefererDAO() {
58         super();
59     }
60
61     public Referer getRefererByID(long id) throws BlogunityException {
62
63         Referer referer = getRefererByID(new Long JavaDoc(id));
64         return referer;
65     }
66
67     public Referer getRefererByID(Long JavaDoc 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 JavaDoc createReferer(Referer referer) throws BlogunityException {
83
84         Session session = HibernateUtil.getSession();
85         try {
86             Serializable JavaDoc 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 JavaDoc(millis), Hibernate.LONG);
116
117             // Iterator it = session.iterate(
118
// "from Referer as referer where referer.refererTimeInMillis < :d",
119
// new Long(
120
// millis), Hibernate.LONG);
121
//
122
// while (it.hasNext()) {
123
// session.delete(it.next());
124
// }
125

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 JavaDoc 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             // q.setFirstResult(0);
152
// q.setMaxResults(10);
153

154             //TODO make this query cacheable
155
List JavaDoc results = q.list();
156
157             Collections.sort(results, new Comparator JavaDoc() {
158                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
159                     Referer ref1 = (Referer) o1;
160                     Referer ref2 = (Referer) o2;
161                     return ref2.getNumberOfReferers() - ref1.getNumberOfReferers();
162                 }
163             });
164
165             // return distinct results
166
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