KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > hibernate > workflow > notification > HibernateNotificationDao


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.hibernate.workflow.notification;
25
26 import java.sql.SQLException JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.hibernate.Criteria;
31 import org.hibernate.HibernateException;
32 import org.hibernate.Query;
33 import org.hibernate.Session;
34 import org.riotfamily.riot.hibernate.security.User;
35 import org.riotfamily.riot.workflow.notification.Notification;
36 import org.riotfamily.riot.workflow.notification.NotificationDao;
37 import org.riotfamily.riot.workflow.notification.support.DefaultNotification;
38 import org.springframework.orm.hibernate3.HibernateCallback;
39 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
40
41 public class HibernateNotificationDao extends HibernateDaoSupport
42         implements NotificationDao {
43
44     public List JavaDoc getNotifications(final String JavaDoc userId) {
45         return getHibernateTemplate().executeFind(new HibernateCallback() {
46             public Object JavaDoc doInHibernate(Session session)
47                     throws HibernateException, SQLException JavaDoc {
48
49                 String JavaDoc hql = "select un.notification from " +
50                         UserNotification.class.getName() + " un "
51                         + "where un.read = false and un.user.id = :userId "
52                         + "order by un.notification.issueDate desc";
53                 
54                 Query query = session.createQuery(hql);
55                 query.setParameter("userId", userId);
56                 return query.list();
57             }
58         });
59     }
60
61     public void markAsRead(final String JavaDoc userId, final Long JavaDoc notificationId) {
62         getHibernateTemplate().execute(new HibernateCallback() {
63             public Object JavaDoc doInHibernate(Session session)
64                     throws HibernateException, SQLException JavaDoc {
65                 
66                 String JavaDoc hql = "update UserNotification un set read = true " +
67                         "where un.user.id = :userId and " +
68                         "un.notification.id = :notificationId";
69                 
70                 Query query = session.createQuery(hql);
71                 query.setParameter("userId", userId);
72                 query.setParameter("notificationId", notificationId);
73                 
74                 query.executeUpdate();
75                 return null;
76             }
77         });
78     }
79         
80     public void saveNotification(Notification notification) {
81         final DefaultNotification n = (DefaultNotification) notification;
82         getHibernateTemplate().execute(new HibernateCallback() {
83             public Object JavaDoc doInHibernate(Session session)
84                     throws HibernateException, SQLException JavaDoc {
85                 
86                 session.save(n);
87                 Criteria c = session.createCriteria(User.class);
88                 Iterator JavaDoc it = c.list().iterator();
89                 while (it.hasNext()) {
90                     User user = (User) it.next();
91                     session.save(new UserNotification(user, n));
92                 }
93                 return null;
94             }
95         });
96     }
97     
98 }
99
Popular Tags