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