001/*
002 *  Copyright 2017 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.plugins.odfweb.alerts;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.repository.Content;
022import org.ametys.odf.program.Program;
023import org.ametys.plugins.odfweb.repository.OdfPageResolver;
024import org.ametys.runtime.config.Config;
025import org.ametys.web.repository.page.Page;
026import org.ametys.web.repository.site.Site;
027import org.ametys.web.repository.site.SiteManager;
028
029/**
030 * Alerts engine: sends alerts mail.
031 * This is the web version of the engine: it sets the currently processed content's
032 * site name in the request object, and sends additional site and page information
033 * in the alerts e-mails.
034 */
035public class AlertSchedulable extends org.ametys.web.alerts.AlertSchedulable
036{
037    /** The ODF page resolver */
038    protected OdfPageResolver _odfPageResolver;
039    /** The site manager */
040    protected SiteManager _siteManager;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
047        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
048    }
049    
050    @Override
051    protected Site _getSite(Content content)
052    {
053        Site site = super._getSite(content);
054        if (site == null)
055        {
056            String siteName = Config.getInstance().getValue("odf.web.site.name");
057            site = _siteManager.getSite(siteName);
058        }
059        
060        return site;
061    }
062    
063    @Override
064    protected Page _getPage(Content content, Site site)
065    {
066        if (content instanceof Program)
067        {
068            return _odfPageResolver.getProgramPage((Program) content, site.getName());
069        }
070        else
071        {
072            return super._getPage(content, site);
073        }
074    }
075}