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