001/*
002 *  Copyright 2010 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.program.generators;
017
018import java.io.IOException;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.cocoon.components.source.impl.SitemapSource;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.generation.Generator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.ContentHandler;
029import org.xml.sax.SAXException;
030
031import org.ametys.cms.CmsConstants;
032import org.ametys.cms.repository.Content;
033import org.ametys.core.util.DateUtils;
034import org.ametys.core.util.IgnoreRootHandler;
035import org.ametys.odf.ODFHelper;
036import org.ametys.odf.program.Program;
037import org.ametys.odf.program.SubProgram;
038import org.ametys.plugins.repository.UnknownAmetysObjectException;
039
040/**
041 * {@link Generator} for rendering raw content data for a {@link Program}, to 
042 */
043public class ProgramPdfContentGenerator extends ProgramContentGenerator
044{
045    /**
046     * SAX a sub program
047     * @param subProgram the sub program to SAX
048     * @param parentPath the parent path
049     * @throws SAXException if an error occurs
050     */
051    @Override
052    protected void saxSubProgram (SubProgram subProgram, String parentPath) throws SAXException
053    {
054        try
055        {
056            AttributesImpl attrs = new AttributesImpl();
057            attrs.addCDATAAttribute("title", subProgram.getTitle());
058            attrs.addCDATAAttribute("code", subProgram.getCode());
059            attrs.addCDATAAttribute("id", subProgram.getId());
060            if (parentPath != null)
061            {
062                attrs.addCDATAAttribute("path", parentPath + "/" + subProgram.getName());
063            }
064            XMLUtils.startElement(contentHandler, "subprogram", attrs);
065            saxContent(subProgram, contentHandler, "abstract");  
066            XMLUtils.endElement(contentHandler, "subprogram");
067        }
068        catch (IOException e)
069        {
070            throw new SAXException(e);
071        }
072        catch (UnknownAmetysObjectException e)
073        {
074            throw new SAXException(e);
075        }
076    }
077    
078    
079    @Override
080    protected void saxContent (Content content, ContentHandler handler, String viewName) throws SAXException, IOException
081    {
082        Request request = ObjectModelHelper.getRequest(objectModel);
083        
084        AttributesImpl attrs = new AttributesImpl();
085        attrs.addCDATAAttribute("id", content.getId());
086        attrs.addCDATAAttribute("name", content.getName());
087        attrs.addCDATAAttribute("title", content.getTitle());
088        attrs.addCDATAAttribute("lastModifiedAt", DateUtils.zonedDateTimeToString(content.getLastModified()));
089        
090        XMLUtils.startElement(handler, "content", attrs);
091        
092        Map<String, String> params = new HashMap<>();
093        if (request.getAttribute(ODFHelper.REQUEST_ATTRIBUTE_VALID_LABEL) != null)
094        {
095            params.put("versionLabel", CmsConstants.LIVE_LABEL);
096        }
097        
098        String uri = _contentHelper.getContentViewUrl(content, viewName, "xml", params);
099        SitemapSource src = null;
100        
101        try
102        {
103            src = (SitemapSource) _srcResolver.resolveURI(uri);
104            src.toSAX(new IgnoreRootHandler(handler));
105        }
106        finally
107        {
108            _srcResolver.release(src);
109        }
110        
111        XMLUtils.endElement(handler, "content");
112    }
113}