KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: VisitedPageDAO.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.VisitedPage;
46 import com.j2biz.blogunity.util.HibernateUtil;
47
48 public class VisitedPageDAO extends AbstractDAO {
49     /**
50      * Logger for this class
51      */

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

57     public VisitedPageDAO() {
58         super();
59     }
60
61     public VisitedPage getVisitedPageByID(long id) throws BlogunityException {
62
63         VisitedPage page = getVisitedPageByID(new Long JavaDoc(id));
64         return page;
65     }
66
67     public VisitedPage getVisitedPageByID(Long JavaDoc id) throws BlogunityException {
68
69         Session session = HibernateUtil.getSession();
70         VisitedPage page = null;
71         try {
72             page = (VisitedPage) session.load(VisitedPage.class, id);
73         } catch (HibernateException ex) {
74             log.error("getVisitedPageByID(Long)", ex);
75             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID,
76                     "VisitedPage", ex));
77         }
78
79         return page;
80     }
81
82     public Serializable JavaDoc createVisitedPage(VisitedPage page) throws BlogunityException {
83
84         Session session = HibernateUtil.getSession();
85         try {
86             Serializable JavaDoc returnSerializable = session.save(page);
87             return returnSerializable;
88         } catch (HibernateException e) {
89             log.error("createVisitedPage(VisitedPage)", e);
90             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE,
91                     "VisitedPage", e));
92         }
93     }
94
95     public void deleteVisitedPage(VisitedPage page) throws BlogunityException {
96
97         Session session = HibernateUtil.getSession();
98         try {
99             session.delete(page);
100         } catch (HibernateException e) {
101             log.error("deleteVisitedPage(VisitedPage)", e);
102             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE,
103                     "VisitedPage", e));
104         }
105
106     }
107
108     public void deleteVisitedPageOlderThan(long millis) throws BlogunityException {
109
110         Session session = HibernateUtil.getSession();
111         try {
112
113             if (log.isDebugEnabled()) {
114                 log.debug("delete from VisitedPage as page where page.visitTimeInMillis <= "
115                         + millis);
116             }
117
118             session.delete("from VisitedPage as page where page.visitTimeInMillis < :d", new Long JavaDoc(
119                     millis), Hibernate.LONG);
120
121             // Iterator it = session.iterate(
122
// "from VisitedPage as page where page.visitTimeInMillis < :d",
123
// new Long(millis), Hibernate.LONG);
124
//
125
// while (it.hasNext()) {
126
// session.delete(it.next());
127
// }
128

129         } catch (HibernateException e) {
130             log.error("deleteVisitedPageOlderThan(long)", e);
131             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE,
132                     "VisitedPage", e));
133         }
134     }
135
136     public void updateVisitedPage(VisitedPage page) throws BlogunityException {
137
138         Session session = HibernateUtil.getSession();
139         try {
140             session.update(page);
141         } catch (HibernateException e) {
142             log.error("updateVisitedPage(Referer)", e);
143             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE,
144                     "VisitedPage", e));
145         }
146
147     }
148
149     public List JavaDoc getTodayVisitedPages(Blog b) throws BlogunityException {
150         Session session = HibernateUtil.getSession();
151         try {
152
153             Query q = session
154                     .createQuery("select new com.j2biz.blogunity.pojo.VisitedPage( page.pageUrl, count(*)) "
155                             + "from VisitedPage page where page.blog = :blog group by page.pageUrl");
156             q.setParameter("blog", b);
157
158             // q.setFirstResult(0);
159
// q.setMaxResults(10);
160

161             //TODO make this query cacheable
162
List JavaDoc results = q.list();
163
164             Collections.sort(results, new Comparator JavaDoc() {
165                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
166                     VisitedPage page1 = (VisitedPage) o1;
167                     VisitedPage page2 = (VisitedPage) o2;
168                     return page2.getNumberOfVisitedPages() - page1.getNumberOfVisitedPages();
169                 }
170             });
171
172             // return distinct results
173
return results;
174
175         } catch (HibernateException e) {
176             log.error("getTodayVisitedPages(Blog)", e);
177             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST,
178                     "Visited Pages", e));
179
180         }
181     }
182
183 }
Popular Tags