001/*
002 *  Copyright 2011 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.web.sitemap;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.Comparator;
021import java.util.List;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.context.Context;
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.Contextualizable;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.page.SitemapElement;
033import org.ametys.web.repository.site.Site;
034
035/**
036 * Component to handle {@link SitemapTreeIndicator}s
037 *
038 */
039public class SitemapIndicatorsHandler implements Serviceable, Component, Contextualizable
040{
041    /** Avalon Role */
042    public static final String ROLE = SitemapIndicatorsHandler.class.getName();
043    
044    /** The context */
045    protected Context _context;
046    
047    private SitemapIconsProviderExtensionPoint _sitemapIconsProviderEP;
048    private SitemapDecoratorsProviderExtensionPoint _sitemapDecoratorsProviderEP;
049    
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        _sitemapIconsProviderEP = (SitemapIconsProviderExtensionPoint) manager.lookup(SitemapIconsProviderExtensionPoint.ROLE);
054        _sitemapDecoratorsProviderEP = (SitemapDecoratorsProviderExtensionPoint) manager.lookup(SitemapDecoratorsProviderExtensionPoint.ROLE);
055    }
056    
057    @Override
058    public void contextualize(Context context) throws ContextException
059    {
060        _context = context;
061    }
062
063    /**
064     * Get the list of available icons for a site
065     * @param site the site name
066     * @return the list of available icons
067     */
068    public List<SitemapTreeIndicator> getIcons(Site site)
069    {
070        String skinId = site.getSkinId();
071        return _getIcons(skinId);
072    }
073    
074    private List<SitemapTreeIndicator> _getIcons(String skinId)
075    {
076        List<SitemapTreeIndicator> icons = new ArrayList<>();
077        
078        for (String extensionId : _sitemapIconsProviderEP.getExtensionsIds())
079        {
080            SitemapIconsProvider sitemapIconsProvider = _sitemapIconsProviderEP.getExtension(extensionId);
081            icons.addAll(sitemapIconsProvider.getIcons(skinId));
082        }
083        
084        Collections.sort(icons, new IconOrderComparator());
085        
086        return icons;
087    }
088    
089    /**
090     * Get the icon for a page
091     * @param page the page
092     * @return the page's icon or null if no icon matches.
093     */
094    public SitemapTreeIndicator getIcon(Page page)
095    {
096        String skinId = page.getSite().getSkinId();
097        
098        List<SitemapTreeIndicator> icons = _getIcons(skinId);
099        for (SitemapTreeIndicator icon : icons)
100        {
101            if (icon.matches(page))
102            {
103                return icon;
104            }
105        }
106        
107        return null;
108    }
109    
110    /**
111     * Get the list of available decorators for a site
112     * @param site the site
113     * @return the list of available decorators
114     */
115    public List<SitemapTreeIndicator> getDecorators(Site site)
116    {
117        String skinId = site.getSkinId();
118        return _getDecorators(skinId);
119    }
120    
121    private List<SitemapTreeIndicator> _getDecorators(String skinId)
122    {
123        List<SitemapTreeIndicator> decorators = new  ArrayList<>();
124        for (String extensionId : _sitemapDecoratorsProviderEP.getExtensionsIds())
125        {
126            SitemapDecoratorsProvider sitemapDecoratorsProvider = _sitemapDecoratorsProviderEP.getExtension(extensionId);
127            decorators.addAll(sitemapDecoratorsProvider.getDecorators(skinId));
128        }
129        
130        Collections.sort(decorators, new IconOrderComparator());
131        
132        return decorators;
133    }
134    
135    /**
136     * Get the list of decorators matching the given sitemap element
137     * @param sitemapElement the sitemap element
138     * @return the decorators matching the sitemap element
139     */
140    public List<SitemapTreeIndicator> getDecorators(SitemapElement sitemapElement)
141    {
142        String skinId = sitemapElement.getSite().getSkinId();
143        return _getDecorators(skinId).stream()
144                    .filter(d -> d.matches(sitemapElement))
145                    .toList();
146    }
147    
148    class IconOrderComparator implements Comparator<SitemapTreeIndicator>
149    {
150        @Override
151        public int compare(SitemapTreeIndicator i1, SitemapTreeIndicator i2)
152        {
153            return i1.getPriority() - i2.getPriority();
154        }
155    }
156
157}