1 25 26 package com.j2biz.blogunity.dao; 27 28 import java.io.Serializable ; 29 import java.util.Iterator ; 30 import java.util.List ; 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 56 public class CommentDAO extends AbstractDAO { 57 58 private static final Log log = LogFactory.getLog(CommentDAO.class); 59 60 63 public CommentDAO() { 64 super(); 65 } 66 67 public Comment getCommentByID(long id) throws BlogunityException { 68 return getCommentByID(new Long (id)); 69 } 70 71 public Comment getCommentByID(Long 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 createComment(Comment comment) throws BlogunityException { 85 86 Session session = HibernateUtil.getSession(); 87 try { 88 Serializable 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 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 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 getPaginatedComments(Long 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 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 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 |