001/*
002 *  Copyright 2019 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.plugins.odfpilotage.schedulable;
017
018import java.util.Map;
019import java.util.Optional;
020
021import org.apache.avalon.framework.activity.Initializable;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang.StringUtils;
025
026import org.ametys.core.ui.AddTaskClientSideElement;
027import org.ametys.plugins.odfpilotage.report.ReportExtensionPoint;
028import org.ametys.runtime.i18n.I18nizableText;
029
030/**
031 * This implementation creates an element for adding a new report task
032 */
033public class AddReportTaskClientSideElement extends AddTaskClientSideElement implements Initializable
034{
035    /** The report extension point. */
036    protected ReportExtensionPoint _reportEP;
037    
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        _reportEP = (ReportExtensionPoint) smanager.lookup(ReportExtensionPoint.ROLE);
042        super.service(smanager);
043    }
044
045    @Override
046    public void initialize() throws Exception
047    {
048        initializeSchedulableParameters(_script, _reportEP);
049    }
050    
051    /**
052     * Method to initialize schedulable parameters. Because it's impossible to have a multiple heritage in Java and the 
053     * {@link ODFSelectionAwareAddReportTaskClientSideElement} needs intialization, we added a static method.
054     * @param script The script to update
055     * @param reportEP {@link ReportExtensionPoint} to get some informations on schedulable parameters
056     */
057    public static void initializeSchedulableParameters(Script script, ReportExtensionPoint reportEP)
058    {
059        Map<String, Object> parameters = script.getParameters();
060        
061        String extensionId = Optional.of(parameters)
062            .map(p -> p.get("schedulable-parameters"))
063            .map(Map.class::cast)
064            .map(sp -> sp.get("extensionId"))
065            .map(String.class::cast)
066            .orElse(null);
067        
068        if (StringUtils.isNotEmpty(extensionId) && reportEP.hasExtension(extensionId))
069        {
070            I18nizableText label = reportEP.getExtension(extensionId).getLabel();
071            parameters.put("label", label);
072            parameters.put("get-parameters-title", label);
073            parameters.put("dialog-title", label);
074        }
075    }
076}