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.plugins.odfweb.xslt;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.commons.lang.StringUtils;
021
022import org.ametys.cms.repository.Content;
023import org.ametys.odf.program.AbstractProgram;
024import org.ametys.odf.program.Program;
025import org.ametys.odf.program.SubProgram;
026import org.ametys.plugins.odfweb.repository.OdfPageHandler;
027import org.ametys.plugins.odfweb.repository.ProgramPage;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.sitemap.Sitemap;
031import org.ametys.web.transformation.xslt.AmetysXSLTHelper;
032
033/**
034 * Helper component to be used from XSL stylesheets.
035 */
036public class OdfXSLTHelper extends org.ametys.odf.OdfXSLTHelper
037{
038    private static OdfPageHandler _odfPageHandler;
039    
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _odfPageHandler = (OdfPageHandler) smanager.lookup(OdfPageHandler.ROLE);
045    }
046    
047    /**
048     * Get the ODF root page, for a specific site, language.
049     * If there is many ODF root pages, the first page of the list is returned.
050     * @param siteName the desired site name.
051     * @param language the sitemap language to search in.
052     * @return the first ODF root page, or null if not found
053     */
054    public static String odfRootPage(String siteName, String language)
055    {
056        Page odfRootPage = _odfPageHandler.getOdfRootPage(siteName, language);
057        if (odfRootPage != null)
058        {
059            return odfRootPage.getId();
060        }
061        return null;
062    }
063    
064    /**
065     * Get the ODF root page, for a specific site, language and catalog.
066     * @param siteName the desired site name.
067     * @param language the sitemap language to search in.
068     * @param catalog The ODF catalog
069     * @return the ODF root page, or null if not found
070     */
071    public static String odfRootPage(String siteName, String language, String catalog)
072    {
073        Page odfRootPage = _odfPageHandler.getOdfRootPage(siteName, language, catalog);
074        if (odfRootPage != null)
075        {
076            return odfRootPage.getId();
077        }
078        return null;
079    }
080    
081    /**
082     * Get the PDF url of a program or a subprogram
083     * @param contentId The content id
084     * @param siteName The site name
085     * @return the PDF url or empty string if the content is not a {@link Program} or {@link SubProgram}
086     */
087    public static String odfPDFUrl (String contentId, String siteName)
088    {
089        StringBuilder sb = new StringBuilder();
090        
091        Content content = _ametysObjectResolver.resolveById(contentId);
092        if (content instanceof AbstractProgram)
093        {
094            sb.append(AmetysXSLTHelper.uriPrefix())
095                .append("/plugins/odf-web/")
096                .append(siteName)
097                .append("/_content/")
098                .append(content.getName())
099                .append(".pdf");
100        }
101        
102        return sb.toString();
103    }
104
105    /**
106     * Get the id of parent program from the current page
107     * @return the id of parent program or null if not found
108     */
109    public static String parentProgramId()
110    {
111        String pageId = AmetysXSLTHelper.pageId();
112        
113        if (StringUtils.isNotEmpty(pageId))
114        {
115            Page page = _ametysObjectResolver.resolveById(pageId);
116            
117            AmetysObject parent = page.getParent();
118            while (!(parent instanceof Sitemap))
119            {
120                if (parent instanceof ProgramPage)
121                {
122                    return ((ProgramPage) parent).getProgram().getId();
123                }
124                
125                parent = parent.getParent();
126            }
127        }
128        
129        return null;
130    }
131}