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