001/*
002 *  Copyright 2010 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.repository.site;
017
018import org.apache.avalon.framework.configuration.Configurable;
019import org.apache.avalon.framework.configuration.Configuration;
020import org.apache.avalon.framework.configuration.ConfigurationException;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.avalon.framework.thread.ThreadSafe;
025import org.apache.excalibur.source.SourceResolver;
026
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.plugin.component.PluginAware;
029
030/**
031 * Type of site which is retrieved from a XML configuration.
032 */
033public class DefaultSiteType implements SiteType, Configurable, PluginAware, ThreadSafe, Serviceable
034{
035    /** Plugin name. */
036    protected String _pluginName;
037    /** Site type id. */
038    protected String _id;
039    /** Site type name. */
040    protected String _name;
041    /** Label. */
042    protected I18nizableText _label;
043    /** Description. */
044    protected I18nizableText _description;
045    /** Glyph icon */
046    protected String _iconGlyph;
047    /** Small icon URI 16x16. */
048    protected String _smallIcon;
049    /** Medium icon URI 32x32. */
050    protected String _mediumIcon;
051    /** Large icon URI 48x48. */
052    protected String _largeIcon;
053    
054    /** The source resolver */
055    protected SourceResolver _srcResolver;
056
057    @Override
058    public void service(ServiceManager smanager) throws ServiceException
059    {
060        _srcResolver = (SourceResolver) smanager.lookup(SourceResolver.ROLE);
061    }
062    
063    @Override
064    public void configure(Configuration configuration) throws ConfigurationException
065    {
066        _name = configuration.getChild("name").getValue();
067        
068        _label = _parseI18nizableText(configuration, "label");
069        _description = _parseI18nizableText(configuration, "description");
070        
071        _iconGlyph = configuration.getChild("icons").getChild("glyph").getValue(null);
072        
073        _smallIcon = _parseIcon(configuration.getChild("icons"), "small");
074        _mediumIcon = _parseIcon(configuration.getChild("icons"), "medium");
075        _largeIcon = _parseIcon(configuration.getChild("icons"), "large");
076    }
077    
078    @Override
079    public String getId()
080    {
081        return _id;
082    }
083    
084    @Override
085    public String getName()
086    {
087        return _name;
088    }
089    
090    @Override
091    public I18nizableText getLabel()
092    {
093        return _label;
094    }
095    
096    @Override
097    public I18nizableText getDescription()
098    {
099        return _description;
100    }
101    
102    @Override
103    public String getIconGlyph()
104    {
105        return _iconGlyph;
106    }
107    
108    @Override
109    public String getLargeIcon()
110    {
111        return _largeIcon;
112    }
113
114    @Override
115    public String getMediumIcon()
116    {
117        return _mediumIcon;
118    }
119    
120    @Override
121    public String getSmallIcon()
122    {
123        return _smallIcon;
124    }
125    
126    @Override
127    public String getPluginName()
128    {
129        return _pluginName;
130    }
131
132    @Override
133    public void setPluginInfo(String pluginName, String featureName, String id)
134    {
135        _id = id;
136        _pluginName = pluginName;
137    }
138    
139    /**
140     * Parse an i18n text.
141     * @param config the configuration to use.
142     * @param name the child name.
143     * @return the i18n text.
144     * @throws ConfigurationException if the configuration is not valid.
145     */
146    protected I18nizableText _parseI18nizableText(Configuration config, String name) throws ConfigurationException
147    {
148        return I18nizableText.parseI18nizableText(config.getChild(name), "plugin." + _pluginName, "");
149    }
150    
151    /**
152     * Parse an icon path
153     * @param configuration the configuration to use
154     * @param name the child name.
155     * @return The icon path
156     * @throws ConfigurationException if the configuration is not valid.
157     */
158    protected String _parseIcon (Configuration configuration, String name) throws ConfigurationException
159    {
160        Configuration iconConfig = configuration.getChild(name, false);
161        if (iconConfig != null)
162        {
163            String pluginName = iconConfig.getAttribute("plugin", _pluginName);
164            return "/plugins/" + pluginName + "/resources/" + iconConfig.getValue();
165        }
166        
167        return "/plugins/web/resources/img/sitetype/icon-" + name + ".png";
168    }
169
170}