001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.mail.service.impl;
017
018import java.util.ArrayList;
019import java.util.Collection;
020
021import org.apache.log4j.Logger;
022import org.kuali.rice.core.api.mail.EmailBody;
023import org.kuali.rice.core.api.mail.EmailContent;
024import org.kuali.rice.core.api.mail.EmailSubject;
025import org.kuali.rice.kew.actionitem.ActionItem;
026import org.kuali.rice.kew.actionitem.ActionItemActionListExtension;
027import org.kuali.rice.kew.api.KewApiConstants;
028import org.kuali.rice.kew.api.KewApiServiceLocator;
029import org.kuali.rice.kew.api.preferences.Preferences;
030import org.kuali.rice.kew.mail.service.EmailContentService;
031import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
032import org.kuali.rice.kew.service.KEWServiceLocator;
033import org.kuali.rice.kim.api.identity.Person;
034import org.kuali.rice.kim.api.services.KimApiServiceLocator;
035
036/**
037 * ActionListEmailService implementation whose content is configurable/parameterizable
038 * via a pluggable EmailContentService
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041public class CustomizableActionListEmailServiceImpl extends ActionListEmailServiceImpl {
042    private static final Logger LOG = Logger.getLogger(CustomizableActionListEmailServiceImpl.class);
043
044    private EmailContentService contentService;
045
046    // ---- Spring property
047
048    public void setEmailContentGenerator(EmailContentService contentService) {
049        this.contentService = contentService;
050    }
051
052    protected EmailContentService getEmailContentGenerator() {
053        return contentService;
054    }
055
056    public void sendImmediateReminder(org.kuali.rice.kew.api.action.ActionItem actionItem, Boolean skipOnApprovals) {
057        if (actionItem == null) {
058            LOG.warn("Request to send immediate reminder to recipient of a null action item... aborting.");
059            return;
060        }
061        
062        if (actionItem.getPrincipalId() == null) {
063            LOG.warn("Request to send immediate reminder to null recipient of an action item... aborting.");
064            return;
065        }
066        
067        if (skipOnApprovals != null && skipOnApprovals.booleanValue()
068                && actionItem.getActionRequestCd().equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ)) {
069            LOG.debug("As requested, skipping immediate reminder notification on action item approval for " + actionItem.getPrincipalId());
070            return;
071        }
072        
073        Preferences preferences = KewApiServiceLocator.getPreferencesService().getPreferences(actionItem.getPrincipalId());
074        if(!checkEmailNotificationPreferences(actionItem, preferences, KewApiConstants.EMAIL_RMNDR_IMMEDIATE)) {
075            LOG.debug("Email suppressed due to the user's preferences");
076            return;
077        }
078        
079        if (!sendActionListEmailNotification()) {
080            LOG.debug("not sending immediate reminder");
081            return;
082        }
083        
084        // since this is a message for a single document, we can customize the from
085        // line based on DocumentType
086        DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId());
087        Person person = KimApiServiceLocator.getPersonService().getPerson(actionItem.getPrincipalId());
088        if (person != null) {
089            EmailContent content = getEmailContentGenerator().generateImmediateReminder(person, actionItem, document.getDocumentType());
090            sendEmail(person, new EmailSubject(content.getSubject()),
091                        new EmailBody(content.getBody()), document.getDocumentType());
092        }
093    }
094
095    @Override
096    protected void sendPeriodicReminder(String principalId, Collection<ActionItemActionListExtension> actionItems, String emailSetting) {
097        actionItems = filterActionItemsToNotify(principalId, actionItems, emailSetting);
098        Collection<org.kuali.rice.kew.api.action.ActionItem> apiActionItems = new ArrayList<org.kuali.rice.kew.api.action.ActionItem>();
099        for(ActionItem actionItem : actionItems) {
100            apiActionItems.add(ActionItem.to(actionItem));
101        }
102        // if there are no action items after being filtered, there's no
103        // reason to send the email
104        if (actionItems.isEmpty()) {
105            return;
106        }
107        EmailContent content;
108        Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
109        if (KewApiConstants.EMAIL_RMNDR_DAY_VAL.equals(emailSetting)) {
110            content = getEmailContentGenerator().generateDailyReminder(person, apiActionItems);
111        } else if (KewApiConstants.EMAIL_RMNDR_WEEK_VAL.equals(emailSetting)) {
112            content = getEmailContentGenerator().generateWeeklyReminder(person, apiActionItems);
113        } else {
114            // else...refactor this...
115            throw new RuntimeException("invalid email setting. this code needs refactoring");
116        }
117        sendEmail(person, new EmailSubject(content.getSubject()), new EmailBody(content.getBody()));
118    }
119
120}