001/* 002 * Copyright 2015 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; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Optional; 021import java.util.Set; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026import org.apache.commons.lang3.StringUtils; 027import org.w3c.dom.Node; 028import org.w3c.dom.NodeList; 029 030import org.ametys.cms.repository.Content; 031import org.ametys.core.util.dom.AmetysNodeList; 032import org.ametys.odf.enumeration.OdfReferenceTableEntry; 033import org.ametys.odf.enumeration.OdfReferenceTableHelper; 034import org.ametys.odf.program.Program; 035import org.ametys.odf.program.SubProgram; 036import org.ametys.odf.xslt.OdfReferenceTableElement; 037import org.ametys.odf.xslt.ProgramElement; 038import org.ametys.odf.xslt.SubProgramElement; 039import org.ametys.plugins.repository.AmetysObjectResolver; 040import org.ametys.plugins.repository.AmetysRepositoryException; 041import org.ametys.runtime.config.Config; 042 043/** 044 * Helper component to be used from XSL stylesheets. 045 */ 046public class OdfXSLTHelper implements Serviceable 047{ 048 /** The ODF reference helper */ 049 protected static OdfReferenceTableHelper _odfRefTableHelper; 050 /** The Ametys resolver */ 051 protected static AmetysObjectResolver _ametysObjectResolver; 052 053 @Override 054 public void service(ServiceManager smanager) throws ServiceException 055 { 056 _odfRefTableHelper = (OdfReferenceTableHelper) smanager.lookup(OdfReferenceTableHelper.ROLE); 057 _ametysObjectResolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 058 } 059 060 /** 061 * Get the label associated with the degree key 062 * @param cdmValue The code of degree 063 * @return The label of degree or code if not found 064 */ 065 public static String degreeLabel (String cdmValue) 066 { 067 return degreeLabel(cdmValue, Config.getInstance().getValue("odf.programs.lang")); 068 } 069 070 /** 071 * Get the code associated with the given reference table's entry 072 * @param tableRefEntryId The id of entry 073 * @return the code or <code>null</code> if not found 074 */ 075 public static String getCode (String tableRefEntryId) 076 { 077 try 078 { 079 Content content = _ametysObjectResolver.resolveById(tableRefEntryId); 080 return content.getMetadataHolder().getString("code", null); 081 } 082 catch (AmetysRepositoryException e) 083 { 084 return null; 085 } 086 } 087 088 /** 089 * Get the id of reference table's entry 090 * @param tableRefId The id of content type 091 * @param code The code 092 * @return the id or <code>null</code> if not found 093 */ 094 public static String getEntryId (String tableRefId, String code) 095 { 096 OdfReferenceTableEntry entry = _odfRefTableHelper.getItemFromCode(tableRefId, code); 097 if (entry != null) 098 { 099 return entry.getId(); 100 } 101 return null; 102 } 103 104 /** 105 * Get the label associated with the degree key 106 * @param cdmValue The cdm value of degree 107 * @param lang The language 108 * @return The label of degree or empty string if not found 109 */ 110 public static String degreeLabel (String cdmValue, String lang) 111 { 112 return Optional 113 .ofNullable(_odfRefTableHelper.getItemFromCDM(OdfReferenceTableHelper.DEGREE, cdmValue)) 114 .map(degree -> degree.getLabel(lang)) 115 .orElse(StringUtils.EMPTY); 116 } 117 118 /** 119 * Get the whole structure of a subprogram, including the structure of child subprograms 120 * @param subprogramId The id of subprogram 121 * @return Node with the subprogram structure 122 */ 123 public static Node getSubProgramStructure (String subprogramId) 124 { 125 SubProgram subProgram = _ametysObjectResolver.resolveById(subprogramId); 126 return new SubProgramElement(subProgram, _ametysObjectResolver); 127 } 128 129 /** 130 * Get the structure of a subprogram, including the structure of child subprograms until the given depth 131 * @param subprogramId The id of subprogram 132 * @param depth Set a positive number to get structure of child subprograms until given depth. Set a negative number to get the whole structure recursively, including the structure of child subprograms. This parameter concerns only subprograms. 133 * @return Node with the subprogram structure 134 */ 135 public static Node getSubProgramStructure (String subprogramId, int depth) 136 { 137 SubProgram subProgram = _ametysObjectResolver.resolveById(subprogramId); 138 return new SubProgramElement(subProgram, depth, null, _ametysObjectResolver); 139 } 140 141 /** 142 * Get the parent program information 143 * @param subprogramId The id of subprogram 144 * @return a node for each program's information 145 */ 146 public static NodeList getParentProgram (String subprogramId) 147 { 148 return getParentProgramStructure(subprogramId, 0); 149 } 150 151 /** 152 * Get the program information 153 * @param programId The id of program 154 * @return Node with the program's information 155 */ 156 public static Node getProgram (String programId) 157 { 158 return getProgramStructure(programId, 0); 159 } 160 161 /** 162 * Get the structure of a parent programs, including the structure of child subprograms until the given depth. 163 * @param subprogramId The id of subprogram 164 * @param depth Set a positive number to get structure of child subprograms until given depth. Set a negative number to get the whole structure recursively, including the structure of child subprograms. This parameter concerns only subprograms. 165 * @return a node for each program's structure 166 */ 167 public static NodeList getParentProgramStructure (String subprogramId, int depth) 168 { 169 List<ProgramElement> programs = new ArrayList<>(); 170 171 SubProgram subProgram = _ametysObjectResolver.resolveById(subprogramId); 172 173 Set<Program> rootPrograms = subProgram.getRootPrograms(); 174 if (rootPrograms.size() > 0) 175 { 176 programs.add(new ProgramElement(rootPrograms.iterator().next(), depth, null, _ametysObjectResolver)); 177 } 178 179 return new AmetysNodeList(programs); 180 } 181 182 /** 183 * Get the structure of a program until the given depth. 184 * @param programId The id of program 185 * @param depth Set a positive number to get structure of child subprograms until given depth. Set a negative number to get the whole structure recursively, including the structure of child subprograms. This parameter concerns only subprograms. 186 * @return Node with the program structure 187 */ 188 public static Node getProgramStructure (String programId, int depth) 189 { 190 Program program = _ametysObjectResolver.resolveById(programId); 191 return new ProgramElement(program, depth, null, _ametysObjectResolver); 192 } 193 194 /** 195 * Get the items of a reference table 196 * @param tableRefId the id of reference table 197 * @param lang the language to use for labels 198 * @return the items 199 */ 200 public static Node getTableRefItems(String tableRefId, String lang) 201 { 202 return new OdfReferenceTableElement(tableRefId, _odfRefTableHelper, lang); 203 } 204}