KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CategoryDAO.java,v 1.6 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.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.Category;
45 import com.j2biz.blogunity.util.HibernateUtil;
46
47 /**
48  * @author michelson
49  * @version $$
50  * @since 0.1
51  *
52  *
53  */

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

61     public CategoryDAO() {
62         super();
63     }
64
65     /**
66      * @param catId
67      * @return
68      * @throws BlogunityException
69      */

70     public Category getCategoryByID(long catId) throws BlogunityException {
71         return getCategoryByID(new Long JavaDoc(catId));
72     }
73
74     /**
75      * @param catId
76      * @return
77      * @throws BlogunityException
78      */

79     public Category getCategoryByID(Long JavaDoc catId) throws BlogunityException {
80         Session session = HibernateUtil.getSession();
81         Category blogCategory = null;
82         try {
83
84             Criteria criteria = session.createCriteria(Category.class).add(
85                     Expression.eq("id", catId));
86
87             return (Category) criteria.uniqueResult();
88
89         } catch (HibernateException ex) {
90             log.error("getCategoryByID(id)", ex);
91             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID,
92                     new String JavaDoc[]{"Category"}, ex));
93         }
94     }
95
96     /**
97      * @return
98      * @throws BlogunityException
99      */

100     public List JavaDoc getGlobalCategories() throws BlogunityException {
101         Session session = HibernateUtil.getSession();
102         List JavaDoc categories;
103         try {
104
105             Query q = session.createQuery("from Category c where c.type = :type");
106             q.setInteger("type", Category.GLOBAL);
107
108             return q.list();
109
110         } catch (HibernateException ex) {
111
112             log.error("getGlobalCategories()", ex);
113             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST,
114                     new String JavaDoc[]{"Categories"}, ex));
115
116         }
117     }
118
119     /**
120      * @param name
121      * @return
122      */

123     public boolean existsGlobalCategoryWithName(String JavaDoc name) {
124
125         List JavaDoc categories;
126         try {
127             categories = getGlobalCategories();
128         } catch (BlogunityException e) {
129             return false;
130         }
131
132         for (Iterator JavaDoc i = categories.iterator(); i.hasNext();) {
133             Category c = (Category) i.next();
134             if (c.getName().equals(name)) return true;
135         }
136
137         return false;
138     }
139
140     /**
141      * @param name
142      * @return
143      * @throws BlogunityException
144      */

145     public Category getGlobalCategoryByName(String JavaDoc name) throws BlogunityException {
146
147         Session session = HibernateUtil.getSession();
148         try {
149             // TODO mark this query cacheable?
150
// TODO check if user isActive!
151
Criteria criteria = session.createCriteria(Category.class);
152             criteria.add(Expression.eq("type", new Integer JavaDoc(Category.GLOBAL)));
153             criteria.add(Expression.eq("name", name));
154
155             return (Category) criteria.uniqueResult();
156
157         } catch (HibernateException ex) {
158             log.error("getCategoryByName(String)", ex);
159             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_NAME,
160                     new String JavaDoc[]{"Category"}, ex));
161
162         }
163
164     }
165
166     /**
167      * @param category
168      * @return
169      * @throws BlogunityException
170      */

171     public Serializable JavaDoc createCategory(Category category) throws BlogunityException {
172         Session session = HibernateUtil.getSession();
173         try {
174             return session.save(category);
175         } catch (HibernateException e) {
176
177             log.error("createCategory(category)", e);
178             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE,
179                     new String JavaDoc[]{"Category"}, e));
180
181         }
182     }
183
184     /**
185      * @param category
186      * @throws BlogunityException
187      */

188     public void deleteCategory(Category category) throws BlogunityException {
189         Session session = HibernateUtil.getSession();
190         try {
191             session.delete(category);
192         } catch (HibernateException e) {
193
194             log.error("deleteCategory(category)", e);
195             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE,
196                     new String JavaDoc[]{"Category"}, e));
197
198         }
199     }
200
201     /**
202      * @param category
203      * @throws BlogunityException
204      */

205     public void updateCategory(Category category) throws BlogunityException {
206         Session session = HibernateUtil.getSession();
207         try {
208             session.update(category);
209         } catch (HibernateException e) {
210
211             log.error("updateCategory(category)", e);
212             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE,
213                     new String JavaDoc[]{"Category"}, e));
214         }
215     }
216 }
Popular Tags