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.core.ui.help;
017
018import java.util.HashMap;
019import java.util.HashSet;
020import java.util.Map;
021import java.util.Map.Entry;
022import java.util.Set;
023
024import org.apache.avalon.framework.activity.Initializable;
025import org.apache.avalon.framework.component.Component;
026import org.apache.avalon.framework.configuration.Configuration;
027import org.apache.avalon.framework.configuration.ConfigurationException;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031import org.apache.avalon.framework.thread.ThreadSafe;
032
033import org.ametys.runtime.plugin.ExtensionPoint;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035
036
037/**
038 * This extension point handle a list of help URLs handled by the plugins or the application.
039 */
040public class HelpExtensionPoint extends AbstractLogEnabled implements ExtensionPoint<String>, Initializable, ThreadSafe, Component, Serviceable
041{
042    /** The avalon role */
043    public static final String ROLE = HelpExtensionPoint.class.getName();
044    
045    /** The map of rightId, Right of declared rights */
046    protected Map<String, HelpLink> _links;
047    /** Set of extensions points added */
048    protected Set<String> _points;
049    /** The help manager */
050    protected HelpManager _helpManager;
051    
052    public void initialize() throws Exception
053    {
054        _links = new HashMap<>();
055        _points = new HashSet<>();
056    }
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        _helpManager = (HelpManager) manager.lookup(HelpManager.ROLE);
060    }
061    
062    public boolean hasExtension(String id)
063    {
064        return _points.contains(id);
065    }
066
067    public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException
068    {
069        _points.add(id);
070        getLogger().debug("Adding help URLs from plugin {}/{}", pluginName,  featureName);
071        
072        Map<String, HelpLink> links = _helpManager.parseConfigFile(configuration, pluginName, featureName);
073        
074        for (Entry<String, HelpLink> link : links.entrySet())
075        {
076            if (_links.containsKey(link.getKey()))
077            {
078                getLogger().warn("There is already a help key for this point{}id : {}", HelpLink.SEPARATOR, link.getKey());
079            }
080            else
081            {
082                _links.put(link.getKey(), link.getValue());
083            }
084        }
085    }
086    
087    /**
088     * Return the helpLink available for this point/id
089     * @param point extension point
090     * @param id id of the extension for this extension point
091     * @return a {@link HelpLink}
092     */
093    public HelpLink getHelpLink(String point, String id)
094    {
095        String uniqueId = HelpLink.getUniqueId(point, id);
096        return _links.get(uniqueId);
097    }
098
099    public Set<String> getExtensionsIds()
100    {
101        return _points;
102    }
103
104    public void initializeExtensions() throws Exception
105    {
106        // empty
107    }
108    
109    public String getExtension(String id)
110    {
111        if (_points.contains(id))
112        {
113            return id;
114        }
115        else
116        {
117            return null;
118        }
119    }
120}