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.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.context.Context;
026import org.apache.avalon.framework.context.ContextException;
027import org.apache.avalon.framework.context.Contextualizable;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030
031import org.ametys.core.schedule.AmetysJob;
032import org.ametys.runtime.workspace.WorkspaceMatcher;
033
034/**
035 * This implementation creates an element for adding a new task
036 */
037public class AddTaskClientSideElement extends StaticClientSideElement implements Contextualizable
038{
039    /** The Avalon context */
040    protected Context _context;
041    
042    @Override
043    public void contextualize(Context context) throws ContextException
044    {
045        _context = context;
046    }
047
048    @Override
049    protected String _configureClass(Configuration configuration) throws ConfigurationException
050    {
051        return "Ametys.plugins.coreui.schedule.AddTaskButtonController";
052    }
053    
054    @SuppressWarnings("unchecked")
055    @Override
056    protected Map<String, Object> configureInitialParameters(Configuration configuration) throws ConfigurationException
057    {
058        Map<String, Object> initialParameters = super.configureInitialParameters(configuration);
059        
060        initialParameters.put("action", _configureClass(configuration) + ".act");
061        
062        // Always have at least the log category 'org.ametys.plugins.core.schedule.Scheduler', 
063        // because in case of error in AmetysJob during the execution of the Schedulable, it will log in that category
064        List<String> logCategories = new ArrayList<>();
065        String jobLogCategory = AmetysJob.class.getName() + "$" + initialParameters.get("schedulable");
066        logCategories.add(jobLogCategory);
067        if (initialParameters.containsKey("log-category"))
068        {
069            Object initialLogCategory = initialParameters.get("log-category");
070            
071            if (initialLogCategory instanceof String)
072            {
073                logCategories.add((String) initialLogCategory);
074            }
075            else if (initialLogCategory instanceof List)
076            {
077                logCategories.addAll((List<String>) initialLogCategory);
078            }
079        }
080        initialParameters.put("log-category", logCategories);
081        
082        return initialParameters;
083    }
084    
085    @Override
086    protected Script _configureScript(Configuration configuration) throws ConfigurationException
087    {
088        List<ScriptFile> scriptsImports = _configureImports(configuration.getChild("scripts"));
089        scriptsImports.add(new ScriptFile("/plugins/core-ui/resources/js/Ametys/plugins/coreui/schedule/AddTaskButtonController.js"));
090        List<ScriptFile> cssImports = _configureImports(configuration.getChild("css"));
091        String jsClassName = _configureClass(configuration.getChild("class"));
092        Map<String, Object> initialParameters = configureInitialParameters(configuration);
093        
094        return new Script(this.getId(), jsClassName, scriptsImports, cssImports, initialParameters);
095    }
096    
097    @Override
098    protected Map<String, List<String>> _configureDependencies(Configuration configuration) throws ConfigurationException
099    {
100        Map<String, List<String>> dependencies = super._configureDependencies(configuration);
101        
102        List<String> importDependencies = dependencies.computeIfAbsent("org.ametys.core.ui.StaticFileImportsManager", __ -> new ArrayList<>());
103        importDependencies.add("org.ametys.core.schedule.Scheduler");
104
105        List<String> uitoolsDependencies = dependencies.computeIfAbsent("org.ametys.core.ui.UIToolsFactoriesManager", __ -> new ArrayList<>());
106        uitoolsDependencies.add("uitool-server-logs");
107        
108        return dependencies;
109    }
110    
111    @Override
112    public Map<String, List<String>> getDependencies()
113    {
114        // RUNTIME-2913 The task scheduler is opened in the CMS while using the AddTaskButtonController
115        // Load the task scheduler only on admin context
116        Request request = ContextHelper.getRequest(_context);
117        String workspaceName = (String) request.getAttribute(WorkspaceMatcher.WORKSPACE_NAME);
118        if ("admin".equals(workspaceName))
119        {
120            Map<String, List<String>> clonedDependencies = new HashMap<>(_dependencies);
121            List<String> uitoolsDependencies = new ArrayList<>();
122            if (clonedDependencies.containsKey("org.ametys.core.ui.UIToolsFactoriesManager"))
123            {
124                uitoolsDependencies.addAll(clonedDependencies.get("org.ametys.core.ui.UIToolsFactoriesManager"));
125            }
126            uitoolsDependencies.add("uitool-scheduled-tasks");
127            clonedDependencies.put("org.ametys.core.ui.UIToolsFactoriesManager", uitoolsDependencies);
128            return clonedDependencies;
129        }
130        return _dependencies;
131    }
132}