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