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