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.course.generators;
017
018import java.util.Set;
019
020import org.apache.cocoon.ProcessingException;
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.SAXException;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.odf.course.Course;
030import org.ametys.odf.courselist.CourseList;
031import org.ametys.odf.program.Program;
032import org.ametys.odf.program.generators.ProgramContentGenerator;
033import org.ametys.runtime.config.Config;
034
035/**
036 * {@link Generator} for rendering raw content data for a {@link Course}.
037 */
038public class CourseContentGenerator extends ProgramContentGenerator
039{
040    @Override
041    protected void _saxOtherData(Content content) throws SAXException, ProcessingException
042    {
043        super._saxOtherData(content);
044        
045        boolean isEditionMetadataSet = parameters.getParameterAsBoolean("isEditionMetadataSet", false);
046        if (!isEditionMetadataSet)
047        {
048            if (content instanceof Course)
049            {
050                Course course = (Course) content;
051                
052                // Child courses lists
053                saxCourseLists(course);
054                
055                // Contacts
056                saxPersons(course);
057                
058                // OrgUnits
059                saxOrgUnits(course);
060                
061                // Translations
062                saxTranslation(course);
063                
064                // Referenced programs 
065                saxReferencedPrograms(course);
066            }
067        }
068        
069        Request request = ObjectModelHelper.getRequest(objectModel);
070        request.setAttribute(Content.class.getName(), content);
071    }
072    
073    /**
074     * SAX the referenced programs
075     * @param course The course
076     * @throws SAXException if an error occurs while saxing
077     */
078    protected void saxReferencedPrograms (Course course) throws SAXException
079    {
080        if (Config.getInstance().getValueAsBoolean("odf.course.showReferences"))
081        {
082            Set<Program> referencingPrograms = course.getRootPrograms();
083            
084            for (Program program : referencingPrograms)
085            {
086                AttributesImpl attrs = new AttributesImpl();
087                attrs.addCDATAAttribute("id", program.getId());
088                XMLUtils.createElement(contentHandler, "refProgram", attrs, program.getTitle());
089            }
090        }
091    }
092    
093    /**
094     * SAX the referenced {@link CourseList}s
095     * @param course The course
096     * @throws SAXException if an error occurs while saxing
097     */
098    protected void saxCourseLists (Course course) throws SAXException
099    {
100        for (CourseList cl: course.getCourseLists())
101        {
102            saxCourseList (cl, course.getContextPath());
103        }
104    }
105}