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.stream.Collectors;
020
021import org.apache.avalon.framework.activity.Initializable;
022import org.apache.avalon.framework.configuration.Configurable;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.core.schedule.SchedulableExtensionPoint;
030import org.ametys.plugins.odfpilotage.report.PilotageReport;
031import org.ametys.plugins.odfpilotage.report.ReportExtensionPoint;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.runtime.model.Enumerator;
034
035/**
036 * Enumerator for {@link ReportExtensionPoint}
037 */
038public class ReportExtensionEnumerator implements Enumerator<String>, Serviceable, Configurable, Initializable
039{
040    /** The report extension point */
041    protected ReportExtensionPoint _reportEP;
042    
043    /** The schedulable extension point */
044    protected SchedulableExtensionPoint _schedulableEP;
045    
046    /** The ID of the schedulable to instanciate. */
047    protected String _schedulableId;
048    
049    /** The schedulable. */
050    protected AbstractReportSchedulable _schedulable;
051    
052    @Override
053    public void configure(Configuration configuration) throws ConfigurationException
054    {
055        _schedulableId = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("schedulable").getValue();
056    }
057    
058    @Override
059    public void service(ServiceManager smanager) throws ServiceException
060    {
061        _reportEP = (ReportExtensionPoint) smanager.lookup(ReportExtensionPoint.ROLE);
062        _schedulableEP = (SchedulableExtensionPoint) smanager.lookup(SchedulableExtensionPoint.ROLE);
063    }
064
065    @Override
066    public void initialize() throws Exception
067    {
068        _schedulable = (AbstractReportSchedulable) _schedulableEP.getExtension(_schedulableId);
069    }
070
071    @Override
072    public Map<String, I18nizableText> getTypedEntries() throws Exception
073    {
074        return _reportEP.getExtensionsIds()
075            .stream()
076            .map(_reportEP::getExtension)
077            .filter(r -> r.supports(_schedulable))
078            .collect(Collectors.toMap(PilotageReport::getId, PilotageReport::getLabel));
079    }
080
081    @Override
082    public I18nizableText getEntry(String value) throws Exception
083    {
084        PilotageReport report = _reportEP.getExtension(value);
085        return report.getLabel();
086    }
087}