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.cms.welcome;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.excalibur.source.SourceResolver;
029
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031
032/**
033 * Helper for the welcome button / tool / page
034 */
035public class WelcomeHelper extends AbstractLogEnabled implements Serviceable, Component
036{
037    /** The Avalon role */
038    public static final String ROLE = WelcomeHelper.class.getName();
039    
040    /** Root path for che welcome folder */
041    private static final String __WELCOME_DIRECTORY_ROOT_PATH = "context://WEB-INF/param/welcome/";
042    
043    /** Source Resolver */
044    protected SourceResolver _resolver;
045    
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
049    }
050    
051    /**
052     * Get the path of XSLT file for the welcome tool or null if not found
053     * @param parameters the sitemap parameters
054     * @return the XSLT file path or null if not found
055     */
056    public String getXsltPath(Parameters parameters)
057    {
058        Map<String, Object> map = new HashMap<>();
059        for (String name : parameters.getNames())
060        {
061            map.put(name, parameters.getParameter(name, ""));
062        }
063        return getXsltPath(map);
064    }
065    
066    /**
067     * Get the path of XSLT file for the welcome tool or null if not found
068     * @param contextualParameters the contextual parameters
069     * @return the XSLT file path or null if not found
070     */
071    public String getXsltPath(Map<String, Object> contextualParameters)
072    {
073        String language = (String) contextualParameters.get("language");
074        
075        String languagePath = __WELCOME_DIRECTORY_ROOT_PATH + "index_" + language + ".xsl";
076
077        try
078        {
079            if (_resolver.resolveURI(languagePath).exists())
080            {
081                return languagePath;
082            }
083            else if (_resolver.resolveURI(__WELCOME_DIRECTORY_ROOT_PATH + "index.xsl").exists())
084            {
085                return __WELCOME_DIRECTORY_ROOT_PATH + "index.xsl";
086            }
087        }
088        catch (IOException e)
089        {
090            getLogger().warn("Unable to retrieve XSLT file for welcome tool", e);
091        }
092        
093        return null;
094    }
095}