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.core.util.LambdaUtils; 034import org.ametys.odf.ProgramItem; 035import org.ametys.odf.course.Course; 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 metadataSetName = request.getParameter("metadataSetName"); 059 060 contentHandler.startDocument(); 061 062 if ("main".equals(metadataSetName) || StringUtils.isEmpty(metadataSetName)) 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 distribution.entrySet() 089 .stream() 090 .forEach(LambdaUtils.wrapConsumer(e -> 091 { 092 Content skillSet = e.getKey(); 093 094 AttributesImpl attrs = new AttributesImpl(); 095 attrs.addCDATAAttribute("id", skillSet.getId()); 096 attrs.addCDATAAttribute("title", skillSet.getTitle()); 097 attrs.addCDATAAttribute("code", skillSet.getValue("code", false, "")); 098 099 XMLUtils.startElement(contentHandler, "skillSet", attrs); 100 101 Map<Content, Map<Content, Content>> skills = e.getValue(); 102 103 skills.entrySet() 104 .stream() 105 .forEach(LambdaUtils.wrapConsumer(s -> 106 { 107 Content skill = s.getKey(); 108 109 attrs.clear(); 110 attrs.addCDATAAttribute("id", skill.getId()); 111 attrs.addCDATAAttribute("title", skill.getTitle()); 112 attrs.addCDATAAttribute("code", skill.getValue("code", false, "")); 113 114 XMLUtils.startElement(contentHandler, "skill", attrs); 115 116 Map<Content, Content> courses = s.getValue(); 117 118 courses.entrySet().stream() 119 .forEach(LambdaUtils.wrapConsumer(p -> 120 { 121 Content course = p.getKey(); 122 123 attrs.clear(); 124 attrs.addCDATAAttribute("id", course.getId()); 125 attrs.addCDATAAttribute("title", course.getTitle()); 126 attrs.addCDATAAttribute("code", ((Course) course).getCode()); 127 128 XMLUtils.startElement(contentHandler, "course", attrs); 129 130 Content acquisitionLevel = p.getValue(); 131 if (acquisitionLevel != null) 132 { 133 attrs.clear(); 134 attrs.addCDATAAttribute("id", acquisitionLevel.getId()); 135 attrs.addCDATAAttribute("title", acquisitionLevel.getTitle()); 136 attrs.addCDATAAttribute("code", acquisitionLevel.getValue("code", false, "")); 137 attrs.addCDATAAttribute("order", String.valueOf(acquisitionLevel.getValue("order", false, 1))); 138 XMLUtils.createElement(contentHandler, "acquisitionLevel", attrs); 139 } 140 XMLUtils.endElement(contentHandler, "course"); 141 142 })); 143 144 XMLUtils.endElement(contentHandler, "skill"); 145 })); 146 147 XMLUtils.endElement(contentHandler, "skillSet"); 148 })); 149 XMLUtils.endElement(contentHandler, "skills"); 150 } 151}