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