KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EntryDAO.java,v 1.12 2005/01/17 21:36:10 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.Calendar JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import net.sf.hibernate.Criteria;
36 import net.sf.hibernate.HibernateException;
37 import net.sf.hibernate.Query;
38 import net.sf.hibernate.Session;
39 import net.sf.hibernate.expression.Expression;
40 import net.sf.hibernate.expression.Order;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 import com.j2biz.blogunity.exception.BlogunityException;
46 import com.j2biz.blogunity.i18n.I18N;
47 import com.j2biz.blogunity.i18n.I18NStatusFactory;
48 import com.j2biz.blogunity.pojo.Blog;
49 import com.j2biz.blogunity.pojo.CalendarEntry;
50 import com.j2biz.blogunity.pojo.Entry;
51 import com.j2biz.blogunity.pojo.User;
52 import com.j2biz.blogunity.pojo.Userpic;
53 import com.j2biz.blogunity.util.HibernateUtil;
54
55 /**
56  * @author michelson
57  * @version $$
58  * @since 0.1
59  *
60  *
61  */

62 public class EntryDAO extends AbstractDAO {
63     /**
64      * Logger for this class
65      */

66     private static final Log log = LogFactory.getLog(EntryDAO.class);
67
68     /**
69      *
70      */

71     public EntryDAO() {
72         super();
73     }
74
75     public Entry getEntryByID(long id) throws BlogunityException {
76         return getEntryByID(new Long JavaDoc(id));
77     }
78
79     public Entry getEntryByID(Long JavaDoc id) throws BlogunityException {
80
81         Session session = HibernateUtil.getSession();
82         Entry entry = null;
83         try {
84             entry = (Entry) session.load(Entry.class, id);
85         } catch (HibernateException ex) {
86
87             log.error("getEntryByID(Long)", ex);
88             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID,
89                     "Entry", ex));
90         }
91
92         return entry;
93     }
94
95     public List JavaDoc getEntiresByUserpic(Userpic pic) throws BlogunityException {
96         Session session = HibernateUtil.getSession();
97         try {
98
99             Criteria criteria = session.createCriteria(Entry.class);
100             criteria.add(Expression.eq("userpic", pic));
101             return criteria.list();
102
103         } catch (HibernateException e) {
104             log.error("getPaginatedEntriesByCategory(String, Long, int, int)", e);
105             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_BY_USERPIC,
106                     "Entry", e));
107         }
108     }
109
110     public List JavaDoc getPaginatedEntriesByCategory(Long JavaDoc blogId, Long JavaDoc categoryId, int firstResult,
111             int maxResults) throws BlogunityException {
112
113         Session session = HibernateUtil.getSession();
114
115         try {
116
117             Criteria criteria = session.createCriteria(Entry.class);
118             criteria.createCriteria("categories").add(Expression.eq("id", categoryId));
119             criteria.createCriteria("blog").add(Expression.eq("id", blogId));
120             criteria.setFirstResult(firstResult);
121             criteria.setMaxResults(maxResults);
122             criteria.setCacheable(true);
123             return criteria.list();
124
125         } catch (HibernateException e) {
126             log.error("getPaginatedEntriesByCategory(String, Long, int, int)", e);
127             throw new BlogunityException(I18NStatusFactory.create(
128                     I18N.ERRORS.FETCH_PAGINATED_LIST_BY_CATEGORY, e));
129         }
130     }
131
132     public List JavaDoc getPaginatedEntriesByCategory(String JavaDoc blogUrlname, Long JavaDoc categoryId, int firstResult,
133             int maxResults) throws BlogunityException {
134
135         Session session = HibernateUtil.getSession();
136
137         try {
138
139             Criteria criteria = session.createCriteria(Entry.class);
140             criteria.createCriteria("categories").add(Expression.eq("id", categoryId));
141             criteria.createCriteria("blog").add(Expression.eq("urlName", blogUrlname));
142             criteria.setFirstResult(firstResult);
143             criteria.setMaxResults(maxResults);
144             criteria.setCacheable(true);
145             return criteria.list();
146
147         } catch (HibernateException e) {
148             log.error("getPaginatedEntriesByCategory(String, Long, int, int)", e);
149             throw new BlogunityException(I18NStatusFactory.create(
150                     I18N.ERRORS.FETCH_PAGINATED_LIST_BY_CATEGORY, e));
151         }
152     }
153
154     public List JavaDoc getPaginatedEntries(String JavaDoc urlname, int firstResult, int maxResults)
155             throws BlogunityException {
156
157         Session session = HibernateUtil.getSession();
158
159         try {
160
161             Query q = session
162                     .createQuery("from Entry entry where entry.blog.urlName = :blogName order by entry.createTime desc");
163             q.setString("blogName", urlname);
164             q.setFirstResult(firstResult);
165             q.setMaxResults(maxResults);
166
167             return q.list();
168
169         } catch (HibernateException e) {
170             log.error("getPaginatedEntries(String, int, int)", e);
171             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST,
172                     "Entries", e));
173         }
174     }
175
176     /**
177      * Returns last 10 entries for the given blog.
178      *
179      * @param urlname
180      * @param firstResult
181      * @param maxResults
182      * @return
183      * @throws BlogunityException
184      */

