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                // Skill sets
074                saxSkillSets(course, defaultLocale);
075            }
076        }
077        
078        Request request = ObjectModelHelper.getRequest(objectModel);
079        request.setAttribute(Content.class.getName(), content);
080    }
081    
082    /**
083     * SAX the referenced programs
084     * @param course The course
085     * @throws SAXException if an error occurs while saxing
086     */
087    protected void saxReferencedPrograms (Course course) throws SAXException
088    {
089        boolean showReferences = Config.getInstance().getValue("odf.course.showReferences");
090        if (showReferences)
091        {
092            Set<Program> referencingPrograms = course.getRootPrograms();
093            
094            for (Program program : referencingPrograms)
095            {
096                AttributesImpl attrs = new AttributesImpl();
097                attrs.addCDATAAttribute("id", program.getId());
098                XMLUtils.createElement(contentHandler, "refProgram", attrs, program.getTitle());
099            }
100        }
101    }
102    
103    /**
104     * SAX the referenced {@link CourseList}s
105     * @param course The course
106     * @param defaultLocale The default locale
107     * @throws SAXException if an error occurs while saxing
108     * @throws IOException if an error occurs
109     * @throws ProcessingException if an error occurs
110     */
111    protected void saxCourseLists (Course course, Locale defaultLocale) throws SAXException, ProcessingException, IOException
112    {
113        for (CourseList cl: course.getCourseLists())
114        {
115            saxCourseList(cl, course.getContextPath(), defaultLocale);
116        }
117    }
118
119    /**
120     * SAX the referenced {@link CoursePart}s
121     * @param course The course
122     * @param defaultLocale The default locale
123     * @throws SAXException if an error occurs while saxing
124     * @throws IOException if an error occurs
125     * @throws ProcessingException if an error occurs
126     */
127    protected void saxCourseParts(Course course, Locale defaultLocale) throws SAXException, ProcessingException, IOException
128    {
129        for (CoursePart coursePart : course.getCourseParts())
130        {
131            saxCoursePart(coursePart, defaultLocale);
132        }
133    }
134}