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.plugins.odfweb.inputdata;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Set;
021
022import org.apache.avalon.framework.activity.Initializable;
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029import org.apache.cocoon.ProcessingException;
030import org.apache.cocoon.components.ContextHelper;
031import org.apache.cocoon.environment.Request;
032import org.apache.cocoon.xml.AttributesImpl;
033import org.apache.cocoon.xml.XMLUtils;
034import org.apache.commons.lang.StringUtils;
035import org.xml.sax.ContentHandler;
036import org.xml.sax.SAXException;
037
038import org.ametys.odf.course.Course;
039import org.ametys.odf.enumeration.OdfReferenceTableHelper;
040import org.ametys.odf.program.Program;
041import org.ametys.plugins.odfweb.repository.CoursePage;
042import org.ametys.plugins.odfweb.repository.OdfPageHandler;
043import org.ametys.plugins.repository.AmetysObjectResolver;
044import org.ametys.runtime.config.Config;
045import org.ametys.runtime.plugin.component.AbstractLogEnabled;
046import org.ametys.web.inputdata.InputData;
047import org.ametys.web.repository.page.Page;
048import org.ametys.web.repository.site.Site;
049import org.ametys.web.repository.site.SiteManager;
050
051
052/**
053 * {@link InputData} for SAXing the list of programs having the current course among their courseLists 
054 */
055public class CourseInputData extends AbstractLogEnabled implements InputData, Contextualizable, Initializable, Serviceable
056{
057    /** Avalon context. */
058    protected Context _context;
059    
060    /** The ametys object resolver. */
061    protected AmetysObjectResolver _ametysResolver;
062    
063    /** CMS Sites manager */ 
064    protected SiteManager _siteManager;
065    
066    /** The root handler */
067    protected OdfPageHandler _odfPageHandler;
068    
069    /** The ODF reference table helper */
070    protected OdfReferenceTableHelper _odfRefTableHelper;
071    
072    /** Configuration boolean to active or not the inputdata */
073    private boolean _isActive;
074
075    @Override
076    public void service(ServiceManager manager) throws ServiceException
077    {
078        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
079        _ametysResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
080        _odfPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE);
081        _odfRefTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE);
082    }
083    
084    @Override
085    public void contextualize(Context context) throws ContextException
086    {
087        _context = context;
088    }
089    
090    @Override
091    public void initialize() throws Exception
092    {
093        _isActive = Config.getInstance().getValue("odf.course.showReferences");
094    }
095    
096    @Override
097    public boolean isCacheable(Site site, Page page)
098    {
099        return true;
100    }
101    
102    @Override
103    public void toSAX(ContentHandler handler) throws SAXException, ProcessingException
104    {
105        if (_isActive)
106        {
107            handler.startDocument();
108            XMLUtils.startElement(handler, "programsSearchedByCourse");
109            
110            Request request = ContextHelper.getRequest(_context);
111            
112            String siteName = (String) request.getAttribute("site");
113            String sitemapLanguage = (String) request.getAttribute("sitemapLanguage");
114
115            Page page = (Page) request.getAttribute(Page.class.getName());
116            if (page instanceof CoursePage)
117            {
118                Course course = ((CoursePage) page).getCourse();
119                
120                Set<Program> referencingPrograms = course.getRootPrograms();
121                if (!referencingPrograms.isEmpty())
122                {
123                    Page rootPage = _odfPageHandler.getOdfRootPage(siteName, sitemapLanguage);
124                    AttributesImpl atts = new AttributesImpl();
125                    if (rootPage != null)
126                    {
127                        atts.addCDATAAttribute("root-page-path", rootPage.getPathInSitemap());
128                    }
129                    XMLUtils.startElement(handler, "programs", atts);
130                    
131                    for (Program program : referencingPrograms)
132                    {
133                        try
134                        {
135                            saxProgram(program, handler);
136                        }
137                        catch (IOException e)
138                        {
139                            throw new ProcessingException("An error occurs SAXing a program : " + program.getId(), e);
140                        }
141                    }
142                    XMLUtils.endElement(handler, "programs");
143                }
144            }
145            
146            XMLUtils.endElement(handler, "programsSearchedByCourse");
147            handler.endDocument();
148        }
149    }
150
151    /**
152     * SAX a program.
153     * @param program The program to SAX.
154     * @param contentHandler the content handler
155     * @throws SAXException If an error occurs while SAXing
156     * @throws IOException If an error occurs while retrieving content.
157     */
158    public void saxProgram(Program program, ContentHandler contentHandler) throws SAXException, IOException
159    {
160        AttributesImpl atts = new AttributesImpl();
161        
162        List<String> orgUnitIds = program.getOrgUnits();
163        String degreeId = program.getDegree();
164        String[] dgesipIds = program.getDGESIPCode();
165        String[] siseids = program.getSiseCode();
166        
167        atts.addCDATAAttribute("name", program.getName());
168        atts.addCDATAAttribute("title", program.getTitle());
169        
170        XMLUtils.startElement(contentHandler, "program", atts);
171
172        for (String domain : program.getDomain())
173        {
174            atts.clear();
175            atts.addCDATAAttribute("id", domain);
176            atts.addCDATAAttribute("code", _odfRefTableHelper.getItemCode(domain));
177            XMLUtils.createElement(contentHandler, "domain", atts);
178        }
179        
180        for (String orgUnitId : orgUnitIds)
181        {
182            atts.clear();
183            atts.addCDATAAttribute("id", orgUnitId);
184            XMLUtils.createElement(contentHandler, "orgUnit", atts);
185        }
186        
187        if (StringUtils.isNotEmpty(degreeId))
188        {
189            atts.clear();
190            atts.addCDATAAttribute("id", degreeId);
191            atts.addCDATAAttribute("code", _odfRefTableHelper.getItemCode(degreeId));
192            XMLUtils.startElement(contentHandler, "degree", atts);
193            XMLUtils.endElement(contentHandler, "degree");
194        }
195        
196        if (dgesipIds != null)
197        {
198            for (String dgesipId : dgesipIds)
199            {
200                atts.clear();
201                atts.addCDATAAttribute("id", dgesipId);
202                atts.addCDATAAttribute("code", _odfRefTableHelper.getItemCode(dgesipId));
203                XMLUtils.startElement(contentHandler, "dgesip", atts);
204                XMLUtils.endElement(contentHandler, "dgesip");
205            }
206        }
207        
208        if (siseids != null)
209        {
210            for (String siseId : siseids)
211            {
212                atts.clear();
213                atts.addCDATAAttribute("code", siseId);
214                atts.addCDATAAttribute("code", _odfRefTableHelper.getItemCode(siseId));
215                XMLUtils.startElement(contentHandler, "sise", atts);
216                XMLUtils.endElement(contentHandler, "sise");
217            }
218        }
219        
220        XMLUtils.endElement(contentHandler, "program");
221    }    
222}