185     public List JavaDoc getRecentBlogEntries(Blog b) throws BlogunityException {
186         return getPaginatedEntries(b.getId(), 0, 10);
187     }
188
189     /**
190      * @param blogId
191      * @param firstResult
192      * @param maxResults
193      * @return
194      * @throws BlogunityException
195      */

196     public List JavaDoc getPaginatedEntries(Long JavaDoc blogId, int firstResult, int maxResults)
197             throws BlogunityException {
198
199         Session session = HibernateUtil.getSession();
200
201         try {
202
203             Query q = session
204                     .createQuery("from Entry entry where entry.blog.id = :blogId order by entry.createTime desc");
205             q.setLong("blogId", blogId.longValue());
206             q.setFirstResult(firstResult);
207             q.setMaxResults(maxResults);
208             return q.list();
209
210         } catch (HibernateException e) {
211             log.error("getPaginatedEntries(Long, int, int)", e);
212             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST,
213                     "Entries", e));
214         }
215     }
216
217     public Entry getEntryByIDorAliasname(String JavaDoc param, String JavaDoc blogname) throws BlogunityException {
218         Session session = HibernateUtil.getSession();
219
220         try {
221
222             Query q = session
223                     .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.aliasname=:aliasParam or entry.id=:idParam");
224             q.setString("blogName", blogname);
225             q.setString("aliasParam", param);
226
227             long id;
228             try {
229                 id = Long.parseLong(param);
230             } catch (NumberFormatException JavaDoc e) {
231                 id = -1;
232             }
233
234             q.setLong("idParam", id);
235
236             return (Entry) q.uniqueResult();
237
238         } catch (HibernateException e) {
239             log.error("getPaginatedEntries(Long, int, int)", e);
240             throw new BlogunityException(I18NStatusFactory.create(
241                     I18N.ERRORS.FETCH_ENTRY_BY_ALIASNAME, "Entry", e));
242         }
243     }
244
245     public Entry getEntryByAliasname(String JavaDoc alias, String JavaDoc blogname) throws BlogunityException {
246         Session session = HibernateUtil.getSession();
247
248         try {
249
250             Query q = session
251                     .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.aliasname=:aliasName");
252             q.setString("blogName", blogname);
253             q.setString("aliasName", alias);
254             return (Entry) q.uniqueResult();
255
256         } catch (HibernateException e) {
257             log.error("getPaginatedEntries(Long, int, int)", e);
258             throw new BlogunityException(I18NStatusFactory.create(
259                     I18N.ERRORS.FETCH_ENTRY_BY_ALIASNAME, "Entries", e));
260         }
261     }
262
263     public List JavaDoc getPaginatedFriendsTape(User user, int firstResult, int maxResults)
264             throws BlogunityException {
265
266         Session session = HibernateUtil.getSession();
267
268         try {
269
270             Criteria c = session.createCriteria(Entry.class);
271
272             Set JavaDoc friends = user.getFriends();
273
274             if (friends.size() == 0) return Collections.EMPTY_LIST;
275
276             c.add(Expression.in("author", friends));
277             c.addOrder(Order.desc("createTime"));
278             c.setFirstResult(firstResult);
279             c.setMaxResults(maxResults);
280             c.setCacheable(true);
281             return c.list();
282
283         } catch (HibernateException e) {
284             log.error("getPaginatedFriendsTape(User, int, int)", e);
285             throw new BlogunityException(I18NStatusFactory.create(
286                     I18N.ERRORS.FETCH_PAGINATED_FRIENDS_TAPE, e));
287
288         }
289     }
290
291     public List JavaDoc getPaginatedFavoriteBlogsTape(User user, int firstResult, int maxResults)
292             throws BlogunityException {
293
294         Session session = HibernateUtil.getSession();
295
296         try {
297
298             Criteria c = session.createCriteria(Entry.class);
299
300             Set JavaDoc blogs = user.getFavoriteBlogs();
301
302             if (blogs.size() == 0) return Collections.EMPTY_LIST;
303
304             c.add(Expression.in("blog", blogs));
305             c.addOrder(Order.desc("createTime"));
306             c.setFirstResult(firstResult);
307             c.setMaxResults(maxResults);
308             c.setCacheable(true);
309             return c.list();
310
311         } catch (HibernateException e) {
312             log.error("getPaginatedFavoriteBlogsTape(User, int, int)", e);
313             throw new BlogunityException(I18NStatusFactory.create(
314                     I18N.ERRORS.FETCH_PAGINATED_FAVORITES_TAPE, e));
315         }
316     }
317
318     public List JavaDoc getPaginatedCommunityBlogsTape(User user, int firstResult, int maxResults)
319             throws BlogunityException {
320
321         Session session = HibernateUtil.getSession();
322
323         try {
324
325             Criteria c = session.createCriteria(Entry.class);
326
327             Set JavaDoc blogs = user.getContributedBlogs();
328
329             if (blogs.size() == 0) return Collections.EMPTY_LIST;
330
331             c.add(Expression.in("blog", blogs));
332             c.addOrder(Order.desc("createTime"));
333             c.setFirstResult(firstResult);
334             c.setMaxResults(maxResults);
335             c.setCacheable(true);
336
337             return c.list();
338
339         } catch (HibernateException e) {
340             log.error("getPaginatedFavoriteBlogsTape(User, int, int)", e);
341             throw new BlogunityException(I18NStatusFactory.create(
342                     I18N.ERRORS.FETCH_PAGINATED_COMMUNITY_BLOGS_TAPE, e));
343         }
344     }
345
346     public List JavaDoc getPaginatedFoundedBlogsTape(User user, int firstResult, int maxResults)
347             throws BlogunityException {
348
349         Session session = HibernateUtil.getSession();
350
351         try {
352
353             Criteria c = session.createCriteria(Entry.class);
354
355             Set JavaDoc blogs = user.getFoundedBlogs();
356
357             if (blogs.size() == 0) return Collections.EMPTY_LIST;
358
359             c.add(Expression.in("blog", blogs));
360             c.addOrder(Order.desc("createTime"));
361             c.setFirstResult(firstResult);
362             c.setMaxResults(maxResults);
363             c.setCacheable(true);
364             return c.list();
365
366         } catch (HibernateException e) {
367             log.error("getPaginatedFoundedBlogsTape(User, int, int)", e);
368             throw new BlogunityException(I18NStatusFactory.create(
369                     I18N.ERRORS.FETCH_PAGINATED_FOUNDED_BLOGS_TAPE, e));
370         }
371     }
372
373     public List JavaDoc getEntriesForDay(String JavaDoc blogName, int year, int month, int day)
374             throws BlogunityException {
375
376         Calendar JavaDoc dayObj = Calendar.getInstance();
377         dayObj.set(year, month, day, 0, 0, 0);
378
379         return geEntriesForDay(blogName, dayObj);
380
381     }
382
383     public List JavaDoc geEntriesForDay(String JavaDoc blogName, Calendar JavaDoc day) throws BlogunityException {
384
385         Session session = HibernateUtil.getSession();
386
387         try {
388
389             Calendar JavaDoc dayEnd = Calendar.getInstance();
390             dayEnd.setTimeInMillis(day.getTimeInMillis());
391             dayEnd.add(Calendar.DATE, 1);
392
393             Query q = session
394                     .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.createTime between :begin and :end order by entry.createTime desc");
395             q.setString("blogName", blogName);
396             q.setDate("begin", day.getTime());
397             q.setDate("end", dayEnd.getTime());
398             q.setCacheable(true);
399
400             List JavaDoc returnList = q.list();
401             if (log.isDebugEnabled()) {
402                 log.debug("getBlogEntriesForDay(String, Calendar) - end");
403             }
404             return returnList;
405
406         } catch (HibernateException e) {
407             log.error("getBlogEntriesForDay(String, Calendar)", e);
408             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ENTRY_BY_DAY, e));
409         }
410
411     }
412
413     public List JavaDoc getEntriesForMonth(String JavaDoc blogName, int year, int month) throws BlogunityException {
414
415         Calendar JavaDoc monthObj = Calendar.getInstance();
416         monthObj.set(year, month, 1, 0, 0, 0);
417
418         return getEntriesForMonth(blogName, monthObj);
419
420     }
421
422     public List JavaDoc getEntriesForMonth(String JavaDoc blogName, Calendar JavaDoc month) throws BlogunityException {
423
424         Session session = HibernateUtil.getSession();
425         try {
426
427             Calendar JavaDoc monthEnd = Calendar.getInstance();
428             monthEnd.setTimeInMillis(month.getTimeInMillis());
429             monthEnd.add(Calendar.MONTH, 1);
430
431             Query q = session
432                     .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.createTime between :begin and :end order by entry.createTime desc");
433             q.setString("blogName", blogName);
434             q.setDate("begin", month.getTime());
435             q.setDate("end", monthEnd.getTime());
436             q.setCacheable(true);
437
438             return q.list();
439
440         } catch (HibernateException e) {
441             log.error("getBlogEntriesForMonth(String, Calendar)", e);
442             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ENTRY_BY_MONTH, e));
443         }
444     }
445
446     public List JavaDoc getEntriesForYear(String JavaDoc blogName, int year) throws BlogunityException {
447         Calendar JavaDoc yearObj = Calendar.getInstance();
448         yearObj.set(year, 0, 1, 0, 0, 0);
449
450         return getEntriesForYear(blogName, yearObj);
451
452     }
453
454     public List JavaDoc getEntriesForYear(String JavaDoc blogName, Calendar JavaDoc year) throws BlogunityException {
455
456         Session session = HibernateUtil.getSession();
457
458         try {
459
460             Calendar JavaDoc yearEnd = Calendar.getInstance();
461             yearEnd.setTimeInMillis(year.getTimeInMillis());
462             yearEnd.add(Calendar.YEAR, 1);
463
464             Query q = session
465                     .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.createTime between :begin and :end order by entry.createTime desc");
466             q.setString("blogName", blogName);
467             q.setDate("begin", year.getTime());
468             q.setDate("end", yearEnd.getTime());
469             q.setCacheable(true);
470
471             return q.list();
472
473         } catch (HibernateException e) {
474             log.error("getBlogEntriesForYear(String, Calendar)", e);
475             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ENTRY_BY_YEAR, e));
476         }
477     }
478
479     public Serializable JavaDoc createEntry(Entry entry) throws BlogunityException {
480
481         Session session = HibernateUtil.getSession();
482         try {
483             Serializable JavaDoc result = session.save(entry);
484
485             // increment calendar
486
Date JavaDoc d = entry.getCreateTime();
487
488             CalendarEntryDAO calendarDAO = new CalendarEntryDAO();
489             CalendarEntry ce = null;
490
491             ce = calendarDAO.getCalendarEntryForBlogByDate(entry.getBlog(), d);
492             if (ce == null) {
493                 ce = new CalendarEntry();
494                 ce.setDate(d);
495                 ce.setBlog(entry.getBlog());
496             }
497
498             calendarDAO.incrementCalendarEntry(ce);
499
500             // update blog's lastModified-Date [BGU-10]
501
BlogDAO blogDAO = new BlogDAO();
502             Blog b = entry.getBlog();
503             b.setLastModified(new Date JavaDoc());
504             blogDAO.updateBlog(b);
505
506             return result;
507         } catch (HibernateException e) {
508             log.error("createEntry(Entry)", e);
509             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE, "Entry", e));
510         }
511     }
512
513     public void deleteEntry(Entry entry) throws BlogunityException {
514
515         Session session = HibernateUtil.getSession();
516         try {
517
518             session.delete(entry);
519
520             //decrement calendar
521
Date JavaDoc d = entry.getCreateTime();
522
523             CalendarEntryDAO calendarDAO = new CalendarEntryDAO();
524             CalendarEntry ce = calendarDAO.getCalendarEntryForBlogByDate(entry.getBlog(), d);
525
526             if (ce != null) {
527                 if (ce.getNumberOfMessages() > 1) {
528                     calendarDAO.decrementCalendarEntry(ce);
529                 } else {
530                     calendarDAO.deleteCalendarEntry(ce);
531                 }
532             }
533
534             // update blog's lastModified-Date [BGU-10]
535
BlogDAO blogDAO = new BlogDAO();
536             Blog b = entry.getBlog();
537             b.setLastModified(new Date JavaDoc());
538             blogDAO.updateBlog(b);
539
540         } catch (HibernateException e) {
541             log.error("deleteEntry(Entry)", e);
542             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, "Entry", e));
543         }
544
545     }
546
547     public void updateEntry(Entry entry) throws BlogunityException {
548
549         Session session = HibernateUtil.getSession();
550         try {
551             session.update(entry);
552
553             // update blog's lastModified-Date [BGU-10]
554
BlogDAO blogDAO = new BlogDAO();
555             Blog b = entry.getBlog();
556             b.setLastModified(new Date JavaDoc());
557             blogDAO.updateBlog(b);
558
559         } catch (HibernateException e) {
560             log.error("updateEntry(Entry)", e);
561             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE, "Entry", e));
562         }
563
564     }
565
566     public List JavaDoc getLastPostedEntries(int count) throws BlogunityException {
567
568         Session session = HibernateUtil.getSession();
569         try {
570             Query q = session.createQuery("from Entry e order by e.createTime desc");
571             q.setFirstResult(0);
572             q.setMaxResults(count);
573
574             return q.list();
575         } catch (HibernateException e) {
576             log.error("getLastPostedEntries(int)", e);
577             throw new BlogunityException(I18NStatusFactory.create(
578                     I18N.ERRORS.ENTRY_LAST_POSTED_LIST, e));
579         }
580     }
581
582     /**
583      * Optimized query for fetching total number of entries for given blog.
584      *
585      * @param b
586      * @return
587      * @throws BlogException
588      */

589     public int getNumberOfEntries(Blog b) throws BlogunityException {
590
591         Session session = HibernateUtil.getSession();
592         try {
593             return ((Integer JavaDoc) session.createFilter(b.getEntries(), "select count(*)").iterate()
594                     .next()).intValue();
595         } catch (HibernateException e) {
596             log.error("getNumberOfEntries(Blog)", e);
597             throw new BlogunityException(I18NStatusFactory
598                     .create(I18N.ERRORS.ENTRY_TOTAL_NUMBER, e));
599         }
600     }
601
602 }
Popular Tags