1 25 26 package com.j2biz.blogunity.dao; 27 28 import java.io.Serializable ; 29 import java.util.Date ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Set ; 33 34 import net.sf.hibernate.Criteria; 35 import net.sf.hibernate.HibernateException; 36 import net.sf.hibernate.Query; 37 import net.sf.hibernate.Session; 38 import net.sf.hibernate.expression.Expression; 39 import net.sf.hibernate.expression.MatchMode; 40 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 import com.j2biz.blogunity.exception.BlogunityException; 45 import com.j2biz.blogunity.i18n.I18N; 46 import com.j2biz.blogunity.i18n.I18NStatusFactory; 47 import com.j2biz.blogunity.pojo.Blog; 48 import com.j2biz.blogunity.pojo.User; 49 import com.j2biz.blogunity.util.HibernateUtil; 50 51 58 public class BlogDAO extends AbstractDAO { 59 60 private static final Log log = LogFactory.getLog(BlogDAO.class); 61 62 public BlogDAO() { 63 super(); 64 } 65 66 public Blog getBlogByID(long id) throws BlogunityException { 67 return getBlogByID(new Long (id)); 68 } 69 70 public Blog getBlogByID(Long id) throws BlogunityException { 71 Blog blog = null; 72 try { 73 74 blog = (Blog) getByID(Blog.class, id); 75 76 } catch (HibernateException ex) { 77 log.error("getBlogByID(id)", ex); 78 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID, 79 "Blog", ex)); 80 } 81 return blog; 82 } 83 84 public Blog getBlogByUrlName(String urlName) throws BlogunityException { 85 Session session = HibernateUtil.getSession(); 86 Blog blog = null; 87 try { 88 89 Query q = session.createQuery("from Blog blog where blog.urlName = :urlName"); 90 q.setString("urlName", urlName); 91 q.setFetchSize(1); 92 93 return (Blog) q.uniqueResult(); 94 95 } catch (HibernateException ex) { 96 log.error("getBlogByName(name)", ex); 97 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_NAME, 98 "Blog", ex)); 99 } 100 101 } 102 103 public Serializable createBlog(Blog blog) throws BlogunityException { 104 try { 105 return create(blog); 106 } catch (HibernateException e) { 107 log.error("createBlog(blog)", e); 108 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE, "Blog", e)); 109 } 110 } 111 112 public void deleteBlog(Blog blog) throws BlogunityException { 113 try { 115 116 119 blog.getCategories().removeAll(blog.getGlobalCategories()); 120 121 if (blog.getType() == Blog.COMMUNITY_BLOG) { 122 125 Set contributors = blog.getContributors(); 126 for (Iterator i = contributors.iterator(); i.hasNext();) { 127 User u = (User) i.next(); 128 u.getContributedBlogs().remove(blog); 129 } 130 contributors.clear(); 131 132 136 Set waitingForAcceptanceUsers = blog.getWaitingForAcceptanceUsers(); 137 for (Iterator i = waitingForAcceptanceUsers.iterator(); i.hasNext();) { 138 User u = (User) i.next(); 139 u.getWaitingForAcceptanceBlogs().remove(blog); 140 } 141 waitingForAcceptanceUsers.clear(); 142 } 143 144 147 delete(blog); 148 150 } catch (HibernateException e) { 151 log.error("deleteBlog(blog)", e); 152 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, "Blog", e)); 153 } 154 } 155 156 public void updateBlog(Blog blog) throws BlogunityException { 157 Session session = HibernateUtil.getSession(); 158 try { 159 blog.setLastModified(new Date ()); 160 session.update(blog); 161 } catch (HibernateException e) { 162 log.error("updateBlog(blog)", e); 163 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE, "Blog", e)); 164 } 165 } 166 167 public List getLastRegisteredBlogs(int count) throws BlogunityException { 168 169 Session session = HibernateUtil.getSession(); 170 try { 171 Query q = session.createQuery("from Blog blog order by blog.createTime desc"); 172 q.setFirstResult(0); 173 q.setMaxResults(count); 174 175 return q.list(); 176 } catch (HibernateException e) { 177 log.error("getLastRegisteredBlogs(count)", e); 178 throw new BlogunityException(I18NStatusFactory.create( 179 I18N.ERRORS.BLOG_FIND_LAST_REGISTERED, e)); 180 } 181 } 182 183 public List getAllBlogs() throws BlogunityException { 184 185 Session session = HibernateUtil.getSession(); 186 try { 187 Query q = session.createQuery("from Blog blog order by blog.createTime desc"); 188 return q.list(); 189 } catch (HibernateException e) { 190 log.error("getAllBlogs()", e); 191 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.BLOG_FIND_ALL, e)); 192 } 193 } 194 195 public List getBlogsWithNameLike(String likeBlogname) throws BlogunityException { 196 Session session = HibernateUtil.getSession(); 197 try { 198 199 Criteria criteria = session.createCriteria(Blog.class); 200 criteria.add(Expression.like("urlName", likeBlogname, MatchMode.ANYWHERE)); 201 return criteria.list(); 202 203 } catch (HibernateException e) { 204 log.error("getBlogsWithNameLike(name)", e); 205 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.LIKE_NAME_NOT_FOUND, 206 new String []{"Blog", likeBlogname}, e)); 207 } 208 } 209 } | Popular Tags |