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