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.flowable.task;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.syncope.common.lib.to.UserTO;
23  import org.apache.syncope.common.lib.types.AuditElements;
24  import org.apache.syncope.core.flowable.impl.FlowableRuntimeUtils;
25  import org.apache.syncope.core.persistence.api.entity.user.User;
26  import org.apache.syncope.core.provisioning.api.notification.NotificationManager;
27  import org.flowable.engine.delegate.DelegateExecution;
28  
29  /**
30   * General-purpose notification task for usage within workflow.
31   * It requires a pre-existing Notification with category {@code CUSTOM} and result {@code SUCCESS}.
32   * An {@code event} workflow variable needs to be provided as well.
33   */
34  public class Notify extends FlowableServiceTask {
35  
36      protected final NotificationManager notificationManager;
37  
38      public Notify(final NotificationManager notificationManager) {
39          this.notificationManager = notificationManager;
40      }
41  
42      @Override
43      protected void doExecute(final DelegateExecution execution) {
44          User user = execution.getVariable(FlowableRuntimeUtils.USER, User.class);
45          UserTO userTO = execution.getVariable(FlowableRuntimeUtils.USER_TO, UserTO.class);
46          String event = execution.getVariable(FlowableRuntimeUtils.EVENT, String.class);
47          String wfExecutor = execution.getVariable(FlowableRuntimeUtils.WF_EXECUTOR, String.class);
48  
49          if (StringUtils.isNotBlank(event)) {
50              notificationManager.createTasks(
51                      wfExecutor,
52                      AuditElements.EventCategoryType.CUSTOM,
53                      null,
54                      null,
55                      event,
56                      AuditElements.Result.SUCCESS,
57                      userTO,
58                      null,
59                      user.getToken());
60          } else {
61              LOG.debug("Not sending any notification since no event was found");
62          }
63      }
64  }