001/*
002 *  Copyright 2016 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.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024
025import org.ametys.core.ui.ClientSideElement.Script;
026import org.ametys.core.ui.ClientSideElement.ScriptFile;
027
028/**
029 * Helper for manipulating ClientSideElement
030 */
031public final class ClientSideElementHelper extends AbstractLogEnabled
032{
033    /**
034     * Clone a ClientSideElement Script
035     * @param script The script
036     * @return The new script
037     */
038    public static Script cloneScript(Script script)
039    {
040        return cloneScript(script, script.getId());
041    }
042    
043    /**
044     * Clone a ClientSideElement Script
045     * @param script The script
046     * @param id The new script id
047     * @return The new script
048     */
049    public static Script cloneScript(Script script, String id)
050    {
051        List<ScriptFile> scriptFiles = cloneScriptFile(script.getScriptFiles());
052        List<ScriptFile> cssFiles = cloneScriptFile(script.getCSSFiles());
053        Map<String, Object> parameters = new HashMap<>(script.getParameters());
054        
055        Script clone = new Script(id, script.getScriptClassname(), scriptFiles, cssFiles, parameters);
056        return clone;
057    }
058    
059    private static List<ScriptFile> cloneScriptFile(List<ScriptFile> scriptFiles)
060    {
061        List<ScriptFile> clonedList = new ArrayList<>();
062        
063        for (ScriptFile scriptFile : scriptFiles)
064        {
065            if (scriptFile.isLangSpecific())
066            {
067                clonedList.add(new ScriptFile(scriptFile.getLangPaths(), scriptFile.getDefaultLang()));
068            }
069            else
070            {
071                clonedList.add(new ScriptFile(scriptFile.getRtlMode(), scriptFile.getPath()));
072            }
073        }
074        
075        return clonedList;
076    }
077}