001/*
002 *  Copyright 2020 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.odf.export.pdf;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.generation.AbstractGenerator;
027import org.apache.cocoon.serialization.ZipArchiveSerializer;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang.StringUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.util.I18nUtils;
034import org.ametys.odf.program.SubProgram;
035import org.ametys.plugins.repository.AmetysObjectResolver;
036import org.ametys.runtime.i18n.I18nizableText;
037
038/**
039 * Generate the list of booklet to include in a zip for downloading
040 */
041public class EducationalBookletZipGenerator extends AbstractGenerator implements Serviceable
042{
043    /** The Ametys Object resolver */
044    protected AmetysObjectResolver _resolver;
045    
046    /** The i18n utils */
047    protected I18nUtils _i18nUtils;
048    
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
052        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
053    }
054    
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        Map parentContextParameters = (Map) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
058        if (parentContextParameters == null)
059        {
060            throw new ProcessingException("Missing parent context");
061        }
062
063        String subProgramIdsAsString = (String) parentContextParameters.get("subProgramIds");
064        String[] subProgramIds = StringUtils.split(subProgramIdsAsString, ",");
065        
066        contentHandler.startDocument();
067        contentHandler.startPrefixMapping("zip", ZipArchiveSerializer.ZIP_NAMESPACE);
068        XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive");
069        for (String subProgramId : subProgramIds)
070        {
071            SubProgram subProgram = _resolver.resolveById(subProgramId);
072            String prefix = _i18nUtils.translate(new I18nizableText("plugin.odf", "PLUGINS_ODF_EDUCATIONAL_BOOKLET_SUBPROGRAM_PDF_NAME_PREFIX"), subProgram.getLanguage());
073            String fileName = prefix + " " + subProgram.getTitle() + ".pdf";
074            
075            AttributesImpl zipAttrs = new AttributesImpl();
076            zipAttrs.addAttribute("", "name", "name", "CDATA", fileName);
077            zipAttrs.addAttribute("", "src", "src", "CDATA", "ametys-home://data/odf/booklet/" + subProgram.getName() + "/" + subProgram.getLanguage() + "/educational-booklet.pdf");
078            XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry", zipAttrs);
079            XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry");
080        }
081        
082        XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive");
083        contentHandler.endPrefixMapping("zip");
084        contentHandler.endDocument();
085    }
086}