001/*
002 *  Copyright 2012 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.core.ui;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023
024import org.ametys.plugins.core.ui.util.ConfigurationHelper;
025
026/**
027 * This implementation creates an element from a static configuration
028 */
029public class StaticClientSideElement extends StaticFileImportsClientSideElement
030{
031    @Override
032    protected Script _configureScript(Configuration configuration) throws ConfigurationException
033    {
034        List<ScriptFile> scriptsImports = _configureImports(configuration.getChild("scripts"));
035        List<ScriptFile> cssImports = _configureImports(configuration.getChild("css"));
036        String jsClassName = _configureClass(configuration.getChild("class"));
037        Map<String, Object> initialParameters = configureInitialParameters(configuration);
038        
039        return new Script(this.getId(), jsClassName, scriptsImports, cssImports, initialParameters);
040    }
041    
042    /**
043     * Configure the js class name
044     * @param configuration The configuration on action tag
045     * @return The js class name
046     * @throws ConfigurationException If an error occurs
047     */
048    protected String _configureClass(Configuration configuration) throws ConfigurationException
049    {
050        String jsClassName = configuration.getAttribute("name");
051        if (getLogger().isDebugEnabled())
052        {
053            getLogger().debug("Js class configured is '" + jsClassName + "'");
054        }
055        return jsClassName;        
056    }
057    
058    /**
059     * Configure the initial parameters
060     * @param configuration the global configuration
061     * @return The initial parameters read
062     * @throws ConfigurationException The configuration is incorrect
063     */
064    protected Map<String, Object> configureInitialParameters(Configuration configuration) throws ConfigurationException
065    {
066        Map<String, Object> initialParameters = _configureParameters(configuration.getChild("class"));
067        
068        if (getLogger().isDebugEnabled())
069        {
070            getLogger().debug("Configuration of element '" + _id + "' is over");
071        }
072        
073        return initialParameters;
074    }
075    
076    /**
077     * Configure parameters recursively 
078     * @param configuration the parameters configuration
079     * @return parameters in a Map
080     * @throws ConfigurationException The configuration is incorrect
081     */
082    protected Map<String, Object> _configureParameters(Configuration configuration) throws ConfigurationException
083    {
084        return ConfigurationHelper.parsePluginParameters(configuration, getPluginName(), getLogger());
085    }
086}