001/*
002 *  Copyright 2019 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.workspaces.project.notification;
017
018import java.util.Arrays;
019import java.util.List;
020import java.util.Map;
021import java.util.Objects;
022import java.util.stream.Collectors;
023
024import javax.mail.MessagingException;
025
026import org.ametys.core.observation.Event;
027import org.ametys.core.user.User;
028import org.ametys.core.util.mail.SendMailHelper;
029import org.ametys.plugins.workspaces.project.objects.Project;
030import org.ametys.runtime.config.Config;
031import org.ametys.runtime.i18n.I18nizableText;
032
033/**
034 * Observer to send mail notifications on workspace member removal
035 */
036public class RemoveMemberMailNotifierObserver extends AbstractRemoveMemberMailNotifierObserver
037{
038    @Override
039    public void observe(Event event, Map<String, Object> transientVars) throws Exception
040    {
041        // Check feature enabled
042        boolean notification = Config.getInstance().getValue("workspaces.member.removed.send.notification");
043        if (!notification)
044        {
045            return;
046        }
047
048        super.observe(event, transientVars);
049    }
050
051    @Override
052    protected void sendMail(Project project, List<User> removedUsers)
053    {
054        // Retrieve mail sender
055        String sender = Config.getInstance().getValue("smtp.mail.from");
056
057        // Compute subject and body
058        String subjectForUser = _i18nUtils.translate(getSubjectI18nizableText(project));
059        String bodyForUser = _i18nUtils.translate(getBodyI18nizableText(project));
060
061        // Send mail to removed members
062        for (String recipient : getUsersEmail(removedUsers))
063        {
064            try
065            {
066                SendMailHelper.sendMail(subjectForUser, null, bodyForUser, recipient, sender, true);
067            }
068            catch (MessagingException e)
069            {
070                getLogger().warn("Could not send a notification e-mail to " + recipient + " following his removal from the project " + project.getTitle(), e);
071            }
072        }
073    }
074
075    /**
076     * Gets the {@link I18nizableText} for subject of the mail
077     * @param project the project
078     * @return the subject
079     */
080    protected I18nizableText getSubjectI18nizableText(Project project)
081    {
082        return new I18nizableText("plugin." + _pluginName, getSubjectI18nKey(), getSubjectParams(project));
083    }
084    
085    /**
086     * Gets the i18n subject key
087     * @return the i18n subject key
088     */
089    protected String getSubjectI18nKey()
090    {
091        return "PROJECT_MAIL_NOTIFICATION_SUBJECT_MEMBER_REMOVED";
092    }
093    
094    /**
095     * Gets the i18n parameters for subject key
096     * @param project the project
097     * @return the i18n parameters
098     */
099    protected List<String> getSubjectParams(Project project)
100    {
101        return getI18nParams(project.getTitle());
102    }
103    
104    /**
105     * Gets the {@link I18nizableText} for body of the mail
106     * @param project the project
107     * @return the body
108     */
109    protected I18nizableText getBodyI18nizableText(Project project)
110    {
111        return new I18nizableText("plugin." + _pluginName, getBodyI18nKey(), getBodyParams(project));
112    }
113    
114    /**
115     * Gets the i18n body key
116     * @return the i18n body key
117     */
118    protected String getBodyI18nKey()
119    {
120        return "PROJECT_MAIL_NOTIFICATION_BODY_MEMBER_REMOVED";
121    }
122    
123    /**
124     * Gets the i18n parameters for body key
125     * @param project the project
126     * @return the i18n parameters
127     */
128    protected List<String> getBodyParams(Project project)
129    {
130        return getI18nParams(
131                project.getTitle(), // {0}
132                getProjectManagersNames(project), // {1}
133                getProjectsCatalogUrl()); // {2}
134    }
135
136    /**
137     * Get the list of names of the project managers
138     * @param project The project
139     * @return The list of names, as a single string, comma separated
140     */
141    protected String getProjectManagersNames(Project project)
142    {
143        return Arrays.stream(project.getManagers())
144                .map(_userManager::getUser)
145                .filter(Objects::nonNull)
146                .map(User::getFullName)
147                .collect(Collectors.joining(", "));
148    }
149}