View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package mocks;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.log4j.Logger;
26  import org.kuali.rice.core.mail.Mailer;
27  import org.kuali.rice.kew.api.action.ActionItem;
28  import org.kuali.rice.kew.api.action.ActionRequestType;
29  import org.kuali.rice.kew.mail.service.EmailContentService;
30  import org.kuali.rice.kew.api.KewApiConstants;
31  import org.kuali.rice.kim.api.identity.Person;
32  import org.kuali.rice.kim.api.identity.principal.Principal;
33  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
34  
35  
36  public class MockEmailNotificationServiceImpl /*extends CustomizableActionListEmailServiceImpl*/ implements MockEmailNotificationService {
37      private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
38  
39      private static Map<String,List> immediateReminders = new HashMap<String,List>();
40      private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
41      public static boolean SEND_DAILY_REMINDER_CALLED = false;
42      public static boolean SEND_WEEKLY_REMINDER_CALLED = false;
43  
44      private EmailContentService contentService;
45      private String deploymentEnvironment;
46  
47  	private Mailer mailer;
48  
49      /**
50       * Resets the reminder counts
51       */
52      public void resetReminderCounts() {
53          aggregateReminderCount.clear();
54          immediateReminders.clear();
55      }
56  
57      /**
58       * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action
59       * items processed
60       */
61      @Override
62      public void sendImmediateReminder(ActionItem actionItem, Boolean skipOnApprovals) {
63          if (skipOnApprovals != null && skipOnApprovals.booleanValue()
64                  && actionItem.getActionRequestCd().equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ)) {
65              LOG.debug("As requested, skipping immediate reminder notification on action item approval for " + actionItem.getPrincipalId());
66              return;
67          }
68          List actionItemsSentUser = (List)immediateReminders.get(actionItem.getPrincipalId());
69          if (actionItemsSentUser == null) {
70              actionItemsSentUser = new ArrayList();
71              immediateReminders.put(actionItem.getPrincipalId(), actionItemsSentUser);
72          }
73          actionItemsSentUser.add(actionItem);
74      }
75  
76      /**
77       * This overridden method returns a value of true always
78       */
79      //@Override
80      protected boolean sendActionListEmailNotification() {
81  
82          return true;
83      }
84  
85      @Override
86  	public void sendDailyReminder() {
87          resetStyleService();
88  	    //super.sendDailyReminder();
89  		SEND_DAILY_REMINDER_CALLED = true;
90      }
91  
92      @Override
93      public void sendWeeklyReminder() {
94          resetStyleService();
95          //super.sendWeeklyReminder();
96      	SEND_WEEKLY_REMINDER_CALLED = true;
97      }
98  
99      @Override
100     public void scheduleBatchEmailReminders() throws Exception {
101         //do nothing
102     }
103 
104     //@Override
105     protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
106         //super.sendPeriodicReminder(user, actionItems, emailSetting);
107         if (!aggregateReminderCount.containsKey(emailSetting)) {
108             aggregateReminderCount.put(emailSetting, actionItems.size());
109         } else {
110             aggregateReminderCount.put(emailSetting, aggregateReminderCount.get(emailSetting) + actionItems.size());
111         }
112     }
113 
114     public Integer getTotalPeriodicRemindersSent(String emailReminderConstant) {
115         Integer returnVal = aggregateReminderCount.get(emailReminderConstant);
116         if (returnVal == null) {
117             returnVal = Integer.valueOf(0);
118         }
119         return returnVal;
120     }
121 
122     public Integer getTotalPeriodicRemindersSent() {
123         int total = 0;
124         for (Map.Entry<String, Integer> mapEntry : aggregateReminderCount.entrySet()) {
125             Integer value = mapEntry.getValue();
126             total += (value == null) ? 0 : value.intValue();
127         }
128         return Integer.valueOf(total);
129     }
130 
131     public boolean wasStyleServiceAccessed() {
132         return getEmailContentGenerator().wasServiceAccessed();
133     }
134 
135     private void resetStyleService() {
136         getEmailContentGenerator().resetServiceAccessed();
137     }
138 
139     public int immediateReminderEmailsSent(String networkId, String documentId, String actionRequestCd) {
140         Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(networkId);
141         List actionItemsSentUser = immediateReminders.get(principal.getPrincipalId());
142         if (actionItemsSentUser == null) {
143             return 0;
144         }
145         int emailsSent = 0;
146         for (Iterator iter = actionItemsSentUser.iterator(); iter.hasNext();) {
147             ActionItem actionItem = (ActionItem) iter.next();
148             if (actionItem.getDocumentId().equals(documentId) && actionItem.getActionRequestCd().equals(actionRequestCd)) {
149                 emailsSent++;
150             }
151         }
152         return emailsSent;
153     }
154 
155     public void setEmailContentGenerator(EmailContentService contentService) {
156         this.contentService = contentService;
157     }
158 
159     protected MockStyleableEmailContentService getEmailContentGenerator() {
160         return (MockStyleableEmailContentService) contentService;
161     }
162 
163 	public void setMailer(Mailer mailer){
164 		this.mailer = mailer;
165 	}
166 
167 	public void setDeploymentEnvironment(String deploymentEnvironment) {
168 		this.deploymentEnvironment = deploymentEnvironment;
169 	}
170 }