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.Objects;
021import java.util.stream.Collectors;
022
023import org.apache.commons.lang.StringUtils;
024
025import org.ametys.core.observation.Event;
026import org.ametys.core.user.User;
027import org.ametys.plugins.workspaces.ObservationConstants;
028import org.ametys.plugins.workspaces.project.objects.Project;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * Observer to send mail notifications on workspace member removal
033 */
034public class RemoveMemberMailManagersNotifierObserver extends AbstractMemberMailNotifierObserver
035{
036    public boolean supports(Event event)
037    {
038        return event.getId().equals(ObservationConstants.EVENT_MEMBER_DELETED);
039    }
040
041    @Override
042    protected String getMailBodyURI(Event event, Project project)
043    {
044        return "cocoon://_plugins/workspaces/notification-mail-member-removed-manager";
045    }
046    
047    @Override
048    protected List<String> getUserToNotify(Event event, Project project)
049    {
050        // Recipients are project managers
051        return Arrays.stream(project.getManagers())
052                .map(_userManager::getUser)
053                .filter(Objects::nonNull)
054                .map(User::getEmail)
055                .filter(StringUtils::isNotEmpty)
056                .collect(Collectors.toList());
057    }
058    
059    @Override
060    protected I18nizableText getI18nSubject(Event event, Project project)
061    {
062        return new I18nizableText("plugin." + _pluginName, "PROJECT_MAIL_MANAGER_NOTIFICATION_SUBJECT_MEMBER_DELETED", List.of(project.getTitle()));
063    }
064}