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