001/*
002 *  Copyright 2021 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.members.observers;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023import java.util.stream.Collectors;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.cms.repository.Content;
030import org.ametys.core.group.Group;
031import org.ametys.core.group.GroupIdentity;
032import org.ametys.core.group.GroupManager;
033import org.ametys.core.observation.AsyncObserver;
034import org.ametys.core.observation.Event;
035import org.ametys.core.user.UserIdentity;
036import org.ametys.plugins.userdirectory.UserDirectoryHelper;
037import org.ametys.plugins.workspaces.ObservationConstants;
038import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
039import org.ametys.plugins.workspaces.project.ProjectManager;
040import org.ametys.runtime.plugin.component.AbstractLogEnabled;
041import org.ametys.web.repository.site.Site;
042import org.ametys.web.repository.site.SiteManager;
043import org.ametys.web.repository.sitemap.Sitemap;
044
045/**
046 * Abstract observer for member update
047 *
048 */
049public abstract class AbstractMemberObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable
050{
051    /** The service manager */
052    protected ServiceManager _smanager;
053    /** The group manager */
054    protected GroupManager _groupManager;
055    /** The user directory helper */
056    protected UserDirectoryHelper _userDirectoryHelper;
057    /** The site manager */
058    protected SiteManager _siteManager;
059    /** The project manager */
060    protected ProjectManager _projectManager;
061    
062    public void service(ServiceManager smanager) throws ServiceException
063    {
064        _smanager = smanager;
065        _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE);
066        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
067        _projectManager = (ProjectManager) smanager.lookup(ProjectManager.ROLE);
068    }
069
070    public boolean supports(Event event)
071    {
072        String eventId = event.getId();
073        return eventId.equals(ObservationConstants.EVENT_MEMBER_ADDED) || eventId.equals(ObservationConstants.EVENT_MEMBER_DELETED);
074    }
075
076    public int getPriority(Event event)
077    {
078        return MIN_PRIORITY;
079    }
080
081    public void observe(Event event, Map<String, Object> transientVars) throws Exception
082    {
083        // Lazy Loading
084        if (_groupManager == null)
085        {
086            _groupManager = (GroupManager) _smanager.lookup(GroupManager.ROLE);
087        }
088        
089        Map<String, Object> args = event.getArguments();
090
091        MemberType type = (MemberType) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY_TYPE);
092        String identity = (String) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY);
093        
094        Set<UserIdentity> users = null;
095        
096        if (MemberType.USER == type)
097        {
098            UserIdentity user = UserIdentity.stringToUserIdentity(identity);
099            users = Collections.singleton(user);
100        }
101        else
102        {
103            // Group
104            Group group = _groupManager.getGroup(GroupIdentity.stringToGroupIdentity(identity));
105            users = group.getUsers();
106        }
107        
108        List<Content> userContents = getUserContents(users);
109        _internalObserve(event, userContents);
110    }
111    
112    /**
113     * Observes the event with access to the user contents
114     * @param event the event.
115     * @param userContents the user contents
116     * @throws Exception if an error occurs.
117     */
118    protected abstract void _internalObserve(Event event, List<Content> userContents) throws Exception;
119    
120    /**
121     * Get the user contents associated to user identities
122     * @param users the users
123     * @return the user contents
124     */
125    protected List<Content> getUserContents(Set<UserIdentity> users)
126    {
127        String catalogSiteName = _projectManager.getCatalogSiteName();
128        
129        List<Content> contents = new ArrayList<>();
130        
131        Site site = _siteManager.getSite(catalogSiteName);
132        if (site != null)
133        {
134            Set<String> languages = site.getSitemaps().stream()
135                .map(Sitemap::getName)
136                .collect(Collectors.toSet());
137            
138            for (UserIdentity user : users)
139            {
140                for (String lang : languages)
141                {
142                    Content userContent = _userDirectoryHelper.getUserContent(user, lang);
143                    if (userContent != null)
144                    {
145                        contents.add(userContent);
146                    }
147                }
148            }
149        }
150        
151        return contents;
152    }
153}