001    /*
002     * Copyright 2005-2007 The Kuali Foundation
003     *
004     *
005     * Licensed under the Educational Community License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     * http://www.opensource.org/licenses/ecl2.php
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package mocks;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Map;
025    
026    import org.apache.log4j.Logger;
027    import org.kuali.rice.kew.actionitem.ActionItem;
028    import org.kuali.rice.kew.mail.service.impl.CustomizableActionListEmailServiceImpl;
029    import org.kuali.rice.kim.bo.Person;
030    import org.kuali.rice.kim.bo.entity.KimPrincipal;
031    import org.kuali.rice.kim.service.KIMServiceLocator;
032    
033    
034    public class MockEmailNotificationServiceImpl extends CustomizableActionListEmailServiceImpl implements MockEmailNotificationService {
035        private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
036    
037        private static Map<String,List> immediateReminders = new HashMap<String,List>();
038        private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
039        public static boolean SEND_DAILY_REMINDER_CALLED = false;
040        public static boolean SEND_WEEKLY_REMINDER_CALLED = false;
041    
042        /**
043         * Resets the reminder counts
044         */
045        public void resetReminderCounts() {
046            aggregateReminderCount.clear();
047            immediateReminders.clear();
048        }
049    
050        /**
051         * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action
052         * items processed
053         */
054        @Override
055        public void sendImmediateReminder(Person user, ActionItem actionItem) {
056            super.sendImmediateReminder(user, actionItem);
057            List actionItemsSentUser = (List)immediateReminders.get(user.getPrincipalId());
058            if (actionItemsSentUser == null) {
059                actionItemsSentUser = new ArrayList();
060                immediateReminders.put(user.getPrincipalId(), actionItemsSentUser);
061            }
062            actionItemsSentUser.add(actionItem);
063        }
064    
065        /**
066         * This overridden method returns a value of true always
067         */
068        @Override
069        protected boolean sendActionListEmailNotification() {
070    
071            return true;
072        }
073    
074        @Override
075            public void sendDailyReminder() {
076            resetStyleService();
077                super.sendDailyReminder();
078                    SEND_DAILY_REMINDER_CALLED = true;
079        }
080    
081        @Override
082        public void sendWeeklyReminder() {
083            resetStyleService();
084            super.sendWeeklyReminder();
085            SEND_WEEKLY_REMINDER_CALLED = true;
086        }
087    
088        @Override
089        protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
090            super.sendPeriodicReminder(user, actionItems, emailSetting);
091            if (!aggregateReminderCount.containsKey(emailSetting)) {
092                aggregateReminderCount.put(emailSetting, actionItems.size());
093            } else {
094                aggregateReminderCount.put(emailSetting, aggregateReminderCount.get(emailSetting) + actionItems.size());
095            }
096        }
097    
098        public Integer getTotalPeriodicRemindersSent(String emailReminderConstant) {
099            Integer returnVal = aggregateReminderCount.get(emailReminderConstant);
100            if (returnVal == null) {
101                returnVal = Integer.valueOf(0);
102            }
103            return returnVal;
104        }
105    
106        public Integer getTotalPeriodicRemindersSent() {
107            int total = 0;
108            for (Map.Entry<String, Integer> mapEntry : aggregateReminderCount.entrySet()) {
109                Integer value = mapEntry.getValue();
110                total += (value == null) ? 0 : value.intValue();
111            }
112            return Integer.valueOf(total);
113        }
114    
115        public boolean wasStyleServiceAccessed() {
116            return getEmailContentGenerator().wasServiceAccessed();
117        }
118    
119        private void resetStyleService() {
120            getEmailContentGenerator().resetServiceAccessed();
121        }
122    
123        public int immediateReminderEmailsSent(String networkId, Long documentId, String actionRequestCd) {
124            KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(networkId);
125            List actionItemsSentUser = immediateReminders.get(principal.getPrincipalId());
126            if (actionItemsSentUser == null) {
127                return 0;
128            }
129            int emailsSent = 0;
130            for (Iterator iter = actionItemsSentUser.iterator(); iter.hasNext();) {
131                ActionItem actionItem = (ActionItem) iter.next();
132                if (actionItem.getRouteHeaderId().equals(documentId) && actionItem.getActionRequestCd().equals(actionRequestCd)) {
133                    emailsSent++;
134                }
135            }
136            return emailsSent;
137        }
138    
139        @Override
140        protected MockStyleableEmailContentService getEmailContentGenerator() {
141            return (MockStyleableEmailContentService) super.getEmailContentGenerator();
142        }
143    }