KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CommentDAO.java,v 1.7 2005/01/17 21:36:10 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple 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.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import net.sf.hibernate.Criteria;
33 import net.sf.hibernate.HibernateException;
34 import net.sf.hibernate.Query;
35 import net.sf.hibernate.Session;
36 import net.sf.hibernate.expression.Expression;
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.Comment;
46 import com.j2biz.blogunity.pojo.Userpic;
47 import com.j2biz.blogunity.util.HibernateUtil;
48
49 /**
50  * @author michelson
51  * @version $$
52  * @since 0.1
53  *
54  *
55  */

56 public class CommentDAO extends AbstractDAO {
57
58     private static final Log log = LogFactory.getLog(CommentDAO.class);
59
60     /**
61      *
62      */

63     public CommentDAO() {
64         super();
65     }
66
67     public Comment getCommentByID(long id) throws BlogunityException {
68         return getCommentByID(new Long JavaDoc(id));
69     }
70
71     public Comment getCommentByID(Long JavaDoc id) throws BlogunityException {
72         Session session = HibernateUtil.getSession();
73         Comment comment = null;
74         try {
75             comment = (Comment) session.load(Comment.class, id);
76         } catch (HibernateException ex) {
77             log.error("getCommentByID(id)", ex);
78             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID,
79                     "Comment", ex));
80         }
81         return comment;
82     }
83
84     public Serializable JavaDoc createComment(Comment comment) throws BlogunityException {
85
86         Session session = HibernateUtil.getSession();
87         try {
88             Serializable JavaDoc returnSerializable = session.save(comment);
89             return returnSerializable;
90         } catch (HibernateException e) {
91             log.error("createComment(Comment)", e);
92             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE, "Comment", e));
93         }
94     }
95
96     public void deleteComment(Comment comment) throws BlogunityException {
97
98         Session session = HibernateUtil.getSession();
99         try {
100             session.delete(comment);
101         } catch (HibernateException e) {
102
103             log.error("deleteComment(Comment)", e);
104             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, "Comment", e));
105
106         }
107
108     }
109
110     public void deleteCommentWithAllSubComments(Comment comment) throws BlogunityException {
111
112         Session session = HibernateUtil.getSession();
113         try {
114
115             simpleDeleteComment(comment, session);
116
117         } catch (HibernateException e) {
118             log.error("deleteCommentWithAllSubComments(Comment)", e);
119             throw new BlogunityException(I18NStatusFactory.create(
120                     I18N.ERRORS.COMMENT_DELETE_RECURSIVE, e));
121
122         }
123
124     }
125
126     private void simpleDeleteComment(Comment c, Session session) throws HibernateException {
127
128         for (Iterator JavaDoc io = c.getAnswers().iterator(); io.hasNext();) {
129             Comment c1 = (Comment) io.next();
130             simpleDeleteComment(c1, session);
131         }
132
133         session.delete(c);
134
135     }
136
137     public void updateComment(Comment comment) throws BlogunityException {
138
139         Session session = HibernateUtil.getSession();
140         try {
141             session.update(comment);
142         } catch (HibernateException e) {
143             log.error("updateComment(Comment)", e);
144             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE, "Comment", e));
145         }
146
147     }
148
149     public List JavaDoc getCommentsByUserpic(Userpic pic) throws BlogunityException {
150         Session session = HibernateUtil.getSession();
151         try {
152
153             Criteria criteria = session.createCriteria(Comment.class);
154             criteria.add(Expression.eq("userpic", pic));
155             return criteria.list();
156
157         } catch (HibernateException e) {
158             log.error("getCommentsByUserpic(Userpic)", e);
159             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_BY_USERPIC,
160                     "Comment", e));
161         }
162     }
163
164     public List JavaDoc getPaginatedComments(Long JavaDoc entryId, int firstResult, int maxResults)
165             throws BlogunityException {
166
167         Session session = HibernateUtil.getSession();
168
169         try {
170
171             Query q = session
172                     .createQuery("from Comment comment where comment.commentedEntry.id = :entryId order by comment.createTime desc");
173             q.setLong("entryId", entryId.longValue());
174             q.setFirstResult(firstResult);
175             q.setMaxResults(maxResults);
176             return q.list();
177
178         } catch (HibernateException e) {
179             log.error("getPaginatedEntries(Long, int, int)", e);
180             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST,
181                     "Comment", e));
182
183         }
184     }
185
186     public List JavaDoc getRecentBlogComments(Blog b) throws BlogunityException {
187         Session session = HibernateUtil.getSession();
188         try {
189             Query q = session
190                     .createQuery("from Comment comment where comment.commentedEntry.blog.id = :blogId order by comment.createTime desc");
191             q.setLong("blogId", b.getId().longValue());
192             q.setFirstResult(0);
193             q.setMaxResults(10);
194
195             //TODO make this query cacheable
196
return q.list();
197
198         } catch (HibernateException e) {
199             log.error("getRecentBlogComments(Blog)", e);
200             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST,
201                     "Comment", e));
202
203         }
204     }
205
206 }
Popular Tags