KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: BlogDAO.java,v 1.9 2005/01/17 21:36:11 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.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
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 /**
52  * @author michelson
53  * @version $$
54  * @since 0.1
55  *
56  *
57  */

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 JavaDoc(id));
68     }
69
70     public Blog getBlogByID(Long JavaDoc 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 JavaDoc 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 JavaDoc 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 // Session session = HibernateUtil.getSession();
114
try {
115
116             /*
117              * 1. delete references to global categories
118              */

119             blog.getCategories().removeAll(blog.getGlobalCategories());
120
121             if (blog.getType() == Blog.COMMUNITY_BLOG) {
122                 /*
123                  * 2. delete references to contributors
124                  */

125                 Set JavaDoc contributors = blog.getContributors();
126                 for (Iterator JavaDoc i = contributors.iterator(); i.hasNext();) {
127                     User u = (User) i.next();
128                     u.getContributedBlogs().remove(blog);
129                 }
130                 contributors.clear();
131
132                 /*
133                  *
134                  * 3. delete references to users waiting for acceptance
135                  */

136                 Set JavaDoc waitingForAcceptanceUsers = blog.getWaitingForAcceptanceUsers();
137                 for (Iterator JavaDoc i = waitingForAcceptanceUsers.iterator(); i.hasNext();) {
138                     User u = (User) i.next();
139                     u.getWaitingForAcceptanceBlogs().remove(blog);
140                 }
141                 waitingForAcceptanceUsers.clear();
142             }
143
144             /*
145              * 4. delete blog itself
146              */

147             delete(blog);
148 // session.delete(blog);
149

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 JavaDoc());
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 JavaDoc 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 JavaDoc 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 JavaDoc getBlogsWithNameLike(String JavaDoc 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 JavaDoc[]{"Blog", likeBlogname}, e));
207         }
208     }
209 }
Popular Tags