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.logic;
20  
21  import java.lang.reflect.Method;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import org.apache.commons.lang3.ArrayUtils;
25  import org.apache.commons.lang3.tuple.Triple;
26  import org.apache.syncope.common.lib.to.JobTO;
27  import org.apache.syncope.common.lib.to.NotificationTO;
28  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
29  import org.apache.syncope.common.lib.types.JobAction;
30  import org.apache.syncope.common.lib.types.JobType;
31  import org.apache.syncope.core.persistence.api.dao.JobStatusDAO;
32  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
33  import org.apache.syncope.core.persistence.api.dao.NotificationDAO;
34  import org.apache.syncope.core.persistence.api.entity.Notification;
35  import org.apache.syncope.core.provisioning.api.data.NotificationDataBinder;
36  import org.apache.syncope.core.provisioning.api.job.JobManager;
37  import org.apache.syncope.core.provisioning.java.job.notification.NotificationJob;
38  import org.quartz.JobKey;
39  import org.springframework.scheduling.quartz.SchedulerFactoryBean;
40  import org.springframework.security.access.prepost.PreAuthorize;
41  import org.springframework.transaction.annotation.Transactional;
42  
43  public class NotificationLogic extends AbstractJobLogic<NotificationTO> {
44  
45      protected final NotificationDAO notificationDAO;
46  
47      protected final NotificationDataBinder binder;
48  
49      public NotificationLogic(
50              final JobManager jobManager,
51              final SchedulerFactoryBean scheduler,
52              final JobStatusDAO jobStatusDAO,
53              final NotificationDAO notificationDAO,
54              final NotificationDataBinder binder) {
55  
56          super(jobManager, scheduler, jobStatusDAO);
57  
58          this.notificationDAO = notificationDAO;
59          this.binder = binder;
60      }
61  
62      @PreAuthorize("hasRole('" + IdRepoEntitlement.NOTIFICATION_READ + "')")
63      @Transactional(readOnly = true)
64      public NotificationTO read(final String key) {
65          Notification notification = notificationDAO.find(key);
66          if (notification == null) {
67              LOG.error("Could not find notification '" + key + '\'');
68  
69              throw new NotFoundException(String.valueOf(key));
70          }
71  
72          return binder.getNotificationTO(notification);
73      }
74  
75      @PreAuthorize("hasRole('" + IdRepoEntitlement.NOTIFICATION_LIST + "')")
76      @Transactional(readOnly = true)
77      public List<NotificationTO> list() {
78          return notificationDAO.findAll().stream().map(binder::getNotificationTO).collect(Collectors.toList());
79      }
80  
81      @PreAuthorize("hasRole('" + IdRepoEntitlement.NOTIFICATION_CREATE + "')")
82      public NotificationTO create(final NotificationTO notificationTO) {
83          return binder.getNotificationTO(notificationDAO.save(binder.create(notificationTO)));
84      }
85  
86      @PreAuthorize("hasRole('" + IdRepoEntitlement.NOTIFICATION_UPDATE + "')")
87      public NotificationTO update(final NotificationTO notificationTO) {
88          Notification notification = notificationDAO.find(notificationTO.getKey());
89          if (notification == null) {
90              LOG.error("Could not find notification '" + notificationTO.getKey() + '\'');
91              throw new NotFoundException(String.valueOf(notificationTO.getKey()));
92          }
93  
94          binder.update(notification, notificationTO);
95          notification = notificationDAO.save(notification);
96  
97          return binder.getNotificationTO(notification);
98      }
99  
100     @PreAuthorize("hasRole('" + IdRepoEntitlement.NOTIFICATION_DELETE + "')")
101     public NotificationTO delete(final String key) {
102         Notification notification = notificationDAO.find(key);
103         if (notification == null) {
104             LOG.error("Could not find notification '" + key + '\'');
105 
106             throw new NotFoundException(String.valueOf(key));
107         }
108 
109         NotificationTO deleted = binder.getNotificationTO(notification);
110         notificationDAO.delete(key);
111         return deleted;
112     }
113 
114     @Override
115     protected Triple<JobType, String, String> getReference(final JobKey jobKey) {
116         return JobManager.NOTIFICATION_JOB.equals(jobKey)
117                 ? Triple.of(JobType.NOTIFICATION, null, NotificationJob.class.getSimpleName())
118                 : null;
119     }
120 
121     @PreAuthorize("hasRole('" + IdRepoEntitlement.NOTIFICATION_LIST + "')")
122     public JobTO getJob() {
123         List<JobTO> jobs = super.doListJobs(false);
124         return jobs.isEmpty() ? null : jobs.get(0);
125     }
126 
127     @PreAuthorize("hasRole('" + IdRepoEntitlement.NOTIFICATION_EXECUTE + "')")
128     public void actionJob(final JobAction action) {
129         super.doActionJob(JobManager.NOTIFICATION_JOB, action);
130     }
131 
132     @Override
133     protected NotificationTO resolveReference(final Method method, final Object... args)
134             throws UnresolvedReferenceException {
135 
136         String key = null;
137 
138         if (ArrayUtils.isNotEmpty(args)) {
139             for (int i = 0; key == null && i < args.length; i++) {
140                 if (args[i] instanceof String) {
141                     key = (String) args[i];
142                 } else if (args[i] instanceof NotificationTO) {
143                     key = ((NotificationTO) args[i]).getKey();
144                 }
145             }
146         }
147 
148         if (key != null) {
149             try {
150                 return binder.getNotificationTO(notificationDAO.find(key));
151             } catch (Throwable ignore) {
152                 LOG.debug("Unresolved reference", ignore);
153                 throw new UnresolvedReferenceException(ignore);
154             }
155         }
156 
157         throw new UnresolvedReferenceException();
158     }
159 }