001/*
002 *  Copyright 2018 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.odfpilotage.report.impl;
017
018import java.util.List;
019
020import javax.xml.transform.sax.TransformerHandler;
021
022import org.apache.cocoon.xml.AttributesImpl;
023import org.apache.cocoon.xml.XMLUtils;
024import org.apache.commons.lang3.StringUtils;
025import org.xml.sax.SAXException;
026
027import org.ametys.cms.data.ContentDataHelper;
028import org.ametys.odf.ProgramItem;
029import org.ametys.odf.course.Course;
030import org.ametys.odf.program.Program;
031import org.ametys.plugins.odfpilotage.report.impl.mcc.MCCAmetysObjectTree;
032import org.ametys.plugins.repository.AmetysObject;
033import org.ametys.plugins.repository.data.holder.group.ModelAwareRepeater;
034import org.ametys.plugins.repository.data.holder.group.ModelAwareRepeaterEntry;
035
036/**
037 * Class to generate the MCC report.
038 */
039public class MCCReport extends AbstractMCCReport
040{
041    @Override
042    protected String getType()
043    {
044        return "mcc";
045    }
046    
047    @Override
048    protected void saxMCCs(TransformerHandler handler, Course course, MCCAmetysObjectTree tree) throws SAXException
049    {
050        XMLUtils.startElement(handler, "mcc");
051        
052        // Generate SAX events for MCC sessions
053        _saxSession(handler, course, FIRST_SESSION_NAME);
054        _saxSession(handler, course, SECOND_SESSION_NAME);
055        
056        XMLUtils.endElement(handler, "mcc");
057    }
058
059    private void _saxSession(TransformerHandler handler, Course course, String sessionName) throws SAXException
060    {
061        if (course.hasValue(sessionName))
062        {
063            AttributesImpl sessionAttrs = new AttributesImpl();
064            sessionAttrs.addCDATAAttribute("num", StringUtils.removeStart(sessionName, SESSION_NAME_PREFIX));
065            XMLUtils.startElement(handler, "session", sessionAttrs);
066
067            ModelAwareRepeater session = course.getRepeater(sessionName);
068            for (ModelAwareRepeaterEntry sessionEntry : session.getEntries())
069            {
070                _saxSessionEntry(handler, sessionEntry);
071            }
072
073            XMLUtils.endElement(handler, "session");
074        }
075    }
076    
077    /**
078     * Sax a MCC session entry.
079     * @param handler The transformer handler
080     * @param sessionEntry The session entry name
081     * @throws SAXException If an error occurs
082     */
083    private void _saxSessionEntry(TransformerHandler handler, ModelAwareRepeaterEntry sessionEntry) throws SAXException
084    {
085        AttributesImpl entryAttrs = new AttributesImpl();
086        entryAttrs.addCDATAAttribute("name", String.valueOf(sessionEntry.getPosition()));
087        
088        // nature enseignement
089        String natureEns = ContentDataHelper.getContentIdFromContentData(sessionEntry, "natureEnseignement");
090        if (StringUtils.isNotEmpty(natureEns))
091        {
092            entryAttrs.addCDATAAttribute("natureEnseignement", natureEns);
093        }
094        
095        // start entry
096        XMLUtils.startElement(handler, "entry", entryAttrs);
097        
098        saxSessionEntryDetails(handler, sessionEntry);
099        
100        // end entry
101        XMLUtils.endElement(handler, "entry");
102    }
103    
104    @Override
105    protected void populateMCCAmetysObjectTree(MCCAmetysObjectTree tree)
106    {
107        AmetysObject ao = tree.getCurrent();
108        if (ao instanceof ProgramItem)
109        {
110            List<ProgramItem> children = _odfHelper.getChildProgramItems((ProgramItem) ao);
111            for (ProgramItem child : children)
112            {
113                MCCAmetysObjectTree childTree = tree.addChild(child);
114                populateMCCAmetysObjectTree(childTree);
115            }
116        }
117    }
118    
119    @Override
120    protected void saxGlobalInformations(TransformerHandler handler, Program program) throws SAXException
121    {
122        XMLUtils.createElement(handler, "catalog", program.getCatalog());
123    }
124}