001/*
002 *  Copyright 2018 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.HashMap;
019import java.util.Map;
020import java.util.Optional;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.configuration.DefaultConfiguration;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.quartz.JobDataMap;
028import org.quartz.JobExecutionContext;
029
030import org.ametys.core.schedule.Schedulable;
031import org.ametys.core.user.CurrentUserProvider;
032import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
033import org.ametys.plugins.core.schedule.Scheduler;
034import org.ametys.plugins.odfpilotage.report.PilotageReport;
035import org.ametys.plugins.odfpilotage.report.PilotageReport.PilotageReportTarget;
036import org.ametys.plugins.odfpilotage.report.ReportExtensionPoint;
037
038/**
039 * {@link Schedulable} for pilotage report.
040 */
041public abstract class AbstractReportSchedulable extends AbstractStaticSchedulable
042{
043    /** The key for the extension ID */
044    public static final String JOBDATAMAP_EXTENSION_ID_KEY = "extensionId";
045    
046    /** The key for the output format */
047    public static final String JOBDATAMAP_OUTPUT_FORMAT_KEY = "outputFormat";
048    
049    /** The report extension point */
050    protected ReportExtensionPoint _reportEP;
051
052    /** The current user provider */
053    protected CurrentUserProvider _currentUserProvider;
054    
055    @Override
056    public void configure(Configuration configuration) throws ConfigurationException
057    {
058        for (Configuration paramConf : configuration.getChild("parameters").getChildren("param"))
059        {
060            if (paramConf.getAttribute("id").equals(AbstractReportSchedulable.JOBDATAMAP_EXTENSION_ID_KEY))
061            {
062                Configuration customEnumConf = Optional.of(paramConf)
063                    .map(c -> c.getChild("enumeration", false))
064                    .map(c -> c.getChild("custom-enumerator", false))
065                    .orElse(null);
066                if (customEnumConf.getChild("schedulable", false) == null)
067                {
068                    DefaultConfiguration schedulableConf = (DefaultConfiguration) customEnumConf.getChild("schedulable");
069                    schedulableConf.setValue(this.getClass().getName());
070                    ((DefaultConfiguration) customEnumConf).addChild(schedulableConf);
071                }
072                break;
073            }
074        }
075        super.configure(configuration);
076    }
077    
078    @Override
079    public void service(ServiceManager manager) throws ServiceException
080    {
081        super.service(manager);
082        _reportEP = (ReportExtensionPoint) manager.lookup(ReportExtensionPoint.ROLE);
083        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
084    }
085    
086    @Override
087    public void execute(JobExecutionContext context) throws Exception
088    {
089        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
090        String extensionId = jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_EXTENSION_ID_KEY);
091        PilotageReport report = _reportEP.getExtension(extensionId);
092
093        report.launch(getTarget(), getReportParameters(jobDataMap), _currentUserProvider.getUser());
094    }
095    
096    /**
097     * The target of the report.
098     * @return The target
099     */
100    public abstract PilotageReportTarget getTarget();
101    
102    /**
103     * The schedulable is written for generic reports or it has additional parameters.
104     * @return <code>true</code> if the schedulable is only for generic reports.
105     */
106    public boolean forGenericReports()
107    {
108        return true;
109    }
110    
111    /**
112     * Get the report parameters from the schedulable parameters
113     * @param jobDataMap The schedulable parameters
114     * @return The report parameters
115     */
116    protected Map<String, String> getReportParameters(JobDataMap jobDataMap)
117    {
118        Map<String, String> reportParameters = new HashMap<>();
119        reportParameters.put(PilotageReport.PARAMETER_OUTPUT_FORMAT, jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_OUTPUT_FORMAT_KEY));
120        return reportParameters;
121    }
122}