Class NotificationService
java.lang.Object
fr.utc.miage.sporttrack.service.user.communication.NotificationService
Service layer component responsible for managing
Notification entities
within the SportTrack application.
Provides business logic for creating, querying, marking as read, and dispatching typed notifications for various application events such as messages, friend requests, badge awards, objectives, and challenge endings.
- Author:
- SportTrack Team
-
Constructor Summary
ConstructorsConstructorDescriptionNotificationService(NotificationRepository notificationRepository) Constructs a newNotificationServicewith the given repository. -
Method Summary
Modifier and TypeMethodDescriptionlongcountUnreadNotifications(Integer athleteId) Counts the number of unread notifications for the specified athlete.createNotification(Athlete recipient, Athlete actor, NotificationType type, String title, String content, String targetUrl) Creates and persists a new notification.getNotificationForRecipient(Integer notificationId, Integer recipientId) Returns a specific notification if it belongs to the specified recipient.getNotificationsForAthlete(Integer athleteId) Returns all notifications for the specified athlete, ordered newest first.getRecentNotifications(Integer athleteId) Returns the ten most recent notifications for the specified athlete.voidmarkAllAsRead(Integer recipientId) Marks all notifications as read for the specified recipient.voidmarkAsRead(Integer notificationId, Integer recipientId) Marks a single notification as read for the specified recipient.voidnotifyActivityPublished(Athlete actor, Activity activity, List<Athlete> recipients) Sends a notification to multiple recipients that a friend has published a new activity.voidnotifyBadgeEarned(Athlete athlete, Badge badge) Sends a notification to the athlete that they have earned a badge.voidnotifyChallengeEnded(Challenge challenge) Sends notifications to all participants and the organiser that a challenge has ended.voidnotifyFriendRequest(Friendship friendship) Sends a notification to the friendship recipient that a friend request has been received.voidnotifyFriendRequestAccepted(Friendship friendship) Sends a notification to the friendship initiator that their request has been accepted.voidnotifyMessageReceived(Message message) Sends a notification to the message recipient that a new message has been received.voidnotifyObjectiveCompleted(Athlete athlete, Objective objective) Sends a notification to the athlete that an objective has been completed.
-
Constructor Details
-
NotificationService
Constructs a newNotificationServicewith the given repository.- Parameters:
notificationRepository- the notification repository
-
-
Method Details
-
createNotification
public Notification createNotification(Athlete recipient, Athlete actor, NotificationType type, String title, String content, String targetUrl) Creates and persists a new notification.- Parameters:
recipient- the athlete who will receive the notificationactor- the athlete who triggered the notification (may benull)type- the type of notificationtitle- the notification titlecontent- the notification body texttargetUrl- the URL to navigate to when the notification is clicked- Returns:
- the persisted
Notification, ornullif the recipient is invalid
-
getNotificationsForAthlete
@Transactional(readOnly=true) public List<Notification> getNotificationsForAthlete(Integer athleteId) Returns all notifications for the specified athlete, ordered newest first.- Parameters:
athleteId- the identifier of the athlete- Returns:
- a list of notifications, or an empty list if the identifier is null
-
getNotificationForRecipient
@Transactional(readOnly=true) public Notification getNotificationForRecipient(Integer notificationId, Integer recipientId) Returns a specific notification if it belongs to the specified recipient.- Parameters:
notificationId- the notification identifierrecipientId- the recipient identifier- Returns:
- the matching notification, or
nullif not found or parameters are null
-
getRecentNotifications
Returns the ten most recent notifications for the specified athlete.- Parameters:
athleteId- the identifier of the athlete- Returns:
- a list of up to ten notifications, newest first
-
countUnreadNotifications
Counts the number of unread notifications for the specified athlete.- Parameters:
athleteId- the identifier of the athlete- Returns:
- the count of unread notifications, or
0if the identifier is null
-
markAsRead
Marks a single notification as read for the specified recipient.- Parameters:
notificationId- the notification identifierrecipientId- the recipient identifier- Throws:
IllegalArgumentException- if the notification is not found
-
markAllAsRead
Marks all notifications as read for the specified recipient.- Parameters:
recipientId- the recipient identifier
-
notifyMessageReceived
Sends a notification to the message recipient that a new message has been received.- Parameters:
message- the message that was received
-
notifyFriendRequest
Sends a notification to the friendship recipient that a friend request has been received.- Parameters:
friendship- the friendship representing the request
-
notifyFriendRequestAccepted
Sends a notification to the friendship initiator that their request has been accepted.- Parameters:
friendship- the accepted friendship
-
notifyActivityPublished
@Transactional public void notifyActivityPublished(Athlete actor, Activity activity, List<Athlete> recipients) Sends a notification to multiple recipients that a friend has published a new activity.- Parameters:
actor- the athlete who published the activityactivity- the newly published activityrecipients- the list of friends to notify
-
notifyBadgeEarned
Sends a notification to the athlete that they have earned a badge.- Parameters:
athlete- the athlete who earned the badgebadge- the badge that was earned
-
notifyObjectiveCompleted
Sends a notification to the athlete that an objective has been completed.- Parameters:
athlete- the athlete who completed the objectiveobjective- the completed objective
-
notifyChallengeEnded
Sends notifications to all participants and the organiser that a challenge has ended.- Parameters:
challenge- the challenge that has ended
-