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.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.core.observation.Event;
028import org.ametys.core.user.User;
029import org.ametys.plugins.workspaces.project.notification.preferences.NotificationPreferencesHelper;
030import org.ametys.plugins.workspaces.project.notification.preferences.NotificationPreferencesHelper.Frequency;
031import org.ametys.plugins.workspaces.project.objects.Project;
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    private NotificationPreferencesHelper _notificationPrefHelper;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _notificationPrefHelper = (NotificationPreferencesHelper) manager.lookup(NotificationPreferencesHelper.ROLE);
046    }
047    @Override
048    protected String getMailBodyURI(Event event, Project project)
049    {
050        return "cocoon://_plugins/workspaces/notification-mail-member-manager";
051    }
052    
053    @Override
054    protected List<String> getUserToNotify(Event event, Project project)
055    {
056        // Recipients are project managers
057        return Arrays.stream(project.getManagers())
058                .filter(userId -> _notificationPrefHelper.askedToBeNotified(userId, project.getName(), Frequency.EACH))
059                .map(_userManager::getUser)
060                .filter(Objects::nonNull)
061                .map(User::getEmail)
062                .filter(StringUtils::isNotEmpty)
063                .collect(Collectors.toList());
064    }
065    
066    @Override
067    protected I18nizableText getI18nSubject(Event event, Project project)
068    {
069        return new I18nizableText("plugin." + _pluginName, "PROJECT_MAIL_NOTIFICATION_SUBJECT_PROJECT_MANAGER_MEMBER_DELETED", List.of(project.getTitle()));
070    }
071}