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 */
016
017package org.ametys.plugins.core.ui.help;
018
019import java.util.Collections;
020import java.util.Map;
021import java.util.Set;
022
023/**
024 * Represent a link used for help (point/id and list of urls by lang)
025 */
026public class HelpLink
027{
028    /** default language for the links if the requested one is not found */
029    public static final String DEFAULT_LANGUAGE = "en";
030    /** Separator to create the unique id in getUniqueId */
031    public static final String SEPARATOR = "#";
032    private String _family;
033    private String _id;
034    private Map<String, String> _urls;
035    
036    /**
037     * Create a HelpLink for an extension point/id and a list of urls
038     * @param family the family for this link (for example an extension point)
039     * @param id the id in the family (must be unique in the family)
040     * @param urls a map with language/url for the help pages
041     */
042    public HelpLink(String family, String id, Map<String, String> urls)
043    {
044        this._family = family;
045        this._id = id;
046        this._urls = urls;
047    }
048    
049    /**
050     * Generate an unique ID to access this link accross a map
051     * @param family the family for this link (for example an extension point)
052     * @param id the id in the family (must be unique in the family)
053     * @return an Unique ID
054     */
055    public static String getUniqueId(String family, String id)
056    {
057        return family + SEPARATOR + id;
058    }
059    
060    /**
061     * Get an unique ID across the application
062     * @return an unique ID
063     */
064    public String getUniqueId()
065    {
066        return getUniqueId(this._family, this._id);
067    }
068    
069    /**
070     * Get all urls in a map of language/url
071     * @return the list of urls available (can be null)
072     */
073    public Map<String, String> getUrls()
074    {
075        return _urls;
076    }
077    
078    /**
079     * Get the url for a language, or a default one if the requested language is null
080     * @param language language requested (null if you want any language available, starting with the default one)
081     * @return a help url, or null if nothing is found or map empty
082     */
083    public String getUrl(String language)
084    {
085        if (_urls != null)
086        {
087            return _urls.get(language);
088        }
089        else
090        {
091            return null;
092        }
093    }
094    
095    /**
096     * Get a set of available languages
097     * @return a set of available languages (can not be null)
098     */
099    public Set<String> getLanguages()
100    {
101        if (_urls != null)
102        {
103            return _urls.keySet();
104        }
105        else
106        {
107            return Collections.emptySet();
108        }
109    }
110    
111    /**
112     * Get the the family for this link (for example an extension point)
113     * @return the extension point
114     */
115    public String getFamily()
116    {
117        return _family;
118    }
119    
120    /**
121     * Get the extension id in the extension point, for this link
122     * @return the id in the family (unique in the family)
123     */
124    public String getId()
125    {
126        return _id;
127    }
128}