001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.ametys.plugins.forms.workflow;
017
018import java.util.Map;
019
020import javax.mail.MessagingException;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024
025import org.ametys.core.util.mail.SendMailHelper;
026
027import com.opensymphony.module.propertyset.PropertySet;
028import com.opensymphony.workflow.FunctionProvider;
029import com.opensymphony.workflow.WorkflowException;
030
031/**
032 * OS workflow function to send mail after an action is triggered.
033 * The author of a form entry is also notified if the receipt is set
034 */
035public class SendMailFunction extends AbstractLogEnabled implements Component, FunctionProvider
036{
037    /** Actually send the email ? */
038    public static final String SEND_MAIL = "send-request-information-mail";
039    
040    /** Body of the email */
041    public static final String SENDER = "sender";
042    
043    /** Body of the email */
044    public static final String RECIPIENT = "recipient";
045
046    /** Subject of the email */
047    public static final String SUBJECT = "subject";
048    
049    /** Body of the email */
050    public static final String BODY = "body";
051    
052    @Override
053    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
054    {
055        // If "send-mail" is set to true or is not present, send the mail.
056        boolean dontSendMail = "false".equals(transientVars.get(SEND_MAIL));
057        if (dontSendMail)
058        {
059            return;
060        }
061        
062        String sender = (String) transientVars.get(SENDER);
063        String recipient = (String) transientVars.get(RECIPIENT);
064        String subject = (String) transientVars.get(SUBJECT);
065        String body = (String) transientVars.get(BODY);
066        
067        try
068        {
069            SendMailHelper.sendMail(subject, null, body, recipient, sender);
070        }
071        catch (MessagingException e)
072        {
073            getLogger().warn("Could not send a workflow notification mail to " + recipient, e);
074        }
075    }
076}