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.web.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024
025import org.ametys.core.ui.SelectionAwareAddTaskClientSideElement;
026
027/**
028 * This implementation creates an site-selection aware element for adding a new task which need a site parameter (called 'siteName')
029 */
030public class SiteSelectionAwareAddTaskClientSideElement extends SelectionAwareAddTaskClientSideElement
031{
032    @Override
033    protected String _configureClass(Configuration configuration) throws ConfigurationException
034    {
035        return "Ametys.plugins.web.site.SiteSelectionAwareAddTaskButtonController";
036    }
037    
038    @Override
039    protected Map<String, Object> configureInitialParameters(Configuration configuration) throws ConfigurationException
040    {
041        Map<String, Object> initialParameters = super.configureInitialParameters(configuration);
042        initialParameters.put("schedulable-param-name", "siteName");
043        initialParameters.put("selection-target-id", "^site$");
044        return initialParameters;
045    }
046    
047    @Override
048    protected Script _configureScript(Configuration configuration) throws ConfigurationException
049    {
050        Script script = super._configureScript(configuration);
051        script.getScriptFiles().add(new ScriptFile("/plugins/web/resources/js/Ametys/plugins/web/site/SiteSelectionAwareAddTaskButtonController.js"));
052        return script;
053    }
054    
055    @Override
056    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
057    {
058        
059        if (contextParameters.containsKey("siteName"))
060        {
061            List<Script> clonedScripts = new ArrayList<>();
062            
063            List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
064            for (Script script : scripts)
065            {
066                Script clonedScript = new Script(script);
067                clonedScript.getParameters().remove("selection-target-id");
068                
069                clonedScripts.add(clonedScript);
070            }
071            
072            return clonedScripts;
073        }
074        else
075        {
076            return super.getScripts(ignoreRights, contextParameters);
077        }
078    }
079}