001/*
002 *  Copyright 2022 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.odf.init;
017
018import java.io.IOException;
019import java.net.URI;
020import java.nio.file.FileSystems;
021import java.nio.file.Files;
022import java.nio.file.Path;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.avalon.framework.configuration.Configurable;
028import org.apache.avalon.framework.configuration.Configuration;
029import org.apache.avalon.framework.configuration.ConfigurationException;
030import org.apache.avalon.framework.service.ServiceException;
031import org.apache.avalon.framework.service.ServiceManager;
032import org.apache.avalon.framework.service.Serviceable;
033import org.apache.excalibur.source.SourceResolver;
034import org.apache.excalibur.source.TraversableSource;
035
036import org.ametys.runtime.plugin.component.AbstractLogEnabled;
037import org.ametys.runtime.plugin.component.PluginAware;
038
039/**
040 * ODF reference table data description to import on initialization.
041 * This implementation only works outside a JAR file.
042 */
043public class OdfRefTableDataAsFolder extends AbstractLogEnabled implements OdfRefTableData, PluginAware, Serviceable, Configurable
044{
045    /** The source resolver component */
046    protected SourceResolver _srcResolver;
047    
048    /** The plugin name */
049    protected String _pluginName;
050    /** The extension id */
051    protected String _id;
052
053    /** List of data files to import */
054    protected String _dataFolder;
055    /** The priority */
056    protected Integer _priority;
057    
058    public void setPluginInfo(String pluginName, String featureName, String id)
059    {
060        _pluginName = pluginName;
061        _id = id;
062    }
063    
064    public void service(ServiceManager manager) throws ServiceException
065    {
066        _srcResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
067    }
068    
069    public void configure(Configuration configuration) throws ConfigurationException
070    {
071        _priority = configuration.getChild("priority").getValueAsInteger(50);
072        _dataFolder = configuration.getChild("files").getAttribute("folder", "plugin:" + _pluginName + "://ref-data");
073    }
074    
075    public int getPriority()
076    {
077        return _priority;
078    }
079    
080    public Map<String, String> getDataToImport()
081    {
082        Map<String, String> dataToImport = new HashMap<>();
083        
084        for (String dataFile : getDataFiles())
085        {
086            String contentType = "odf-enumeration." + dataFile.substring(dataFile.lastIndexOf(FileSystems.getDefault().getSeparator()) + 1, dataFile.lastIndexOf("."));
087            dataToImport.put(contentType, dataFile);
088        }
089        
090        if (getLogger().isInfoEnabled())
091        {
092            getLogger().info("Data files found in extension '{}': {}", _id, dataToImport.toString());
093        }
094        
095        return dataToImport;
096    }
097    
098    /**
099     * Get the list of {@link Path} including data files to import.
100     * @return The list of files to import
101     */
102    protected List<String> getDataFiles()
103    {
104        TraversableSource refDataSource = null;
105        try
106        {
107            refDataSource = (TraversableSource) _srcResolver.resolveURI(_dataFolder);
108            if (refDataSource.exists() && refDataSource.isCollection())
109            {
110                Path refDataPath = Path.of(URI.create(refDataSource.getURI()));
111                return Files.list(refDataPath)
112                        .map(Path::toString)
113                        .toList();
114            }
115        }
116        catch (IOException e)
117        {
118            getLogger().error("Problem while getting ODF reference table data to import for extension {}.", _id, e);
119        }
120        finally
121        {
122            if (refDataSource != null)
123            {
124                _srcResolver.release(refDataSource);
125            }
126        }
127        
128        return List.of();
129    }
130}