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.web.administration.welcome;
017
018import java.util.List;
019import java.util.stream.Collectors;
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.user.population.PopulationContextHelper;
026import org.ametys.web.repository.site.SiteManager;
027import org.ametys.web.site.SiteConfigurationManager;
028
029/**
030 * This step indicates that there is at least one site well configured and with at least one affected population.
031 */
032public class SiteWithPopulationStep extends AbstractWelcomeStep implements Serviceable
033{
034    private ServiceManager _manager;
035    private SiteManager _siteManager;
036    private SiteConfigurationManager _siteConfigurationManager;
037    private PopulationContextHelper _populationContextHelper;
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        _manager = manager;
043        _populationContextHelper = (PopulationContextHelper) _manager.lookup(PopulationContextHelper.ROLE);
044    }
045    
046    @Override
047    public boolean isPerformed()
048    {
049        if (_siteManager == null)
050        {
051            try
052            {
053                _siteManager = (SiteManager) _manager.lookup(SiteManager.ROLE);
054            }
055            catch (ServiceException e)
056            {
057                // The component cannot be looked up in safe mode
058                return false;
059            }
060        }
061        if (_siteConfigurationManager == null)
062        {
063            try
064            {
065                _siteConfigurationManager = (SiteConfigurationManager) _manager.lookup(SiteConfigurationManager.ROLE);
066            }
067            catch (ServiceException e)
068            {
069                // The component cannot be looked up in safe mode
070                return false;
071            }
072        }
073        
074        List<String> configuredSites = _siteManager.getSiteNames()
075                .stream()
076                .filter(_siteConfigurationManager::isSiteConfigurationValid)
077                .collect(Collectors.toList());
078        
079        return configuredSites.stream()
080                .anyMatch(siteName -> !_populationContextHelper.getUserPopulationsOnContext("/sites/" + siteName, false).isEmpty());
081    }
082}