001/**
002 * Copyright 2005-2017 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 org.kuali.rice.core.api.config.property.ConfigContext;
019import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
020import org.kuali.rice.kew.actionrequest.ActionRequestValue;
021import org.kuali.rice.kew.api.action.ActionItem;
022import org.kuali.rice.kew.api.action.ActionRequest;
023import org.kuali.rice.kew.api.document.Document;
024import org.kuali.rice.kew.api.exception.WorkflowException;
025import org.kuali.rice.kew.doctype.bo.DocumentType;
026import org.kuali.rice.kew.mail.CustomEmailAttribute;
027import org.kuali.rice.kew.mail.service.EmailContentService;
028import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
029import org.kuali.rice.kew.service.KEWServiceLocator;
030import org.kuali.rice.kew.api.KewApiConstants;
031import org.kuali.rice.kim.api.identity.Person;
032import org.kuali.rice.krad.util.KRADConstants;
033
034/**
035 * Base EmailContentService implementation with a default email from address that can be
036 * configured via Spring property injection
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public abstract class BaseEmailContentServiceImpl implements EmailContentService {
040    protected String defaultEmailFromAddress = "admin@localhost";
041    protected String deploymentEnvironment;
042
043    public void setDefaultEmailFromAddress(String defaultEmailFromAddress) {
044        this.defaultEmailFromAddress = defaultEmailFromAddress;
045    }
046
047    public String getApplicationEmailAddress() {
048        // first check the configured value
049        String fromAddress = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.MAILER_DETAIL_TYPE, KewApiConstants.EMAIL_REMINDER_FROM_ADDRESS);
050        // if there's no value configured, use the default
051        if (org.apache.commons.lang.StringUtils.isEmpty(fromAddress)) {
052            fromAddress = defaultEmailFromAddress;
053        }
054        return fromAddress;
055    }
056
057    public String getDocumentTypeEmailAddress(DocumentType documentType) {
058        String fromAddress = (documentType == null ? null : documentType.getNotificationFromAddress());
059        if (org.apache.commons.lang.StringUtils.isEmpty(fromAddress)) {
060            fromAddress = getApplicationEmailAddress();
061        }
062        return fromAddress;
063    }
064
065    public String getDeploymentEnvironment() {
066        return deploymentEnvironment;
067    }
068
069    public void setDeploymentEnvironment(String deploymentEnvironment) {
070        this.deploymentEnvironment = deploymentEnvironment;
071    }
072
073    protected static CustomEmailAttribute getCustomEmailAttribute(Person user, ActionItem actionItem) throws WorkflowException {
074        DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId());
075        CustomEmailAttribute customEmailAttribute = routeHeader.getCustomEmailAttribute();
076        if (customEmailAttribute != null) {
077            Document routeHeaderVO = DocumentRouteHeaderValue.to(routeHeader);
078            ActionRequestValue actionRequest = KEWServiceLocator.getActionRequestService().findByActionRequestId(actionItem.getActionRequestId());
079            ActionRequest actionRequestVO = ActionRequestValue.to(actionRequest);
080            customEmailAttribute.setRouteHeaderVO(routeHeaderVO);
081            customEmailAttribute.setActionRequestVO(actionRequestVO);
082        }
083        return customEmailAttribute;
084    }
085
086    protected String getActionListUrl() {
087        return ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.WORKFLOW_URL_KEY) + "/" + "ActionList.do";
088    }
089
090    protected String getPreferencesUrl() {
091        return ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.WORKFLOW_URL_KEY) + "/" + "Preferences.do";
092    }
093
094    //KULRICE-12359 - add route log to Action List Reminder email
095    protected String getRouteLogUrl() {
096        return ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.WORKFLOW_URL_KEY) + "/" + "RouteLog.do?documentId=";
097    }
098}