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