1 24 package org.riotfamily.riot.hibernate.workflow.notification; 25 26 import java.sql.SQLException ; 27 import java.util.Iterator ; 28 import java.util.List ; 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 getNotifications(final String userId) { 45 return getHibernateTemplate().executeFind(new HibernateCallback() { 46 public Object doInHibernate(Session session) 47 throws HibernateException, SQLException { 48 49 String 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 userId, final Long notificationId) { 62 getHibernateTemplate().execute(new HibernateCallback() { 63 public Object doInHibernate(Session session) 64 throws HibernateException, SQLException { 65 66 String 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 doInHibernate(Session session) 84 throws HibernateException, SQLException { 85 86 session.save(n); 87 Criteria c = session.createCriteria(User.class); 88 Iterator 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 |