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.entity.principal.Principal;
023    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
024    import org.kuali.rice.kim.bo.Person;
025    
026    
027    import java.util.ArrayList;
028    import java.util.Collection;
029    import java.util.HashMap;
030    import java.util.Iterator;
031    import java.util.List;
032    import java.util.Map;
033    
034    
035    public class MockEmailNotificationServiceImpl /*extends CustomizableActionListEmailServiceImpl*/ implements MockEmailNotificationService {
036        private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
037    
038        private static Map<String,List> immediateReminders = new HashMap<String,List>();
039        private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
040        public static boolean SEND_DAILY_REMINDER_CALLED = false;
041        public static boolean SEND_WEEKLY_REMINDER_CALLED = false;
042    
043        private EmailContentService contentService;
044        private String deploymentEnvironment;
045    
046            private Mailer mailer;
047    
048        /**
049         * Resets the reminder counts
050         */
051        public void resetReminderCounts() {
052            aggregateReminderCount.clear();
053            immediateReminders.clear();
054        }
055    
056        /**
057         * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action
058         * items processed
059         */
060        @Override
061        public void sendImmediateReminder(Person user, ActionItem actionItem) {
062            //super.sendImmediateReminder(user, actionItem);
063            List actionItemsSentUser = (List)immediateReminders.get(user.getPrincipalId());
064            if (actionItemsSentUser == null) {
065                actionItemsSentUser = new ArrayList();
066                immediateReminders.put(user.getPrincipalId(), actionItemsSentUser);
067            }
068            actionItemsSentUser.add(actionItem);
069        }
070    
071        /**
072         * This overridden method returns a value of true always
073         */
074        //@Override
075        protected boolean sendActionListEmailNotification() {
076    
077            return true;
078        }
079    
080        @Override
081            public void sendDailyReminder() {
082            resetStyleService();
083                //super.sendDailyReminder();
084                    SEND_DAILY_REMINDER_CALLED = true;
085        }
086    
087        @Override
088        public void sendWeeklyReminder() {
089            resetStyleService();
090            //super.sendWeeklyReminder();
091            SEND_WEEKLY_REMINDER_CALLED = true;
092        }
093    
094        @Override
095        public void scheduleBatchEmailReminders() throws Exception {
096            //do nothing
097        }
098    
099        //@Override
100        protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
101            //super.sendPeriodicReminder(user, actionItems, emailSetting);
102            if (!aggregateReminderCount.containsKey(emailSetting)) {
103                aggregateReminderCount.put(emailSetting, actionItems.size());
104            } else {
105                aggregateReminderCount.put(emailSetting, aggregateReminderCount.get(emailSetting) + actionItems.size());
106            }
107        }
108    
109        public Integer getTotalPeriodicRemindersSent(String emailReminderConstant) {
110            Integer returnVal = aggregateReminderCount.get(emailReminderConstant);
111            if (returnVal == null) {
112                returnVal = Integer.valueOf(0);
113            }
114            return returnVal;
115        }
116    
117        public Integer getTotalPeriodicRemindersSent() {
118            int total = 0;
119            for (Map.Entry<String, Integer> mapEntry : aggregateReminderCount.entrySet()) {
120                Integer value = mapEntry.getValue();
121                total += (value == null) ? 0 : value.intValue();
122            }
123            return Integer.valueOf(total);
124        }
125    
126        public boolean wasStyleServiceAccessed() {
127            return getEmailContentGenerator().wasServiceAccessed();
128        }
129    
130        private void resetStyleService() {
131            getEmailContentGenerator().resetServiceAccessed();
132        }
133    
134        public int immediateReminderEmailsSent(String networkId, String documentId, String actionRequestCd) {
135            Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(networkId);
136            List actionItemsSentUser = immediateReminders.get(principal.getPrincipalId());
137            if (actionItemsSentUser == null) {
138                return 0;
139            }
140            int emailsSent = 0;
141            for (Iterator iter = actionItemsSentUser.iterator(); iter.hasNext();) {
142                ActionItem actionItem = (ActionItem) iter.next();
143                if (actionItem.getDocumentId().equals(documentId) && actionItem.getActionRequestCd().equals(actionRequestCd)) {
144                    emailsSent++;
145                }
146            }
147            return emailsSent;
148        }
149    
150        public void setEmailContentGenerator(EmailContentService contentService) {
151            this.contentService = contentService;
152        }
153    
154        protected MockStyleableEmailContentService getEmailContentGenerator() {
155            return (MockStyleableEmailContentService) contentService;
156        }
157    
158            public void setMailer(Mailer mailer){
159                    this.mailer = mailer;
160            }
161    
162            public void setDeploymentEnvironment(String deploymentEnvironment) {
163                    this.deploymentEnvironment = deploymentEnvironment;
164            }
165    }