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.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.schedule.AmetysJob;
026
027/**
028 * This implementation creates an element for adding a new task
029 */
030public class AddTaskClientSideElement extends StaticClientSideElement
031{
032    @Override
033    protected String _configureClass(Configuration configuration) throws ConfigurationException
034    {
035        return "Ametys.plugins.coreui.schedule.AddTaskButtonController";
036    }
037    
038    @SuppressWarnings("unchecked")
039    @Override
040    protected Map<String, Object> configureInitialParameters(Configuration configuration) throws ConfigurationException
041    {
042        Map<String, Object> initialParameters = super.configureInitialParameters(configuration);
043        
044        initialParameters.put("action", "Ametys.plugins.coreui.schedule.AddTaskButtonController.act");
045        
046        // Always have at least the log category 'org.ametys.plugins.core.schedule.Scheduler', 
047        // because in case of error in AmetysJob during the execution of the Schedulable, it will log in that category
048        List<String> logCategories = new ArrayList<>();
049        String jobLogCategory = AmetysJob.class.getName() + "$" + initialParameters.get("schedulable");
050        logCategories.add(jobLogCategory);
051        if (initialParameters.containsKey("log-category"))
052        {
053            Object initialLogCategory = initialParameters.get("log-category");
054            
055            if (initialLogCategory instanceof String)
056            {
057                logCategories.add((String) initialLogCategory);
058            }
059            else if (initialLogCategory instanceof List)
060            {
061                logCategories.addAll((List<String>) initialLogCategory);
062            }
063        }
064        initialParameters.put("log-category", logCategories);
065        
066        return initialParameters;
067    }
068    
069    @Override
070    protected Script _configureScript(Configuration configuration) throws ConfigurationException
071    {
072        List<ScriptFile> scriptsImports = _configureImports(configuration.getChild("scripts"));
073        scriptsImports.add(new ScriptFile("all", "all", "/plugins/core/resources/js/Ametys/plugins/core/schedule/Scheduler.js"));
074        scriptsImports.add(new ScriptFile("all", "all", "/plugins/core-ui/resources/js/Ametys/plugins/coreui/schedule/AddTaskButtonController.js"));
075        List<ScriptFile> cssImports = _configureImports(configuration.getChild("css"));
076        String jsClassName = _configureClass(configuration.getChild("class"));
077        Map<String, Object> initialParameters = configureInitialParameters(configuration);
078        
079        return new Script(this.getId(), jsClassName, scriptsImports, cssImports, initialParameters);
080    }
081    
082    @Override
083    protected Map<String, List<String>> _configureDependencies(Configuration configuration) throws ConfigurationException
084    {
085        Map<String, List<String>> dependencies = super._configureDependencies(configuration);
086        
087        List<String> importDependencies = dependencies.get("org.ametys.core.ui.StaticFileImportsManager");
088        if (importDependencies == null)
089        {
090            importDependencies = new ArrayList<>();
091        }
092        importDependencies.add("org.ametys.core.schedule.Scheduler");
093        
094        List<String> uitoolsDependencies = dependencies.get("org.ametys.core.ui.UIToolsFactoriesManager");
095        if (uitoolsDependencies == null)
096        {
097            uitoolsDependencies = new ArrayList<>();
098        }
099        uitoolsDependencies.add("uitool-scheduled-tasks");
100        uitoolsDependencies.add("uitool-server-logs");
101        
102        return dependencies;
103    }
104}