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 = _odfHelper.getParentPrograms(course);
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            try
113            {
114                _odfHelper.switchToLiveVersionIfNeeded(cl);
115                
116                saxCourseList(cl, course.getContextPath(), defaultLocale);
117            }
118            catch (NoLiveVersionException e) 
119            {
120                // Just ignore the course list
121            }
122        }
123    }
124
125    /**
126     * SAX the referenced {@link CoursePart}s
127     * @param course The course
128     * @param defaultLocale The default locale
129     * @throws SAXException if an error occurs while saxing
130     * @throws IOException if an error occurs
131     * @throws ProcessingException if an error occurs
132     */
133    protected void saxCourseParts(Course course, Locale defaultLocale) throws SAXException, ProcessingException, IOException
134    {
135        for (CoursePart coursePart : course.getCourseParts())
136        {
137            try
138            {
139                _odfHelper.switchToLiveVersionIfNeeded(coursePart);
140                
141                saxCoursePart(coursePart, defaultLocale);
142            }
143            catch (NoLiveVersionException e) 
144            {
145                // Just ignore the course part
146            }
147        }
148    }
149}