-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
NotificationsLogic.java
123 lines (102 loc) · 4.09 KB
/
NotificationsLogic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package teammates.sqllogic.core;
import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.storage.sqlapi.NotificationsDb;
import teammates.storage.sqlentity.Notification;
/**
* Handles the logic related to notifications.
*/
public final class NotificationsLogic {
private static final NotificationsLogic instance = new NotificationsLogic();
private NotificationsDb notificationsDb;
private NotificationsLogic() {
// prevent initialization
}
public static NotificationsLogic inst() {
return instance;
}
/**
* Initialise dependencies for {@code NotificationLogic} object.
*/
public void initLogicDependencies(NotificationsDb notificationsDb) {
this.notificationsDb = notificationsDb;
}
/**
* Creates a notification.
*
* @return the created notification
* @throws InvalidParametersException if the notification is not valid
* @throws EntityAlreadyExistsException if the notification already exists in the database.
*/
public Notification createNotification(Notification notification)
throws InvalidParametersException, EntityAlreadyExistsException {
return notificationsDb.createNotification(notification);
}
/**
* Gets notification associated with the {@code notificationId}.
*
* @return null if no match found.
*/
public Notification getNotification(UUID notificationId) {
assert notificationId != null;
return notificationsDb.getNotification(notificationId);
}
/**
* Updates/Creates the notification using {@link Notification}.
*
* @return updated notification
* @throws InvalidParametersException if attributes to update are not valid
* @throws EntityDoesNotExistException if notification cannot be found with given Id
*/
public Notification updateNotification(UUID notificationId, Instant startTime, Instant endTime,
NotificationStyle style, NotificationTargetUser targetUser, String title,
String message)
throws InvalidParametersException, EntityDoesNotExistException {
Notification notification = notificationsDb.getNotification(notificationId);
if (notification == null) {
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT + Notification.class);
}
notification.setStartTime(startTime);
notification.setEndTime(endTime);
notification.setStyle(style);
notification.setTargetUser(targetUser);
notification.setTitle(title);
notification.setMessage(message);
if (!notification.isValid()) {
throw new InvalidParametersException(notification.getInvalidityInfo());
}
return notification;
}
/**
* Deletes notification associated with the {@code notificationId}.
*
* <p>Fails silently if the notification doesn't exist.</p>
*/
public void deleteNotification(UUID notificationId) {
assert notificationId != null;
Notification notification = getNotification(notificationId);
notificationsDb.deleteNotification(notification);
}
/**
* Gets all notifications.
*/
public List<Notification> getAllNotifications() {
return notificationsDb.getAllNotifications();
}
/**
* Gets a list of notifications.
*
* @return a list of notifications with the specified {@code targetUser}.
*/
public List<Notification> getActiveNotificationsByTargetUser(NotificationTargetUser targetUser) {
assert targetUser != null;
return notificationsDb.getActiveNotificationsByTargetUser(targetUser);
}
}