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.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021import java.util.Objects;
022import java.util.stream.Collectors;
023
024import javax.mail.MessagingException;
025
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.core.user.User;
029import org.ametys.core.util.mail.SendMailHelper;
030import org.ametys.plugins.workspaces.project.objects.Project;
031import org.ametys.runtime.config.Config;
032import org.ametys.runtime.i18n.I18nizableText;
033
034/**
035 * Observer to send mail notifications on workspace member removal
036 */
037public class RemoveMemberMailManagersNotifierObserver extends AbstractRemoveMemberMailNotifierObserver
038{
039    @Override
040    protected void sendMail(Project project, List<User> removedUsers)
041    {
042        // Retrieve mail sender
043        String sender = Config.getInstance().getValue("smtp.mail.from");
044
045        // Compute subject and body
046        String subjectForManager = _i18nUtils.translate(getSubjectI18nizableText(project, removedUsers));
047        String bodyForManager = _i18nUtils.translate(getBodyI18nizableText(project, removedUsers));
048        
049        // Send mail to project managers
050        for (String recipient : _getProjectManagerEmails(project))
051        {
052            try
053            {
054                SendMailHelper.sendMail(subjectForManager, null, bodyForManager, recipient, sender, true);
055            }
056            catch (MessagingException e)
057            {
058                getLogger().warn("Could not send a notification e-mail to the project manager " + recipient + " to inform him of the removal of members of the project " + project.getTitle(), e);
059            }
060        }
061    }
062    
063    private List<String> _getProjectManagerEmails(Project project)
064    {
065        return Arrays.stream(project.getManagers())
066                .map(_userManager::getUser)
067                .filter(Objects::nonNull)
068                .map(User::getEmail)
069                .filter(StringUtils::isNotEmpty)
070                .collect(Collectors.toList());
071    }
072
073    /**
074     * Gets the {@link I18nizableText} for subject of the mail
075     * @param project the project
076     * @param removedUsers the users removed
077     * @return the subject
078     */
079    protected I18nizableText getSubjectI18nizableText(Project project, List<User> removedUsers)
080    {
081        return new I18nizableText("plugin." + _pluginName, getSubjectI18nKey(), getSubjectParams(project, removedUsers));
082    }
083    
084    /**
085     * Gets the i18n subject key
086     * @return the i18n subject key
087     */
088    protected String getSubjectI18nKey()
089    {
090        return "PROJECT_MAIL_NOTIFICATION_SUBJECT_PROJECT_MANAGER_MEMBER_REMOVED";
091    }
092    
093    /**
094     * Gets the i18n parameters for subject key
095     * @param project the project
096     * @param removedUsers the users removed
097     * @return the i18n parameters
098     */
099    protected List<String> getSubjectParams(Project project, List<User> removedUsers)
100    {
101        List<String> i18nParams = new ArrayList<>();
102        i18nParams.add(StringUtils.defaultString(project.getTitle())); // {0}
103        return i18nParams;
104    }
105    
106    /**
107     * Gets the {@link I18nizableText} for body of the mail
108     * @param project the project
109     * @param removedUsers the users removed
110     * @return the body
111     */
112    protected I18nizableText getBodyI18nizableText(Project project, List<User> removedUsers)
113    {
114        return new I18nizableText("plugin." + _pluginName, getBodyI18nKey(), getBodyParams(project, removedUsers));
115    }
116    
117    /**
118     * Gets the i18n body key
119     * @return the i18n body key
120     */
121    protected String getBodyI18nKey()
122    {
123        return "PROJECT_MAIL_NOTIFICATION_BODY_PROJECT_MANAGER_MEMBER_REMOVED";
124    }
125    
126    /**
127     * Gets the i18n parameters for body key
128     * @param project the project
129     * @param removedUsers the users removed
130     * @return the i18n parameters
131     */
132    protected List<String> getBodyParams(Project project, List<User> removedUsers)
133    {
134        String removedUsersName = removedUsers.stream()
135                .map(User::getFullName)
136                .collect(Collectors.joining(", "));
137        
138        return getI18nParams(
139                project.getTitle(), // {0}
140                removedUsersName, // {1}
141                getProjectUrl(project)); // {2}
142    }
143
144}