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.workflow;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.repository.WorkflowAwareContent;
022import org.ametys.odf.course.Course;
023import org.ametys.odf.program.AbstractProgram;
024import org.ametys.odf.program.Program;
025import org.ametys.odf.program.SubProgram;
026import org.ametys.plugins.odfweb.repository.OdfPageResolver;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.web.repository.page.Page;
029import org.ametys.web.repository.site.Site;
030import org.ametys.web.repository.site.SiteManager;
031
032/**
033 * OS workflow function to send mail after an action is triggered on ODF contents
034 */
035public class SendMailFunction extends org.ametys.web.workflow.SendMailFunction
036{
037    private OdfPageResolver _odfPageResolver;
038    private SiteManager _siteManager;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
045        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
046    }
047    
048    @Override
049    protected Site _getSite(WorkflowAwareContent content)
050    {
051        Site site = super._getSite(content);
052        if (site == null)
053        {
054            String siteName = (String) _getRequest().getAttribute("siteName");
055            if (siteName != null)
056            {
057                site = _siteManager.getSite(siteName);
058            }
059        }
060        return site;
061    }
062    
063    @Override
064    protected Page _getPage(WorkflowAwareContent content, Site site)
065    {
066        if (content instanceof Program)
067        {
068            return _odfPageResolver.getProgramPage((Program) content, site.getName());
069        }
070        else if (content instanceof AbstractProgram)
071        {
072            return _odfPageResolver.getSubProgramPage((SubProgram) content, null, site.getName());
073        }
074        else if (content instanceof Course)
075        {
076            return _odfPageResolver.getCoursePage((Course) content, (AbstractProgram) null, site.getName());
077        }
078        else
079        {
080            return super._getPage(content, site);
081        }
082    } 
083    
084    @Override
085    public I18nizableText getLabel()
086    {
087        return new I18nizableText("plugin.odf-web", "PLUGINS_ODF_WEB_SEND_MAIL_FUNCTION_LABEL");
088    }
089}