KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CalendarEntryDAO.java,v 1.6 2005/01/17 21:36:10 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * 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.Calendar JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.List 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
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import com.j2biz.blogunity.exception.BlogunityException;
44 import com.j2biz.blogunity.i18n.I18N;
45 import com.j2biz.blogunity.i18n.I18NStatusFactory;
46 import com.j2biz.blogunity.pojo.Blog;
47 import com.j2biz.blogunity.pojo.CalendarEntry;
48 import com.j2biz.blogunity.util.HibernateUtil;
49
50 public class CalendarEntryDAO extends AbstractDAO {
51
52     private static final Log log = LogFactory.getLog(CalendarEntryDAO.class);
53
54     /**
55      *
56      */

57     public CalendarEntryDAO() {
58         super();
59     }
60
61     public CalendarEntry getCalendarEntryByID(long id) throws BlogunityException {
62         return getCalendarEntryByID(new Long JavaDoc(id));
63     }
64
65     public CalendarEntry getCalendarEntryByID(Long JavaDoc id) throws BlogunityException {
66 // Session session = HibernateUtil.getSession();
67
CalendarEntry ce = null;
68         try {
69 // ce = (CalendarEntry) session.load(CalendarEntry.class, id);
70
ce = (CalendarEntry) getByID(CalendarEntry.class, id);
71         } catch (HibernateException ex) {
72
73             log.error("getCalendarEntryByID(id)", ex);
74             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID,
75                     "CalendarEntry", ex));
76
77         }
78         return ce;
79     }
80
81     public CalendarEntry getCalendarEntryForBlogByDayMonthYear(Blog b, int day, int month, int year)
82             throws BlogunityException {
83         Session session = HibernateUtil.getSession();
84         try {
85
86             Criteria criteria = session.createCriteria(CalendarEntry.class);
87             criteria.add(Expression.eq("blog", b));
88             criteria.add(Expression.eq("day", new Integer JavaDoc(day)));
89             criteria.add(Expression.eq("month", new Integer JavaDoc(month)));
90             criteria.add(Expression.eq("year", new Integer JavaDoc(year)));
91             return (CalendarEntry) criteria.uniqueResult();
92
93         } catch (HibernateException e) {
94             log.error("getCalendarEntryForBlogByDayMonthYear(...)", e);
95             throw new BlogunityException(I18NStatusFactory.create(
96                     I18N.ERRORS.CALENDARENTRY_FOR_DDMMYYYY_NOT_FOUND, new String JavaDoc[]{
97                             String.valueOf(day), String.valueOf(month), String.valueOf(year)}, e));
98
99         }
100     }
101
102     public CalendarEntry getCalendarEntryForBlogByDate(Blog b, Date JavaDoc date) throws BlogunityException {
103         Calendar JavaDoc c = Calendar.getInstance();
104         c.setTime(date);
105
106         return getCalendarEntryForBlogByDayMonthYear(b, c.get(Calendar.DAY_OF_MONTH), c
107                 .get(Calendar.MONTH) + 1, c.get(Calendar.YEAR));
108     }
109
110     public List JavaDoc getCalendarEntryForBlogByMonthYear(Blog b, int month, int year)
111             throws BlogunityException {
112         Session session = HibernateUtil.getSession();
113         try {
114
115             Criteria criteria = session.createCriteria(CalendarEntry.class);
116             criteria.add(Expression.eq("blog", b));
117             criteria.add(Expression.eq("month", new Integer JavaDoc(month)));
118             criteria.add(Expression.eq("year", new Integer JavaDoc(year)));
119             return criteria.list();
120
121         } catch (HibernateException e) {
122             log.error("getCalendarEntryForBlogByMonthYear(...)", e);
123
124             throw new BlogunityException(I18NStatusFactory.create(
125                     I18N.ERRORS.CALENDARENTRY_FOR_MMYYYY_NOT_FOUND, new String JavaDoc[]{
126                             String.valueOf(month), String.valueOf(year)}, e));
127         }
128     }
129
130     public List JavaDoc getCalendarEntryForBlogByYear(Blog b, int year) throws BlogunityException {
131         Session session = HibernateUtil.getSession();
132         try {
133
134             Criteria criteria = session.createCriteria(CalendarEntry.class);
135             criteria.add(Expression.eq("blog", b));
136             criteria.add(Expression.eq("year", new Integer JavaDoc(year)));
137             return criteria.list();
138
139         } catch (HibernateException e) {
140             log.error("getCalendarEntryForBlogByYear(...)", e);
141             throw new BlogunityException(I18NStatusFactory.create(
142                     I18N.ERRORS.CALENDARENTRY_FOR_YYYY_NOT_FOUND,
143                     new String JavaDoc[]{String.valueOf(year)}, e));
144
145         }
146     }
147
148     public Serializable JavaDoc createCalendarEntry(CalendarEntry ce) throws BlogunityException {
149         Session session = HibernateUtil.getSession();
150         try {
151             return session.save(ce);
152         } catch (HibernateException e) {
153             log.error("createCalendarEntry(CalendarEntry)", e);
154             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE,
155                     "CalendarEntry", e));
156         }
157     }
158
159     public void deleteCalendarEntry(CalendarEntry ce) throws BlogunityException {
160         Session session = HibernateUtil.getSession();
161         try {
162
163             session.delete(ce);
164
165         } catch (HibernateException e) {
166             log.error("delete(CalendarEntry(CalendarEntry)", e);
167             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE,
168                     "CalendarEntry", e));
169         }
170     }
171
172     public void updateCalendarEntry(CalendarEntry ce) throws BlogunityException {
173         Session session = HibernateUtil.getSession();
174         try {
175             session.update(ce);
176         } catch (HibernateException e) {
177             log.error("update(CalendarEntry(CalendarEntry)", e);
178             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE,
179                     "CalendarEntry", e));
180         }
181     }
182
183     public void incrementCalendarEntry(CalendarEntry ce) throws BlogunityException {
184         Session session = HibernateUtil.getSession();
185         try {
186             ce.setNumberOfMessages(ce.getNumberOfMessages() + 1);
187             session.saveOrUpdate(ce);
188         } catch (HibernateException e) {
189             log.error("incrementCalendarEntry(CalendarEntry)", e);
190             throw new BlogunityException(I18NStatusFactory.create(
191                     I18N.ERRORS.CALENDARENTRY_INCREMENT, e));
192
193         }
194     }
195
196     public void decrementCalendarEntry(CalendarEntry ce) throws BlogunityException {
197         Session session = HibernateUtil.getSession();
198         try {
199             ce.setNumberOfMessages(ce.getNumberOfMessages() - 1);
200             if (ce.getNumberOfMessages() < 0) ce.setNumberOfMessages(0);
201             session.saveOrUpdate(ce);
202         } catch (HibernateException e) {
203             log.error("decrementCalendarEntry(CalendarEntry)", e);
204             throw new BlogunityException(I18NStatusFactory.create(
205                     I18N.ERRORS.CALENDARENTRY_DECREMENT, e));
206         }
207     }
208
209     public List JavaDoc/* <CalendarArchive> */getArchives(Blog b) {
210         Session session = HibernateUtil.getSession();
211         try {
212             Query query = session
213                     .createQuery("select new com.j2biz.blogunity.pojo.CalendarArchive( entry.month, entry.year, sum(entry.numberOfMessages)) "
214                             + "from CalendarEntry entry group by entry.month, entry.year "
215                             + "where entry.blog = :blog "
216                             + "order by entry.year desc, entry.month desc");
217             query.setParameter("blog", b);
218             query.setFirstResult(0);
219             query.setMaxResults(5);
220             query.setCacheable(true);
221             return query.list();
222
223         } catch (HibernateException e) {
224             log.error("Error fetching archives!", e);
225             return Collections.EMPTY_LIST;
226         }
227     }
228
229     public List JavaDoc/* <CalendarArchive> */getArchives(Long JavaDoc blogId) {
230         Session session = HibernateUtil.getSession();
231         try {
232             Query query = session
233                     .createQuery("select new com.j2biz.blogunity.pojo.CalendarArchive( entry.month, entry.year, sum(entry.numberOfMessages)) "
234                             + "from CalendarEntry entry group by entry.month, entry.year "
235                             + "where entry.blog.id = :blogId "
236                             + "order by entry.year desc, entry.month desc");
237             query.setParameter("blogId", blogId);
238             query.setFirstResult(0);
239             query.setMaxResults(5);
240             query.setCacheable(true);
241             return query.list();
242
243         } catch (HibernateException e) {
244             log.error("Error fetching archives!", e);
245             return Collections.EMPTY_LIST;
246         }
247     }
248
249     public List JavaDoc/* <CalendarArchive> */getYearArchives(Blog b, int year) {
250         Session session = HibernateUtil.getSession();
251         try {
252             Query query = session
253                     .createQuery("select new com.j2biz.blogunity.pojo.CalendarArchive( entry.month, entry.year, sum(entry.numberOfMessages)) "
254                             + "from CalendarEntry entry group by entry.month, entry.year "
255                             + "where entry.blog = :blog and entry.year = :year "
256                             + "order by entry.year asc, entry.month asc");
257             query.setParameter("blog", b);
258             query.setParameter("year", new Integer JavaDoc(year));
259             query.setCacheable(true);
260             return query.list();
261
262         } catch (HibernateException e) {
263             log.error("Error fetching year-archives!", e);
264             return Collections.EMPTY_LIST;
265         }
266     }
267
268     public List JavaDoc/* <CalendarArchive> */getYearArchives(Long JavaDoc blogId, int year) {
269         Session session = HibernateUtil.getSession();
270         try {
271             Query query = session
272                     .createQuery("select new com.j2biz.blogunity.pojo.CalendarArchive( entry.month, entry.year, sum(entry.numberOfMessages)) "
273                             + "from CalendarEntry entry group by entry.month, entry.year "
274                             + "where entry.blog.id = :blogId and entry.year = :year "
275                             + "order by entry.year asc, entry.month asc");
276             query.setParameter("blogId", blogId);
277             query.setParameter("year", new Integer JavaDoc(year));
278             query.setCacheable(true);
279             return query.list();
280
281         } catch (HibernateException e) {
282             log.error("Error fetching year-archives!", e);
283             return Collections.EMPTY_LIST;
284         }
285     }
286
287     public List JavaDoc/* <CalendarArchive> */getYearArchives(String JavaDoc blogUrlname, int year) {
288         Session session = HibernateUtil.getSession();
289         try {
290             Query query = session
291                     .createQuery("select new com.j2biz.blogunity.pojo.CalendarArchive( entry.month, entry.year, sum(entry.numberOfMessages)) "
292                             + "from CalendarEntry entry group by entry.month, entry.year "
293                             + "where entry.blog.urlName = :blogUrlname and entry.year = :year "
294                             + "order by entry.year asc, entry.month asc");
295             query.setString("blogUrlname", blogUrlname);
296             query.setParameter("year", new Integer JavaDoc(year));
297             query.setCacheable(true);
298             return query.list();
299
300         } catch (HibernateException e) {
301             log.error("Error fetching year-archives!", e);
302             return Collections.EMPTY_LIST;
303         }
304     }
305
306 }
Popular Tags