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