1
|
package eu.dnetlib.uoanotificationservice.services;
|
2
|
|
3
|
import eu.dnetlib.uoanotificationservice.dao.NotificationDAO;
|
4
|
import eu.dnetlib.uoanotificationservice.dao.UserDAO;
|
5
|
import eu.dnetlib.uoanotificationservice.entities.Notification;
|
6
|
import eu.dnetlib.uoanotificationservice.entities.User;
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.scheduling.annotation.Scheduled;
|
10
|
import org.springframework.stereotype.Service;
|
11
|
|
12
|
import java.util.Calendar;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
@Service
|
18
|
public class NotificationService {
|
19
|
|
20
|
Logger logger = Logger.getLogger(NotificationService.class);
|
21
|
|
22
|
@Autowired
|
23
|
private NotificationDAO notificationDAO;
|
24
|
|
25
|
@Autowired
|
26
|
private UserDAO userDAO;
|
27
|
|
28
|
public List<Notification> getAllNotifications() {
|
29
|
return notificationDAO.findByOrderByDateDesc();
|
30
|
}
|
31
|
|
32
|
public List<Notification> getMyNotifications(String userId, String email, String service, List<String> roles) {
|
33
|
logger.info("Getting notifications of user " + userId + ", " + email);
|
34
|
Set<String> services = new HashSet<>();
|
35
|
services.add(service);
|
36
|
services.add("all");
|
37
|
Set<String> groups = new HashSet<>(roles);
|
38
|
groups.add(email.toLowerCase());
|
39
|
groups.add("all");
|
40
|
List<Notification> notifications = notificationDAO.findByUserNotAndServicesInAndGroupsInOrderByDateDesc(userId, services, groups);
|
41
|
User user = userDAO.findById(userId);
|
42
|
if(user != null) {
|
43
|
notifications.forEach(notification -> {
|
44
|
notification.setRead(user.getRead().contains(notification.getId()));
|
45
|
});
|
46
|
}
|
47
|
return notifications;
|
48
|
}
|
49
|
|
50
|
public Notification save(Notification notification) {
|
51
|
return notificationDAO.save(notification);
|
52
|
}
|
53
|
|
54
|
public User readNotification(String userId, String id) {
|
55
|
logger.info("Read notification " + id + " for user " + userId);
|
56
|
User user = userDAO.findById(userId);
|
57
|
if(user == null) {
|
58
|
user = new User();
|
59
|
user.setId(userId);
|
60
|
user.setRead(new HashSet<>());
|
61
|
}
|
62
|
user.getRead().add(id);
|
63
|
return userDAO.save(user);
|
64
|
}
|
65
|
|
66
|
public void delete(String id) {
|
67
|
List<User> users = userDAO.findByReadContains(id);
|
68
|
users.forEach(user -> {
|
69
|
user.getRead().remove(id);
|
70
|
userDAO.save(user);
|
71
|
});
|
72
|
notificationDAO.delete(id);
|
73
|
}
|
74
|
|
75
|
// // every min for testing
|
76
|
// //@Scheduled(cron = "0 * * * * *"
|
77
|
// // every day at midnight
|
78
|
// @Scheduled(cron = "0 0 0 1/1 * ?")
|
79
|
// protected void deleteNotifications() {
|
80
|
// logger.info("Deleting notifications that have been created more than a week ago");
|
81
|
// Calendar calendar = Calendar.getInstance();
|
82
|
// calendar.add(Calendar.DAY_OF_MONTH, -7);
|
83
|
// List<Notification> notifications = notificationDAO.findByDateBefore(calendar.getTime());
|
84
|
// notifications.forEach(notification -> {
|
85
|
// delete(notification.getId());
|
86
|
// });
|
87
|
// }
|
88
|
}
|