KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > workflow > notification > support > DefaultNotificationDao


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.workflow.notification.support;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.riotfamily.riot.workflow.notification.Notification;
34 import org.riotfamily.riot.workflow.notification.NotificationDao;
35
36 public class DefaultNotificationDao implements NotificationDao {
37     
38     private List JavaDoc notifications = new ArrayList JavaDoc();
39     
40     private Map JavaDoc notificationsReadByUser = new HashMap JavaDoc();
41     
42     
43     public DefaultNotificationDao() {
44     }
45     
46     public List JavaDoc getNotifications(String JavaDoc userId) {
47         Set JavaDoc readNotificationIds = (Set JavaDoc) notificationsReadByUser.get(userId);
48         if (readNotificationIds == null) {
49             return notifications;
50         }
51         else {
52             return getUnreadNotifications(readNotificationIds);
53         }
54     }
55     
56     private List JavaDoc getUnreadNotifications(Set JavaDoc readNotificationIds) {
57         List JavaDoc unreadNotifications = new ArrayList JavaDoc();
58         for (int i = 0; i < notifications.size(); i++) {
59             Notification notification = (Notification) notifications.get(i);
60             if (!readNotificationIds.contains(notification.getId())) {
61                 unreadNotifications.add(notification);
62             }
63         }
64         return unreadNotifications;
65     }
66
67     public void markAsRead(String JavaDoc userId, Long JavaDoc notificationId) {
68         Set JavaDoc readNotifications = (Set JavaDoc) notificationsReadByUser.get(userId);
69         if (readNotifications == null) {
70             readNotifications = new HashSet JavaDoc();
71             notificationsReadByUser.put(userId, readNotifications);
72         }
73         readNotifications.add(notificationId);
74     }
75         
76     public void saveNotification(Notification notification) {
77         notifications.add(notification);
78     }
79
80 }
81
Popular Tags