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/resources/js/Ametys/plugins/core/schedule/Scheduler.js"));
090        scriptsImports.add(new ScriptFile("/plugins/core-ui/resources/js/Ametys/plugins/coreui/schedule/AddTaskButtonController.js"));
091        List<ScriptFile> cssImports = _configureImports(configuration.getChild("css"));
092        String jsClassName = _configureClass(configuration.getChild("class"));
093        Map<String, Object> initialParameters = configureInitialParameters(configuration);
094        
095        return new Script(this.getId(), jsClassName, scriptsImports, cssImports, initialParameters);
096    }
097    
098    @Override
099    protected Map<String, List<String>> _configureDependencies(Configuration configuration) throws ConfigurationException
100    {
101        Map<String, List<String>> dependencies = super._configureDependencies(configuration);
102        
103        List<String> importDependencies = dependencies.get("org.ametys.core.ui.StaticFileImportsManager");
104        if (importDependencies == null)
105        {
106            importDependencies = new ArrayList<>();
107        }
108        importDependencies.add("org.ametys.core.schedule.Scheduler");
109        
110        List<String> uitoolsDependencies = dependencies.get("org.ametys.core.ui.UIToolsFactoriesManager");
111        if (uitoolsDependencies == null)
112        {
113            uitoolsDependencies = new ArrayList<>();
114        }
115        uitoolsDependencies.add("uitool-server-logs");
116        
117        return dependencies;
118    }
119    
120    @Override
121    public Map<String, List<String>> getDependencies()
122    {
123        // RUNTIME-2913 The task scheduler is opened in the CMS while using the AddTaskButtonController
124        // Load the task scheduler only on admin context
125        Request request = ContextHelper.getRequest(_context);
126        String workspaceName = (String) request.getAttribute(WorkspaceMatcher.WORKSPACE_NAME);
127        if ("admin".equals(workspaceName))
128        {
129            Map<String, List<String>> clonedDependencies = new HashMap<>(_dependencies);
130            List<String> uitoolsDependencies = new ArrayList<>();
131            if (clonedDependencies.containsKey("org.ametys.core.ui.UIToolsFactoriesManager"))
132            {
133                uitoolsDependencies.addAll(clonedDependencies.get("org.ametys.core.ui.UIToolsFactoriesManager"));
134            }
135            uitoolsDependencies.add("uitool-scheduled-tasks");
136            clonedDependencies.put("org.ametys.core.ui.UIToolsFactoriesManager", uitoolsDependencies);
137            return clonedDependencies;
138        }
139        return _dependencies;
140    }
141}