001/*
002 *  Copyright 2017 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.web.site;
018
019import java.util.HashMap;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.configuration.Configurable;
025import org.apache.avalon.framework.configuration.Configuration;
026import org.apache.avalon.framework.configuration.ConfigurationException;
027import org.apache.commons.lang3.StringUtils;
028
029/**
030 * Component listing the available colors for a site
031 */
032public class SiteColorsComponent implements Component, Configurable
033{
034    /** The component role */
035    public static final String ROLE = SiteColorsComponent.class.getName();
036    
037    private Map<String, Map<String, String>> _colors;
038    private String _defaultKey;
039    
040    public void configure(Configuration configuration) throws ConfigurationException
041    {
042        Map<String, Map<String, String>> siteColors = new LinkedHashMap<>();
043        String defaultKey = configuration.getAttribute("default", "");
044        for (Configuration colorConfiguration : configuration.getChildren("color"))
045        {
046            Map<String, String> color = new HashMap<>();
047            
048            for (Configuration child : colorConfiguration.getChildren())
049            {
050                String name = child.getName();
051                String value = child.getValue();
052                
053                color.put(name, value);
054            }
055
056            String id = colorConfiguration.getAttribute("id");
057            siteColors.put(id, color);
058            
059            if (StringUtils.isEmpty(defaultKey))
060            {
061                defaultKey = id;
062            }
063        }
064        
065        _defaultKey = defaultKey;
066        _colors = siteColors;
067    }
068    
069    /**
070     * Get the key to use as default in the colors
071     * @return A non null key.
072     */
073    public String getDefaultKey()
074    {
075        return _defaultKey;
076    }
077    
078    /**
079     * Get the colors by index
080     * A 'color' is an association key-hexacolor. Where key can be 'main', 'text'... 
081     * @return The map
082     */
083    public Map<String, Map<String, String>> getColors()
084    {
085        return _colors;
086    }
087}