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 if (_odfHelper.useLegacyProgramStructure(course)) 057 { 058 // Child courses lists 059 saxCourseLists(course, defaultLocale); 060 061 // Course parts 062 saxCourseParts(course, defaultLocale); 063 } 064 065 // Contacts 066 saxPersons(course); 067 068 // OrgUnits 069 saxOrgUnits(course); 070 071 // Translations 072 saxTranslation(course); 073 074 // Referenced programs 075 saxReferencedPrograms(course); 076 } 077 } 078 079 Request request = ObjectModelHelper.getRequest(objectModel); 080 request.setAttribute(Content.class.getName(), content); 081 } 082 083 /** 084 * SAX the referenced programs 085 * @param course The course 086 * @throws SAXException if an error occurs while saxing 087 */ 088 protected void saxReferencedPrograms (Course course) throws SAXException 089 { 090 boolean showReferences = Config.getInstance().getValue("odf.course.showReferences"); 091 if (showReferences) 092 { 093 Set<Program> referencingPrograms = _odfHelper.getParentPrograms(course); 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 try 116 { 117 _odfHelper.switchToLiveVersionIfNeeded(cl); 118 119 saxCourseList(cl, course.getContextPath(), defaultLocale); 120 } 121 catch (NoLiveVersionException e) 122 { 123 // Just ignore the course list 124 } 125 } 126 } 127 128 /** 129 * SAX the referenced {@link CoursePart}s 130 * @param course The course 131 * @param defaultLocale The default locale 132 * @throws SAXException if an error occurs while saxing 133 * @throws IOException if an error occurs 134 * @throws ProcessingException if an error occurs 135 */ 136 protected void saxCourseParts(Course course, Locale defaultLocale) throws SAXException, ProcessingException, IOException 137 { 138 for (CoursePart coursePart : course.getCourseParts()) 139 { 140 try 141 { 142 _odfHelper.switchToLiveVersionIfNeeded(coursePart); 143 144 saxCoursePart(coursePart, defaultLocale); 145 } 146 catch (NoLiveVersionException e) 147 { 148 // Just ignore the course part 149 } 150 } 151 } 152}