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.io.IOException;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024
025import org.ametys.core.ui.mail.StandardMailBodyHelper;
026import org.ametys.core.util.mail.SendMailHelper;
027import org.ametys.plugins.workflow.EnhancedFunction;
028import org.ametys.plugins.workflow.support.WorkflowHelper.WorkflowVisibility;
029import org.ametys.runtime.i18n.I18nizableText;
030
031import com.opensymphony.module.propertyset.PropertySet;
032import com.opensymphony.workflow.WorkflowException;
033
034import jakarta.mail.MessagingException;
035
036/**
037 * OS workflow function to send mail after an action is triggered.
038 * The author of a form entry is also notified if the receipt is set
039 */
040public class SendMailFunction extends AbstractLogEnabled implements Component, EnhancedFunction
041{
042    /** Actually send the email ? */
043    public static final String SEND_MAIL = "send-request-information-mail";
044    
045    /** Sender of the email */
046    public static final String SENDER = "sender";
047    
048    /** Recipient of the email */
049    public static final String RECIPIENT = "recipient";
050
051    /** Subject of the email */
052    public static final String SUBJECT = "subject";
053    
054    /** Body of the email */
055    public static final String BODY = "body";
056    
057    @Override
058    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
059    {
060        // If "send-mail" is set to true or is not present, send the mail.
061        boolean dontSendMail = "false".equals(transientVars.get(SEND_MAIL));
062        if (dontSendMail)
063        {
064            return;
065        }
066        
067        String sender = (String) transientVars.get(SENDER);
068        String recipient = (String) transientVars.get(RECIPIENT);
069        String subject = (String) transientVars.get(SUBJECT);
070        String body = (String) transientVars.get(BODY);
071        
072        try
073        {
074            String htmlBody = StandardMailBodyHelper.newHTMLBody()
075                .withTitle(subject)
076                .withMessage(body.replaceAll("\n\r", "<br/>"))
077                .build();
078            
079            SendMailHelper.newMail()
080                          .withAsync(true)
081                          .withSubject(subject)
082                          .withHTMLBody(htmlBody)
083                          .withSender(sender)
084                          .withRecipient(recipient)
085                          .sendMail();
086        }
087        catch (MessagingException | IOException e)
088        {
089            getLogger().warn("Could not send a workflow notification mail to " + recipient, e);
090        }
091    }
092    
093    @Override
094    public FunctionType getFunctionExecType()
095    {
096        return FunctionType.POST;
097    }
098    
099
100    public I18nizableText getLabel()
101    {
102        return new I18nizableText("plugin.forms", "PLUGINS_FORMS_SEND_MAIL_FUNCTION_LABEL");
103    }
104    
105    @Override
106    public I18nizableText getFullLabel(Map<String, String> argumentsValues)
107    {
108        return new I18nizableText("plugin.forms", "PLUGINS_FORMS_SEND_MAIL_FUNCTION_DESCRIPTION");
109    }
110    
111    @Override
112    public List<WorkflowVisibility> getVisibilities()
113    {
114        List<WorkflowVisibility> visibilities = EnhancedFunction.super.getVisibilities();
115        visibilities.add(WorkflowVisibility.USER);
116        return visibilities;
117    }
118}