001/*
002 *  Copyright 2020 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.content;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.apache.commons.lang.StringUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.cms.repository.Content;
033import org.ametys.odf.ProgramItem;
034import org.ametys.odf.course.Course;
035import org.ametys.odf.enumeration.OdfReferenceTableEntry;
036import org.ametys.odf.skill.ODFSkillsHelper;
037
038/**
039 * SAX the acquired skills hold by child courses of a {@link ProgramItem}
040 *
041 */
042public class ProgramItemSkillsGenerator extends ServiceableGenerator
043{
044    private ODFSkillsHelper _skillsHelper;
045
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _skillsHelper = (ODFSkillsHelper) smanager.lookup(ODFSkillsHelper.ROLE);
051    }
052    
053    public void generate() throws IOException, SAXException, ProcessingException
054    {
055        Request request = ObjectModelHelper.getRequest(objectModel);
056        Content content = (Content) request.getAttribute(Content.class.getName());
057
058        String viewName = request.getParameter("viewName");
059        
060        contentHandler.startDocument();
061        
062        if ("main".equals(viewName) || StringUtils.isEmpty(viewName))
063        {
064            if (content instanceof ProgramItem)
065            {
066                Map<Content, Map<Content, Map<Content, Content>>> skillsDistribution = _skillsHelper.getSkillsDistribution((ProgramItem) content, 2);
067                saxSkillsDistribution(skillsDistribution);
068            }
069            else
070            {
071                getLogger().warn("Cannot get the skills of a non program item '" + content.getId() + "'");
072            }
073        }
074        
075        contentHandler.endDocument();
076    }
077    
078    /**
079     * SAX the skills distribution
080     * @param distribution the skills distribution
081     * @throws IOException if an I/O error occurred
082     * @throws SAXException if an error occurred while saxing
083     */
084    protected void saxSkillsDistribution(Map<Content, Map<Content, Map<Content, Content>>> distribution) throws IOException, SAXException
085    {
086        XMLUtils.startElement(contentHandler, "skills");
087        
088        // Iterate on skill sets
089        for (Map.Entry<Content, Map<Content, Map<Content, Content>>> skillSetEntry : distribution.entrySet())
090        {
091            Content skillSet = skillSetEntry.getKey();
092            
093            AttributesImpl attrs = new AttributesImpl();
094            attrs.addCDATAAttribute("id", skillSet.getId());
095            attrs.addCDATAAttribute("title", skillSet.getTitle());
096            attrs.addCDATAAttribute("code", skillSet.getValue(OdfReferenceTableEntry.CODE, false, ""));
097            
098            XMLUtils.startElement(contentHandler, "skillSet", attrs);
099            
100            // Iterate on skills
101            for (Map.Entry<Content, Map<Content, Content>> skillEntry : skillSetEntry.getValue().entrySet())
102            {
103                Content skill = skillEntry.getKey();
104                
105                attrs.clear();
106                attrs.addCDATAAttribute("id", skill.getId());
107                attrs.addCDATAAttribute("title", skill.getTitle());
108                attrs.addCDATAAttribute("code", skill.getValue(OdfReferenceTableEntry.CODE, false, StringUtils.EMPTY));
109                
110                XMLUtils.startElement(contentHandler, "skill", attrs);
111                
112                // Iterate on courses
113                for (Map.Entry<Content, Content> courseEntry : skillEntry.getValue().entrySet())
114                {
115                    Content course = courseEntry.getKey();
116                    
117                    attrs.clear();
118                    attrs.addCDATAAttribute("id", course.getId());
119                    attrs.addCDATAAttribute("title", course.getTitle());
120                    attrs.addCDATAAttribute("code", ((Course) course).getCode());
121                    
122                    XMLUtils.startElement(contentHandler, "course", attrs);
123                    
124                    // Acquision level can be null
125                    Content acquisitionLevel = courseEntry.getValue();
126                    if (acquisitionLevel != null)
127                    {
128                        attrs.clear();
129                        attrs.addCDATAAttribute("id", acquisitionLevel.getId());
130                        attrs.addCDATAAttribute("title", acquisitionLevel.getTitle());
131                        attrs.addCDATAAttribute("code", acquisitionLevel.getValue(OdfReferenceTableEntry.CODE, false, ""));
132                        attrs.addCDATAAttribute("order", String.valueOf(acquisitionLevel.getValue(OdfReferenceTableEntry.ORDER, false, 1)));
133                        XMLUtils.createElement(contentHandler, "acquisitionLevel", attrs);
134                    }
135                    
136                    XMLUtils.endElement(contentHandler, "course");
137                }
138                
139                XMLUtils.endElement(contentHandler, "skill");
140            }
141            
142            XMLUtils.endElement(contentHandler, "skillSet");
143        }
144        
145        XMLUtils.endElement(contentHandler, "skills");
146    }
147}