001    /**
002     * Copyright 2005-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 java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    
025    import org.apache.log4j.Logger;
026    import org.kuali.rice.core.mail.Mailer;
027    import org.kuali.rice.kew.api.action.ActionItem;
028    import org.kuali.rice.kew.api.action.ActionRequestType;
029    import org.kuali.rice.kew.mail.service.EmailContentService;
030    import org.kuali.rice.kew.api.KewApiConstants;
031    import org.kuali.rice.kim.api.identity.Person;
032    import org.kuali.rice.kim.api.identity.principal.Principal;
033    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
034    
035    
036    public class MockEmailNotificationServiceImpl /*extends CustomizableActionListEmailServiceImpl*/ implements MockEmailNotificationService {
037        private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
038    
039        private static Map<String,List> immediateReminders = new HashMap<String,List>();
040        private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
041        public static boolean SEND_DAILY_REMINDER_CALLED = false;
042        public static boolean SEND_WEEKLY_REMINDER_CALLED = false;
043    
044        private EmailContentService contentService;
045        private String deploymentEnvironment;
046    
047            private Mailer mailer;
048    
049        /**
050         * Resets the reminder counts
051         */
052        public void resetReminderCounts() {
053            aggregateReminderCount.clear();
054            immediateReminders.clear();
055        }
056    
057        /**
058         * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action
059         * items processed
060         */
061        @Override
062        public void sendImmediateReminder(ActionItem actionItem, Boolean skipOnApprovals) {
063            if (skipOnApprovals != null && skipOnApprovals.booleanValue()
064                    && actionItem.getActionRequestCd().equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ)) {
065                LOG.debug("As requested, skipping immediate reminder notification on action item approval for " + actionItem.getPrincipalId());
066                return;
067            }
068            List actionItemsSentUser = (List)immediateReminders.get(actionItem.getPrincipalId());
069            if (actionItemsSentUser == null) {
070                actionItemsSentUser = new ArrayList();
071                immediateReminders.put(actionItem.getPrincipalId(), actionItemsSentUser);
072            }
073            actionItemsSentUser.add(actionItem);
074        }
075    
076        /**
077         * This overridden method returns a value of true always
078         */
079        //@Override
080        protected boolean sendActionListEmailNotification() {
081    
082            return true;
083        }
084    
085        @Override
086            public void sendDailyReminder() {
087            resetStyleService();
088                //super.sendDailyReminder();
089                    SEND_DAILY_REMINDER_CALLED = true;
090        }
091    
092        @Override
093        public void sendWeeklyReminder() {
094            resetStyleService();
095            //super.sendWeeklyReminder();
096            SEND_WEEKLY_REMINDER_CALLED = true;
097        }
098    
099        @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    }