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