-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
src/main/java/teammates/storage/sqlapi/NotificationsDb.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package teammates.storage.sqlapi; | ||
|
||
import teammates.common.exception.EntityAlreadyExistsException; | ||
import teammates.common.exception.EntityDoesNotExistException; | ||
import teammates.common.exception.InvalidParametersException; | ||
import teammates.common.util.HibernateUtil; | ||
import teammates.storage.sqlentity.Notification; | ||
|
||
/** | ||
* Handles CRUD operations for notifications. | ||
* | ||
* @see Notification | ||
*/ | ||
public final class NotificationsDb extends EntitiesDb<Notification> { | ||
|
||
private static final NotificationsDb instance = new NotificationsDb(); | ||
|
||
private NotificationsDb() { | ||
// prevent initialization | ||
} | ||
|
||
public static NotificationsDb inst() { | ||
return instance; | ||
} | ||
|
||
/** | ||
* Creates a notification. | ||
*/ | ||
public Notification createNotification(Notification notification) | ||
throws InvalidParametersException, EntityAlreadyExistsException { | ||
assert notification != null; | ||
|
||
notification.sanitizeForSaving(); | ||
if (!notification.isValid()) { | ||
throw new InvalidParametersException(notification.getInvalidityInfo()); | ||
} | ||
|
||
if (getNotification(notification.getNotificationId().toString()) != null) { | ||
throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, | ||
notification.toString())); | ||
} | ||
|
||
persist(notification); | ||
return notification; | ||
} | ||
|
||
/** | ||
* Gets a notification by its unique ID. | ||
*/ | ||
public Notification getNotification(String notificationId) { | ||
assert notificationId != null; | ||
|
||
return HibernateUtil.getSessionFactory().getCurrentSession().get(Notification.class, notificationId); | ||
} | ||
|
||
/** | ||
* Updates a notification with {@link Notification}. | ||
*/ | ||
public Notification updateNotification(Notification notification) | ||
throws InvalidParametersException, EntityDoesNotExistException { | ||
assert notification != null; | ||
|
||
notification.sanitizeForSaving(); | ||
|
||
if (!notification.isValid()) { | ||
throw new InvalidParametersException(notification.getInvalidityInfo()); | ||
} | ||
|
||
if (getNotification(notification.getNotificationId().toString()) == null) { | ||
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT); | ||
} | ||
|
||
return merge(notification); | ||
} | ||
|
||
/** | ||
* Deletes a notification by its unique ID. | ||
* | ||
* <p>Fails silently if there is no such notification. | ||
*/ | ||
public void deleteNotification(String notificationId) { | ||
assert notificationId != null; | ||
|
||
Notification notification = getNotification(notificationId); | ||
if (notification != null) { | ||
delete(notification); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters