001/*
002 *  Copyright 2014 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.catalog;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.web.WebConstants;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033
034/**
035 * This engine export the PDF catalog and send it by email.
036 */
037public class GeneratePDFEngine extends org.ametys.odf.export.pdf.GeneratePDFEngine
038{
039    private SiteManager _siteManager;
040    
041    @Override
042    public void initialize(ServiceManager manager, Context context) throws ContextException, ServiceException
043    {
044        super.initialize(manager, context);
045        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
046    }
047    
048    @Override
049    protected void _doGeneratePDF(Request request) throws IOException
050    {
051        String workspaceBackup = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
052        try
053        {
054            // Force live workspace
055            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE);
056            
057            request.setAttribute("site", getSiteName());
058            request.setAttribute("siteName", getSiteName());
059            
060            Site site = _siteManager.getSite(getSiteName());
061            request.setAttribute("skin", site.getSkinId());
062            
063            super._doGeneratePDF(request);
064        }
065        finally
066        {
067            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, workspaceBackup);
068        }
069    }
070    
071    @Override
072    protected String getMailSubject()
073    {
074        Site site = _siteManager.getSite(getSiteName());
075        
076        List<String> params = new ArrayList<>();
077        params.add(site.getTitle()); // {0}
078        
079        I18nizableText subjectKey = new I18nizableText("plugin.odf-web", "PLUGINS_ODFWEB_PDF_EXPORT_MAIL_SUBJECT", params);
080        return _i18nUtils.translate(subjectKey, null); // FIXME Use user preference language
081    }
082    
083    /**
084     * Get the site name
085     * @return the site name
086     */
087    protected String getSiteName()
088    {
089        return (String) _contextualParameters.get("siteName");
090    }
091    
092}