-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FRI-635 Updated logic when canceling the build and added notifications
- Loading branch information
Showing
21 changed files
with
789 additions
and
9 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
src/main/java/org/ihtsdo/buildcloud/core/dao/NotificationDao.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,19 @@ | ||
package org.ihtsdo.buildcloud.core.dao; | ||
|
||
import org.ihtsdo.buildcloud.core.entity.BuildStatusTracker; | ||
import org.ihtsdo.buildcloud.core.entity.Notification; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
|
||
import java.util.List; | ||
|
||
public interface NotificationDao extends EntityDAO<Notification> { | ||
|
||
Page<Notification> findAll(String recipient, PageRequest pageRequest); | ||
|
||
Long countUnreadNotifications(String recipient); | ||
|
||
List<Notification> findByRecipient(String recipient); | ||
|
||
List<Notification> findByIds(List<Long> notificationIds); | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/org/ihtsdo/buildcloud/core/dao/NotificationDaoImpl.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,72 @@ | ||
package org.ihtsdo.buildcloud.core.dao; | ||
|
||
import org.hibernate.query.Query; | ||
import org.ihtsdo.buildcloud.core.entity.Notification; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class NotificationDaoImpl extends EntityDAOImpl<Notification> implements NotificationDao { | ||
|
||
protected NotificationDaoImpl() { | ||
super(Notification.class); | ||
} | ||
|
||
@Override | ||
public Page<Notification> findAll(String recipient, PageRequest pageRequest) { | ||
Query query = getCurrentSession().createQuery( | ||
"select notification " + | ||
"from Notification notification " + | ||
"where notification.recipient = :recipient " + | ||
"order by notification.createdDate DESC"); | ||
query.setParameter("recipient", recipient); | ||
query.setFirstResult(pageRequest.getPageNumber() * pageRequest.getPageSize()); | ||
query.setMaxResults(pageRequest.getPageSize()); | ||
|
||
Query queryTotal = getCurrentSession().createQuery( | ||
"select count(notification.id) " + | ||
"from Notification notification " + | ||
"where notification.recipient = :recipient"); | ||
queryTotal.setParameter("recipient", recipient); | ||
|
||
|
||
return new PageImpl(query.list(), pageRequest, (long) queryTotal.uniqueResult()); | ||
} | ||
|
||
@Override | ||
public Long countUnreadNotifications(String recipient) { | ||
Query queryTotal = getCurrentSession().createQuery( | ||
"select count(notification.id) " + | ||
"from Notification notification " + | ||
"where notification.recipient = :recipient and notification.read = 'N'"); | ||
queryTotal.setParameter("recipient", recipient); | ||
|
||
return (long) queryTotal.uniqueResult(); | ||
} | ||
|
||
@Override | ||
public List<Notification> findByRecipient(String recipient) { | ||
Query query = getCurrentSession().createQuery( | ||
"select notification " + | ||
"from Notification notification " + | ||
"where notification.recipient = :recipient"); | ||
query.setParameter("recipient", recipient); | ||
|
||
return query.list(); | ||
} | ||
|
||
@Override | ||
public List<Notification> findByIds(List<Long> notificationIds) { | ||
Query query = getCurrentSession().createQuery( | ||
"select notification " + | ||
"from Notification notification " + | ||
"where notification.id in (:notificationIds)"); | ||
query.setParameter("notificationIds", notificationIds); | ||
|
||
return query.list(); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/org/ihtsdo/buildcloud/core/entity/Notification.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,90 @@ | ||
package org.ihtsdo.buildcloud.core.entity; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import org.hibernate.annotations.Type; | ||
|
||
import javax.persistence.*; | ||
|
||
import java.util.Date; | ||
|
||
@Entity | ||
@Table(name = "notification") | ||
public class Notification { | ||
|
||
public enum NotificationType { | ||
BUILD_RUN_OUT_OF_TIME | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Column(name = "details") | ||
private String details; | ||
|
||
@Column(name = "recipient") | ||
private String recipient; | ||
|
||
@Column(name = "notification_type") | ||
private String notificationType; | ||
|
||
@Column(name = "created_date") | ||
private Date createdDate; | ||
|
||
@Type(type="yes_no") | ||
@Column(name = "is_read") | ||
private Boolean read; | ||
|
||
public Notification() { | ||
this.createdDate = new Date(); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getDetails() { | ||
return details; | ||
} | ||
|
||
public void setDetails(String details) { | ||
this.details = details; | ||
} | ||
|
||
public String getRecipient() { | ||
return recipient; | ||
} | ||
|
||
public void setRecipient(String recipient) { | ||
this.recipient = recipient; | ||
} | ||
|
||
public String getNotificationType() { | ||
return notificationType; | ||
} | ||
|
||
public void setNotificationType(String notificationType) { | ||
this.notificationType = notificationType; | ||
} | ||
|
||
public Date getCreatedDate() { | ||
return createdDate; | ||
} | ||
|
||
public void setCreatedDate(Date createdDate) { | ||
this.createdDate = createdDate; | ||
} | ||
|
||
public boolean isRead() { | ||
return read; | ||
} | ||
|
||
public void setRead(boolean read) { | ||
this.read = read; | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/org/ihtsdo/buildcloud/core/service/NotificationService.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,20 @@ | ||
package org.ihtsdo.buildcloud.core.service; | ||
|
||
import org.ihtsdo.buildcloud.core.entity.Notification; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
|
||
import java.util.List; | ||
|
||
public interface NotificationService { | ||
|
||
Notification create(Notification notification); | ||
|
||
Page<Notification> findAll(String username, PageRequest pageRequest); | ||
|
||
Long countUnreadNotifications(String username); | ||
|
||
List<Long> removeNotifications(List<Long> notificationIds); | ||
|
||
List<Long> markNotificationsAsRead(List<Long> notificationIds); | ||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/org/ihtsdo/buildcloud/core/service/NotificationServiceImpl.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,99 @@ | ||
package org.ihtsdo.buildcloud.core.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.ihtsdo.buildcloud.core.dao.NotificationDao; | ||
import org.ihtsdo.buildcloud.core.entity.Notification; | ||
import org.ihtsdo.sso.integration.SecurityUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@ConditionalOnProperty(name = "srs.manager", havingValue = "true") | ||
@Service | ||
@Transactional | ||
public class NotificationServiceImpl implements NotificationService { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private NotificationDao dao; | ||
|
||
@Autowired | ||
private SimpMessagingTemplate simpMessagingTemplate; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Override | ||
public Notification create(Notification notification) { | ||
dao.save(notification); | ||
return notification; | ||
} | ||
|
||
@Override | ||
public Page<Notification> findAll(String recipient, PageRequest pageRequest) { | ||
return dao.findAll(recipient, pageRequest); | ||
} | ||
|
||
@Override | ||
public Long countUnreadNotifications(String username) { | ||
return dao.countUnreadNotifications(username); | ||
} | ||
|
||
@Override | ||
public List<Long> removeNotifications(List<Long> notificationIds) { | ||
String currentUser = SecurityUtil.getUsername(); | ||
List<Notification> notifications; | ||
if (notificationIds == null || notificationIds.isEmpty()) { | ||
notifications = dao.findByRecipient(currentUser); | ||
} else { | ||
notifications = dao.findByIds(notificationIds); | ||
} | ||
List<Long> removedNotificationIds = new ArrayList<>(); | ||
for (Notification notification : notifications) { | ||
removedNotificationIds.add(notification.getId()); | ||
dao.delete(notification); | ||
} | ||
|
||
try { | ||
Map<String, Object> message = new HashMap<>(); | ||
message.put("event", "DELETE_NOTIFICATIONS"); | ||
simpMessagingTemplate.convertAndSend("/topic/user/" + currentUser + "/notification", objectMapper.writeValueAsString(message)); | ||
} catch (JsonProcessingException e) { | ||
logger.error("Failed to send message through web-socket", e); | ||
} | ||
return removedNotificationIds; | ||
} | ||
|
||
@Override | ||
public List<Long> markNotificationsAsRead(List<Long> notificationIds) { | ||
String currentUser = SecurityUtil.getUsername(); | ||
List<Notification> notifications = dao.findByIds(notificationIds); | ||
List<Long> readNotifications = new ArrayList<>(); | ||
for (Notification notification : notifications) { | ||
notification.setRead(Boolean.TRUE); | ||
readNotifications.add(notification.getId()); | ||
dao.update(notification); | ||
} | ||
try { | ||
Map<String, Object> message = new HashMap<>(); | ||
message.put("event", "MARK_NOTIFICATIONS_AS_READ"); | ||
simpMessagingTemplate.convertAndSend("/topic/user/" + currentUser + "/notification", objectMapper.writeValueAsString(message)); | ||
} catch (JsonProcessingException e) { | ||
logger.error("Failed to send message through web-socket", e); | ||
} | ||
return readNotifications; | ||
} | ||
} |
Oops, something went wrong.