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