View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }