Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1911ac526d71ac013e650e95a615d50b99e02df6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*******************************************************************************
 * Copyright (c) 2014 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ats.rest.internal.notify;

import java.util.List;
import org.eclipse.osee.ats.api.notify.AtsNotificationCollector;
import org.eclipse.osee.ats.api.notify.AtsNotificationEvent;
import org.eclipse.osee.ats.api.notify.AtsWorkItemNotificationEvent;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.user.IAtsUserService;
import org.eclipse.osee.ats.rest.util.IAtsNotifierServer;
import org.eclipse.osee.framework.jdk.core.util.EmailUtil;
import org.eclipse.osee.framework.jdk.core.util.Strings;

public class AtsNotificationEventProcessor {

   private final String noReplyEmail;
   private final WorkItemNotificationProcessor workItemNotificationProcessor;
   private final IAtsUserService userService;

   public AtsNotificationEventProcessor(WorkItemNotificationProcessor workItemNotificationProcessor, IAtsUserService userService, String noReplyEmail) {
      this.workItemNotificationProcessor = workItemNotificationProcessor;
      this.userService = userService;
      this.noReplyEmail = noReplyEmail;
   }

   public void sendNotifications(AtsNotificationCollector notifications, List<IAtsNotifierServer> notifiers) {

      // convert all WorkItem notifications to AtsNotificationEvent
      for (AtsWorkItemNotificationEvent workItemEvent : notifications.getWorkItemNotificationEvents()) {
         workItemNotificationProcessor.run(notifications, workItemEvent);
      }

      String testingUserEmail = ""; // change to email address for testing purposes; all emails will go there
      String fromUserEmail = getFromUserEmail(notifications);

      for (IAtsNotifierServer notifier : notifiers) {
         notifier.sendNotifications(fromUserEmail, testingUserEmail, notifications.getSubject(),
            notifications.getBody(), notifications.getNotificationEvents());
      }
   }

   private String getFromUserEmail(AtsNotificationCollector notifications) {
      String email = noReplyEmail;
      for (AtsNotificationEvent event : notifications.getNotificationEvents()) {
         if (Strings.isValid(event.getFromUserId())) {
            IAtsUser userById = userService.getUserById(event.getFromUserId());
            if (EmailUtil.isEmailValid(userById.getEmail())) {
               email = userById.getEmail();
               break;
            }
         }
      }
      return email;
   }

}

Back to the top