001/*
002 *  Copyright 2016 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.core.right;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.core.ObservationConstants;
025import org.ametys.core.group.GroupIdentity;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.core.user.UserIdentity;
029
030/**
031 * This observer listens for events which may have an impact on the storage of assignments
032 *
033 */
034public class ProfileAssignmentStorageObserver implements Serviceable, Observer
035{
036    private ProfileAssignmentStorageExtensionPoint _profileAssignmentStorageEP;
037
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _profileAssignmentStorageEP = (ProfileAssignmentStorageExtensionPoint) manager.lookup(ProfileAssignmentStorageExtensionPoint.ROLE);
042    }
043    
044    @Override
045    public boolean supports(Event event)
046    {
047        return event.getId().equals(ObservationConstants.EVENT_PROFILE_DELETED)
048                || event.getId().equals(ObservationConstants.EVENT_USER_DELETED)
049                || event.getId().equals(ObservationConstants.EVENT_GROUP_DELETED);
050    }
051
052    @Override
053    public int getPriority(Event event)
054    {
055        return MAX_PRIORITY;
056    }
057
058    @Override
059    public void observe(Event event, Map<String, Object> transientVars) throws Exception
060    {
061        String id = event.getId();
062        
063        if (id.equals(ObservationConstants.EVENT_PROFILE_DELETED))
064        {
065            _onProfileRemoved (event);
066        }
067        else if (id.equals(ObservationConstants.EVENT_USER_DELETED))
068        {
069            _onUserRemoved(event);
070        }
071        else if (id.equals(ObservationConstants.EVENT_GROUP_DELETED))
072        {
073            _onGroupRemoved(event);
074        }
075    }
076    
077    private void _onProfileRemoved (Event event)
078    {
079        Map<String, Object> arguments = event.getArguments();
080        
081        Profile profile = (Profile) arguments.get(ObservationConstants.ARGS_PROFILE);
082        _profileAssignmentStorageEP.getExtensionsIds().stream()
083            .map(_profileAssignmentStorageEP::getExtension)
084            .filter(pas -> pas instanceof ModifiableProfileAssignmentStorage)
085            .map(ModifiableProfileAssignmentStorage.class::cast)
086            .forEach(pas -> pas.removeProfile(profile.getId()));
087    }
088    
089    private void _onUserRemoved (Event event)
090    {
091        Map<String, Object> arguments = event.getArguments();
092        
093        UserIdentity user = (UserIdentity) arguments.get(ObservationConstants.ARGS_USER);
094        _profileAssignmentStorageEP.getExtensionsIds().stream()
095            .map(_profileAssignmentStorageEP::getExtension)
096            .filter(pas -> pas instanceof ModifiableProfileAssignmentStorage)
097            .map(ModifiableProfileAssignmentStorage.class::cast)
098            .forEach(pas -> pas.removeUser(user));
099    }
100    
101    private void _onGroupRemoved (Event event)
102    {
103        Map<String, Object> arguments = event.getArguments();
104        
105        GroupIdentity group = (GroupIdentity) arguments.get(ObservationConstants.ARGS_GROUP);
106        _profileAssignmentStorageEP.getExtensionsIds().stream()
107            .map(_profileAssignmentStorageEP::getExtension)
108            .filter(pas -> pas instanceof ModifiableProfileAssignmentStorage)
109            .map(ModifiableProfileAssignmentStorage.class::cast)
110            .forEach(pas -> pas.removeGroup(group));
111    }
112}