001/*
002 *  Copyright 2022 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.preferences;
017
018import java.util.Collection;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.core.observation.AsyncObserver;
026import org.ametys.core.observation.Event;
027import org.ametys.plugins.workspaces.ObservationConstants;
028import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
029import org.ametys.plugins.workspaces.members.ProjectMemberManager.ProjectMember;
030
031/**
032 * Observer removing custom notification preferences on project deletion
033 */
034public class ProjectDeletedObserver implements AsyncObserver, Serviceable
035{
036    private NotificationPreferencesHelper _notificationPreferenceHelper;
037
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        _notificationPreferenceHelper = (NotificationPreferencesHelper) manager.lookup(NotificationPreferencesHelper.ROLE);
041    }
042    
043    public boolean supports(Event event)
044    {
045        return ObservationConstants.EVENT_PROJECT_DELETED.equals(event.getId());
046    }
047
048    public int getPriority(Event event)
049    {
050        return Integer.MAX_VALUE;
051    }
052
053    public void observe(Event event, Map<String, Object> transientVars) throws Exception
054    {
055        String projectName = (String) event.getArguments().get(ObservationConstants.ARGS_PROJECT_NAME);
056        @SuppressWarnings("unchecked")
057        Collection<ProjectMember> projectMembers = (Collection<ProjectMember>) event.getArguments().get(ObservationConstants.ARGS_PROJECT_MEMBERS);
058        for (ProjectMember projectMember : projectMembers)
059        {
060            if (projectMember.getType() == MemberType.USER)
061            {
062                _notificationPreferenceHelper.deleteProjectNotificationPreferences(projectMember.getUser().getIdentity(), projectName);
063            }
064        }
065    }
066
067}