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