1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.persistence.jpa.dao;
20
21 import java.util.List;
22 import javax.persistence.TypedQuery;
23 import org.apache.syncope.common.lib.types.TaskType;
24 import org.apache.syncope.core.persistence.api.dao.NotificationDAO;
25 import org.apache.syncope.core.persistence.api.dao.TaskDAO;
26 import org.apache.syncope.core.persistence.api.entity.Implementation;
27 import org.apache.syncope.core.persistence.api.entity.MailTemplate;
28 import org.apache.syncope.core.persistence.api.entity.Notification;
29 import org.apache.syncope.core.persistence.api.entity.task.Task;
30 import org.apache.syncope.core.persistence.jpa.entity.JPANotification;
31 import org.springframework.transaction.annotation.Transactional;
32
33 public class JPANotificationDAO extends AbstractDAO<Notification> implements NotificationDAO {
34
35 protected final TaskDAO taskDAO;
36
37 public JPANotificationDAO(final TaskDAO taskDAO) {
38 this.taskDAO = taskDAO;
39 }
40
41 @Transactional(readOnly = true)
42 @Override
43 public Notification find(final String key) {
44 return entityManager().find(JPANotification.class, key);
45 }
46
47 @Transactional(readOnly = true)
48 @Override
49 public List<Notification> findByTemplate(final MailTemplate template) {
50 TypedQuery<Notification> query = entityManager().createQuery(
51 "SELECT e FROM " + JPANotification.class.getSimpleName() + " e "
52 + "WHERE e.template=:template", Notification.class);
53 query.setParameter("template", template);
54 return query.getResultList();
55 }
56
57 @Override
58 public List<Notification> findByRecipientsProvider(final Implementation recipientsProvider) {
59 TypedQuery<Notification> query = entityManager().createQuery(
60 "SELECT e FROM " + JPANotification.class.getSimpleName()
61 + " e WHERE e.recipientsProvider=:recipientsProvider", Notification.class);
62 query.setParameter("recipientsProvider", recipientsProvider);
63
64 return query.getResultList();
65 }
66
67 @Transactional(readOnly = true)
68 @Override
69 public List<Notification> findAll() {
70 TypedQuery<Notification> query = entityManager().createQuery(
71 "SELECT e FROM " + JPANotification.class.getSimpleName() + " e", Notification.class);
72 return query.getResultList();
73 }
74
75 @Override
76 public Notification save(final Notification notification) {
77 ((JPANotification) notification).list2json();
78 return entityManager().merge(notification);
79 }
80
81 @Override
82 public void delete(final String key) {
83 Notification notification = find(key);
84 if (notification == null) {
85 return;
86 }
87
88 taskDAO.findAll(
89 TaskType.NOTIFICATION, null, notification, null, null, -1, -1, List.of()).
90 stream().map(Task::getKey).forEach(this::delete);
91
92 entityManager().remove(notification);
93 }
94 }