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.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025
026import org.ametys.web.repository.page.Page.PageType;
027
028
029/**
030 * A icon for page in sitemap
031 * 
032 */
033public class SitemapIcon
034{
035    /** The tag condition. */
036    public enum Condition
037    {
038        /** Or condition */
039        OR,
040        /** And condition. */
041        AND
042    }
043    
044    private String _iconGlyph;
045    private String _iconDecorator;
046    private String _iconPath;
047    private boolean _live;
048    private boolean _restricted;
049    private List<String> _tags;
050    private PageType _pageType;
051    private Map<String, String> _metadata;
052    private Map<String, String> _properties;
053    private Condition _tagsCondition;
054    private Condition _metadataCondition;
055    private Condition _propertiesCondition;
056    
057    /**
058     * Constructor
059     */
060    public SitemapIcon ()
061    {
062        // nothing
063    }
064    
065    /**
066     * Configure decoration
067     * @param configuration the configuration
068     * @throws ConfigurationException if configuration is invalid
069     */
070    public void configure (Configuration configuration) throws ConfigurationException
071    {
072        this._iconGlyph = configuration.getChild("glyph").getValue(null);
073        this._iconDecorator = configuration.getChild("decorator").getValue(null);
074        this._iconPath = _configureIcon (configuration.getChild("image", false));
075        this._tags = _configureTags(configuration.getChild("conditions").getChild("tags"));
076        this._pageType = _configurePageType(configuration.getChild("conditions").getChild("type"));
077        this._metadata = _configureMetadata(configuration.getChild("conditions").getChild("metadata"));
078        this._properties = _configureProperties(configuration.getChild("conditions").getChild("properties"));
079        this._live = configuration.getChild("conditions").getChild("live", false) != null;
080        this._restricted = configuration.getChild("conditions").getChild("restricted", false) != null;
081    }
082    
083    /**
084     * The icon path
085     * @return The icon path. Can be null.
086     */
087    public String getIcon()
088    {
089        return _iconPath;
090    }
091    
092    /**
093     * The icon glyph
094     * @return The icon glyph. Can be null.
095     */
096    public String getIconGlyph()
097    {
098        return _iconGlyph;
099    }
100    
101    /**
102     * The icon decorator
103     * @return The icon decorator
104     */
105    public String getIconDecorator()
106    {
107        return _iconDecorator;
108    }
109    
110    /**
111     * Get the metadata condition
112     * @return the metadata condition
113     */
114    public Condition getMetadataCondition ()
115    {
116        return _metadataCondition;
117    }
118    
119    /**
120     * The tags condition
121     * @return The tags condition
122     */
123    public Condition getTagsCondition ()
124    {
125        return _tagsCondition;
126    }
127    
128    /**
129     * Get the metadata condition
130     * @return the metadata condition
131     */
132    public Condition getPropertiesCondition ()
133    {
134        return _propertiesCondition;
135    }
136    
137    /**
138     * The tags
139     * @return The tags
140     */
141    public List<String> getTags ()
142    {
143        return _tags;
144    }
145    
146    /**
147     * The page type
148     * @return The page type
149     */
150    public PageType getPageType ()
151    {
152        return _pageType;
153    }
154    
155    /**
156     * Determines the live version
157     * @return true if this icon is for live version
158     */
159    public boolean isLive ()
160    {
161        return _live;
162    }
163    
164    /**
165     * Determines the limited access
166     * @return true if this icon is for page with limited access
167     */
168    public boolean isRestricted ()
169    {
170        return _restricted;
171    }
172    
173    /**
174     * Get metadata
175     * @return the metadata
176     */
177    public Map<String, String> getMetadata ()
178    {
179        return this._metadata;
180    }
181    
182    /**
183     * Get properties
184     * @return the properties
185     */
186    public Map<String, String> getProperties ()
187    {
188        return this._properties;
189    }
190
191    private String _configureIcon (Configuration iconConf) throws ConfigurationException
192    {
193        if (iconConf == null)
194        {
195            return null;
196        }
197        
198        String pluginName = iconConf.getAttribute("plugin");
199        String path = iconConf.getValue();
200        
201        return "/plugins/" + pluginName + "/resources/" + path;
202    }
203    
204    private List<String> _configureTags (Configuration tagsConf) throws ConfigurationException
205    {
206        List<String> tags = new ArrayList<>();
207        
208        this._tagsCondition = Condition.valueOf(tagsConf.getAttribute("type", "AND").toUpperCase());
209        
210        Configuration[] children = tagsConf.getChildren("tag");
211        for (Configuration child : children)
212        {
213            tags.add(child.getValue());
214        }
215        return tags;
216    }
217    
218    private PageType _configurePageType (Configuration child)
219    {
220        String value = child.getValue(null);
221        return value != null ? PageType.valueOf(value.toUpperCase()) : null;
222    }
223    
224    private Map<String, String> _configureMetadata (Configuration metadataConf) throws ConfigurationException
225    {
226        Map<String, String> metadata = new HashMap<>();
227        
228        this._metadataCondition = Condition.valueOf(metadataConf.getAttribute("type", "AND").toUpperCase());
229        
230        Configuration[] children = metadataConf.getChildren("metadata");
231        for (Configuration child : children)
232        {
233            metadata.put(child.getAttribute("name"), child.getValue(""));
234        }
235        return metadata;
236    }
237    
238    private Map<String, String> _configureProperties (Configuration propConf) throws ConfigurationException
239    {
240        Map<String, String> properties = new HashMap<>();
241        
242        this._propertiesCondition = Condition.valueOf(propConf.getAttribute("type", "AND").toUpperCase());
243        
244        Configuration[] children = propConf.getChildren("property");
245        for (Configuration child : children)
246        {
247            properties.put(child.getAttribute("name"), child.getValue(""));
248        }
249        return properties;
250    }
251}