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