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;
017
018import java.util.ArrayList;
019import java.util.List;
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.runtime.i18n.I18nizableText;
026import org.ametys.runtime.plugins.admin.notificator.AbstractConfigurableAdministratorNotificator;
027import org.ametys.runtime.plugins.admin.notificator.Notification;
028import org.ametys.web.repository.site.Site;
029import org.ametys.web.repository.site.SiteManager;
030import org.ametys.web.site.SiteConfigurationManager;
031
032/**
033 * Notificator for sites which are misconfigured.
034 */
035public class MisconfiguredSitesNotificator extends AbstractConfigurableAdministratorNotificator implements Serviceable
036{
037    private SiteManager _siteManager;
038    private SiteConfigurationManager _siteConfigurationManager;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
044        _siteConfigurationManager = (SiteConfigurationManager) manager.lookup(SiteConfigurationManager.ROLE);
045    }
046    
047    @Override
048    public List<Notification> getNotifications()
049    {
050        List<Notification> notifications = new ArrayList<>();
051       
052        for (String siteName : _siteManager.getSiteNames())
053        {
054            if (!_siteConfigurationManager.isSiteConfigurationValid(siteName))
055            {
056                Site site = _siteManager.getSite(siteName);
057                String siteTitle = site.getTitle() != null ? site.getTitle() : "";
058                Notification notification = new Notification(_type, _title, _getMessage(siteName, siteTitle), _iconGlyph, _getAction(site.getId(), siteName, siteTitle));
059                notifications.add(notification);
060            }
061        }
062        return notifications;
063    }
064    
065    private I18nizableText _getMessage (String siteName, String siteTitle)
066    {
067        List<String> i18nParams = new ArrayList<>();
068        i18nParams.add(siteTitle);
069        i18nParams.add(siteName);
070        
071        return new I18nizableText(_message.getCatalogue(), _message.getKey(), i18nParams);
072    }
073    
074    private String _getAction (String siteId, String siteName, String siteTitle)
075    {
076        return "Ext.bind(Ametys.tool.ToolsManager.openTool, Ametys.tool.ToolsManager, ['uitool-admin-site-config', { id: '" + siteName + "', siteId: '" + siteId + "', siteName: '" + siteName + "', siteTitle: '" + siteTitle + "'}])";
077    }
078}