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.odf.export.pdf;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.Collection;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.ParameterException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.components.source.impl.SitemapSource;
028import org.apache.cocoon.generation.ServiceableGenerator;
029import org.apache.cocoon.xml.AttributesImpl;
030import org.apache.cocoon.xml.XMLUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.cms.contenttype.ContentAttributeDefinition;
034import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
035import org.ametys.core.util.IgnoreRootHandler;
036import org.ametys.odf.catalog.Catalog;
037import org.ametys.odf.catalog.CatalogsManager;
038import org.ametys.odf.enumeration.OdfReferenceTableHelper;
039import org.ametys.odf.program.Program;
040import org.ametys.odf.program.ProgramFactory;
041import org.ametys.plugins.repository.AmetysObjectIterable;
042import org.ametys.plugins.repository.AmetysRepositoryException;
043import org.ametys.plugins.repository.UnknownAmetysObjectException;
044import org.ametys.runtime.model.Model;
045import org.ametys.runtime.model.View;
046
047
048/**
049 * Generator producing the SAX events for the catalogue summary 
050 */ 
051public class FOProgramsGenerator extends ServiceableGenerator
052{
053    /** The content type extension point */
054    protected ContentTypeExtensionPoint _ctypeEP;
055    /** The ODf enumeration helper */
056    protected OdfReferenceTableHelper _odfTableRefHelper;
057    /** The catalog manager */
058    protected CatalogsManager _catalogManager;
059    
060    @Override
061    public void service(ServiceManager sManager) throws ServiceException
062    {
063        super.service(sManager);
064        _ctypeEP = (ContentTypeExtensionPoint) sManager.lookup(ContentTypeExtensionPoint.ROLE);
065        _odfTableRefHelper = (OdfReferenceTableHelper) sManager.lookup(OdfReferenceTableHelper.ROLE);
066        _catalogManager = (CatalogsManager) sManager.lookup(CatalogsManager.ROLE);
067    }
068    
069    public void generate() throws IOException, SAXException, ProcessingException
070    {
071        contentHandler.startDocument();
072        XMLUtils.startElement(contentHandler, "programs");
073        
074        Catalog catalog = null;
075        if ("_default".equals(source) && _catalogManager.getCatalogs().size() > 0)
076        {
077            catalog = _catalogManager.getCatalogs().get(0);
078        }
079        else
080        {
081            catalog = _catalogManager.getCatalog(source);
082        }
083        
084        if (catalog == null)
085        {
086            throw new IllegalArgumentException ("Failed to generated PDF of unknown catalog '" + source + "'");
087        }
088        
089        AttributesImpl attrs = new AttributesImpl();
090        attrs.addCDATAAttribute("name", source);
091        XMLUtils.createElement(contentHandler, "catalog", attrs, catalog.getTitle());
092        
093        Map<String, ContentAttributeDefinition> tableRefAttributeDefs = _odfTableRefHelper.getTableRefAttributeDefinitions(ProgramFactory.PROGRAM_CONTENT_TYPE);
094        
095        try
096        {
097            _saxPrograms(catalog.getName(), parameters.getParameter("lang"), tableRefAttributeDefs);
098        }
099        catch (ParameterException e)
100        {
101            throw new IllegalArgumentException ("Missing lang parameter", e);
102        }
103        
104        // SAX entries of table references
105        XMLUtils.startElement(contentHandler, "enumerated-metadata");
106        for (ContentAttributeDefinition attributeDef : tableRefAttributeDefs.values())
107        {
108            _odfTableRefHelper.saxItems(contentHandler, attributeDef);
109        }
110        XMLUtils.endElement(contentHandler, "enumerated-metadata");
111        
112        XMLUtils.endElement(contentHandler, "programs");
113        contentHandler.endDocument();
114    }
115    
116    /**
117     * SAX programs
118     * @param catalog the catalog's name
119     * @param lang the programs' language
120     * @param tableRefAttributeDefs The table reference attribute definitions
121     * @throws MalformedURLException if an error occurred
122     * @throws IOException if an error occurred
123     * @throws SAXException if an error occurred
124     */
125    protected void _saxPrograms(String catalog, String lang, Map<String, ContentAttributeDefinition> tableRefAttributeDefs) throws MalformedURLException, IOException, SAXException
126    {
127        AmetysObjectIterable<Program> programs = _catalogManager.getPrograms(catalog, lang);
128        
129        for (Program program : programs)
130        {
131            AttributesImpl attrs = new AttributesImpl();
132            attrs.addCDATAAttribute("id", program.getId());
133            attrs.addCDATAAttribute("name", program.getName());
134            attrs.addCDATAAttribute("title", program.getTitle());
135            
136            XMLUtils.startElement(contentHandler, "program", attrs);
137            
138            _saxTableRefAttributeValues (program, tableRefAttributeDefs);
139            
140            XMLUtils.startElement(contentHandler, "fo");
141            SitemapSource src = null;      
142            
143            try
144            {
145                String uri = "cocoon://_plugins/odf/_content/" + program.getName() + ".fo";
146                src = (SitemapSource) resolver.resolveURI(uri);
147                src.toSAX(new IgnoreRootHandler(contentHandler));
148            }
149            catch (UnknownAmetysObjectException e)
150            {
151                // The content may be archived
152            }
153            finally
154            {
155                resolver.release(src);
156            }
157            
158            XMLUtils.endElement(contentHandler, "fo");
159            XMLUtils.endElement(contentHandler, "program");
160        }
161    }
162    
163    /**
164     * SAX enumerated values of an attribute 
165     * @param program The program
166     * @param tableRefAttributeDefs The table reference attribute definitions
167     * @throws AmetysRepositoryException if an error occurred
168     * @throws SAXException if an error occurred
169     * @throws IOException if an error occurred
170     */
171    protected void _saxTableRefAttributeValues(Program program, Map<String, ContentAttributeDefinition> tableRefAttributeDefs) throws AmetysRepositoryException, SAXException, IOException
172    {
173        // Build a view containing all the reference tables attributes
174        @SuppressWarnings("unchecked")
175        View view = View.of((Collection<Model>) program.getModel(), tableRefAttributeDefs.keySet().toArray(new String[tableRefAttributeDefs.size()]));
176        
177        // Generate SAX events for the built view
178        XMLUtils.startElement(contentHandler, "metadata");
179        program.dataToSAX(contentHandler, view);
180        XMLUtils.endElement(contentHandler, "metadata");
181    }
182}