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 java.util.List;
019
020import org.ametys.cms.repository.Content;
021import org.ametys.odf.program.Program;
022import org.ametys.plugins.odfweb.repository.OdfPageResolver;
023import org.ametys.runtime.config.Config;
024import org.ametys.web.repository.page.Page;
025import org.ametys.web.repository.site.Site;
026import org.ametys.web.repository.site.SiteManager;
027import org.apache.avalon.framework.context.Context;
028import org.apache.avalon.framework.context.ContextException;
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031
032/**
033 * Alerts engine: sends alerts mail.
034 * This is the web version of the engine: it sets the currently processed content's
035 * site name in the request object, and sends additional site and page information
036 * in the alerts e-mails.
037 */
038public class AlertEngine extends org.ametys.web.alerts.AlertEngine
039{
040    /** The ODF page resolver */
041    protected OdfPageResolver _odfPageResolver;
042    /** The site manager */
043    protected SiteManager _siteManager;
044    
045    /**
046     * Default constructor
047     */
048    public AlertEngine()
049    {
050        super();
051    }
052
053    /**
054     * Constructor used to send instant alerts
055     * @param contentIds The content's id
056     * @param message the message
057     */
058    public AlertEngine (List<String> contentIds, String message)
059    {
060        super(contentIds, message);
061    }
062
063    @Override
064    public void initialize(ServiceManager manager, Context context) throws ContextException, ServiceException
065    {
066        super.initialize(manager, context);
067        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
068        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
069    }
070    
071    @Override
072    protected Site _getSite(Content content)
073    {
074        Site site = super._getSite(content);
075        if (site == null)
076        {
077            String siteName = Config.getInstance().getValueAsString("odf.web.site.name");
078            site = _siteManager.getSite(siteName);
079        }
080        
081        return site;
082    }
083
084    @Override
085    protected Page _getPage(Content content, Site site)
086    {
087        if (content instanceof Program)
088        {
089            return _odfPageResolver.getProgramPage((Program) content, site.getName());
090        }
091        else
092        {
093            return super._getPage(content, site);
094        }
095    }
096}