001/*
002 *  Copyright 2023 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.web.group.directory;
017
018import java.util.List;
019import java.util.stream.Collectors;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026
027import org.ametys.core.group.directory.GroupDirectory;
028import org.ametys.core.user.population.UserPopulation;
029import org.ametys.web.WebHelper;
030
031/**
032 * Implementation of a {@link GroupDirectory} based on user's populations restricted to the site contexts
033 *
034 */
035public class UserPopulationsGroupDirectory extends org.ametys.cms.group.directory.UserPopulationsGroupDirectory implements Contextualizable
036{
037    private Context _context;
038
039    public void contextualize(Context context) throws ContextException
040    {
041        _context = context;
042    }
043    
044    @Override
045    protected List<UserPopulation> getAvailableUserPopulationsForContext()
046    {
047        Request request = ContextHelper.getRequest(_context);
048        
049        if (request != null)
050        {
051            String siteName = WebHelper.getSiteName(request);
052            if (siteName != null)
053            {
054                return _populationContextHelper.getUserPopulationsOnContexts(List.of("/sites/" + siteName, "/sites-fo/" + siteName), false, false)
055                    .stream()
056                    .map(_userPopulationDAO::getUserPopulation)
057                    .collect(Collectors.toList());
058            }
059            
060        }
061        
062        return super.getAvailableUserPopulationsForContext();
063    }
064    
065}