001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.riot.hibernate.workflow.notification;
025:
026: import java.sql.SQLException;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.hibernate.Criteria;
031: import org.hibernate.HibernateException;
032: import org.hibernate.Query;
033: import org.hibernate.Session;
034: import org.riotfamily.riot.hibernate.security.User;
035: import org.riotfamily.riot.workflow.notification.Notification;
036: import org.riotfamily.riot.workflow.notification.NotificationDao;
037: import org.riotfamily.riot.workflow.notification.support.DefaultNotification;
038: import org.springframework.orm.hibernate3.HibernateCallback;
039: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
040:
041: public class HibernateNotificationDao extends HibernateDaoSupport
042: implements NotificationDao {
043:
044: public List getNotifications(final String userId) {
045: return getHibernateTemplate().executeFind(
046: new HibernateCallback() {
047: public Object doInHibernate(Session session)
048: throws HibernateException, SQLException {
049:
050: String hql = "select un.notification from "
051: + UserNotification.class.getName()
052: + " un "
053: + "where un.read = false and un.user.id = :userId "
054: + "order by un.notification.issueDate desc";
055:
056: Query query = session.createQuery(hql);
057: query.setParameter("userId", userId);
058: return query.list();
059: }
060: });
061: }
062:
063: public void markAsRead(final String userId,
064: final Long notificationId) {
065: getHibernateTemplate().execute(new HibernateCallback() {
066: public Object doInHibernate(Session session)
067: throws HibernateException, SQLException {
068:
069: String hql = "update UserNotification un set read = true "
070: + "where un.user.id = :userId and "
071: + "un.notification.id = :notificationId";
072:
073: Query query = session.createQuery(hql);
074: query.setParameter("userId", userId);
075: query.setParameter("notificationId", notificationId);
076:
077: query.executeUpdate();
078: return null;
079: }
080: });
081: }
082:
083: public void saveNotification(Notification notification) {
084: final DefaultNotification n = (DefaultNotification) notification;
085: getHibernateTemplate().execute(new HibernateCallback() {
086: public Object doInHibernate(Session session)
087: throws HibernateException, SQLException {
088:
089: session.save(n);
090: Criteria c = session.createCriteria(User.class);
091: Iterator it = c.list().iterator();
092: while (it.hasNext()) {
093: User user = (User) it.next();
094: session.save(new UserNotification(user, n));
095: }
096: return null;
097: }
098: });
099: }
100:
101: }
|