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.List;
019import java.util.Map;
020import java.util.Optional;
021import java.util.stream.Collectors;
022
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.core.group.Group;
026import org.ametys.core.group.GroupIdentity;
027import org.ametys.core.observation.Event;
028import org.ametys.core.user.User;
029import org.ametys.core.user.UserIdentity;
030import org.ametys.core.util.LambdaUtils.BiPredicate;
031import org.ametys.plugins.workspaces.ObservationConstants;
032import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
033import org.ametys.plugins.workspaces.project.objects.Project;
034
035/**
036 * Abstract Observer to send mail notifications on workspace member removal
037 */
038public abstract class AbstractRemoveMemberMailNotifierObserver extends AbstractMemberMailNotifierObserver
039{
040    public boolean supports(Event event)
041    {
042        return event.getId().equals(ObservationConstants.EVENT_MEMBER_DELETED);
043    }
044
045    @Override
046    protected List<String> getUserToNotify(Event event, Project project)
047    {
048        // Retrieve removed member
049        Map<String, Object> args = event.getArguments();
050        String identity = (String) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY);
051        MemberType identityType = (MemberType) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY_TYPE);
052        
053        if (MemberType.USER == identityType)
054        {
055            return Optional.of(identity)
056                    .map(UserIdentity::stringToUserIdentity)
057                    .filter(userIdentity -> !_projectMemberManager.isProjectMember(project, userIdentity)) // user is not in a group that is still part of the project
058                    .map(_userManager::getUser)
059                    .map(User::getEmail)
060                    .filter(StringUtils::isNotEmpty)
061                    .map(e -> List.of(e))
062                    .orElse(List.of());
063        }
064        else if (MemberType.GROUP == identityType)
065        {
066            GroupIdentity groupIdentity = GroupIdentity.stringToGroupIdentity(identity);
067            if (groupIdentity != null)
068            {
069                Group group = _groupManager.getGroup(groupIdentity);
070                if (group != null)
071                {
072                    List<User> removedUsers = _projectMemberManager.getGroupUsersFromProject(group, project, BiPredicate.not(_projectMemberManager::isProjectMember)); // Removed user
073                    return removedUsers.stream()
074                            .map(User::getEmail)
075                            .filter(StringUtils::isNotEmpty)
076                            .collect(Collectors.toList());
077                }
078            }
079        }
080        
081        return List.of();
082    }
083    
084}