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 org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.avalon.framework.service.Serviceable;
021
022import org.ametys.web.repository.site.SiteManager;
023import org.ametys.web.site.SiteConfigurationManager;
024
025/**
026 * This step indicates that there is at least one well configured site.
027 */
028public class CompleteSiteStep extends AbstractWelcomeStep implements Serviceable
029{
030    private ServiceManager _manager;
031    private SiteManager _siteManager;
032    private SiteConfigurationManager _siteConfigurationManager;
033    
034    @Override
035    public void service(ServiceManager manager) throws ServiceException
036    {
037        _manager = manager;
038    }
039    
040    @Override
041    public boolean isPerformed()
042    {
043        if (_siteManager == null)
044        {
045            try
046            {
047                _siteManager = (SiteManager) _manager.lookup(SiteManager.ROLE);
048            }
049            catch (ServiceException e)
050            {
051                // The component cannot be looked up in safe mode
052                return false;
053            }
054        }
055        if (_siteConfigurationManager == null)
056        {
057            try
058            {
059                _siteConfigurationManager = (SiteConfigurationManager) _manager.lookup(SiteConfigurationManager.ROLE);
060            }
061            catch (ServiceException e)
062            {
063                // The component cannot be looked up in safe mode
064                return false;
065            }
066        }
067        
068        return _siteManager.getSiteNames()
069                .stream()
070                .anyMatch(_siteConfigurationManager::isSiteConfigurationValid);
071    }
072}