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.glyph;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.LinkedHashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Map.Entry;
024import java.util.Set;
025
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028
029import org.ametys.core.ui.Callable;
030import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
031
032/**
033 * Manager for glyphs provide by the application
034 *
035 */
036public class ApplicationGlyphManager extends AbstractThreadSafeComponentExtensionPoint<GlyphProvider>
037{
038    private CssFontHelper _cssFontHelper;
039    
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _cssFontHelper = (CssFontHelper) smanager.lookup(CssFontHelper.ROLE);
045    }
046    
047    /**
048     * Determines if the back office has available glyphs
049     * @return true if the back office has available glyphs
050     */
051    @Callable
052    public boolean hasGlyphs()
053    {
054        return !getGlyphs().isEmpty();
055    }
056    
057    /**
058     * Get the glyphs provide by the application
059     * @return The CSS class names for glyphs
060     */
061    @Callable
062    public List<Map<String, String>> getGlyphsStore()
063    {
064        List<Map<String, String>> glyphs = new ArrayList<>();
065
066        Set<String> glyphClassNames = getGlyphs();
067        for (String glyphClassName : glyphClassNames)
068        {
069            Map<String, String> glyph = new HashMap<>();
070            glyph.put("cssClassName", glyphClassName);
071            glyphs.add(glyph);
072        }
073        
074        return glyphs;
075    }
076    
077    /**
078     * Get the CSS class names for glyphs provide by the application
079     * @return The CSS class names for glyphs
080     */
081    public Set<String> getGlyphs()
082    {
083        Set<String> glyphs = new LinkedHashSet<>();
084        
085        for (String id : getExtensionsIds())
086        {
087            GlyphProvider glyphProvider = getExtension(id);
088            
089            Map<String, String> cssFiles = glyphProvider.getCSSFiles();
090            
091            for (Entry<String, String> cssFile : cssFiles.entrySet())
092            {
093                String cssFileURI = cssFile.getKey();
094                String prefix = cssFile.getValue();
095                glyphs.addAll(_cssFontHelper.getGlyphClassNames(cssFileURI, prefix));
096            }
097        }
098        
099        return glyphs;
100    }
101}