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.skin;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.commons.lang.StringUtils;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import org.ametys.runtime.i18n.I18nizableText;
026
027/**
028 * A zone definition in a template 
029 */
030public class SkinTemplateZone
031{
032    /** The type of a primary zone */
033    public static final String TYPE_PRIMARY = "primary";
034    /** The type of a secondary zone */
035    public static final String TYPE_SECONDARY = "secondary";
036    
037    private static Logger _logger = LoggerFactory.getLogger(SkinTemplateZone.class);
038
039    /** The skin id */
040    protected String _skinId;
041    /** The template id */
042    protected String _templateId;
043    /** Template id (e.g. the directory name) */
044    protected String _id;
045    /** Zone type (primary or secondary) */
046    protected String _type;
047    /** Zone label */
048    protected I18nizableText _label;
049    /** Zone description */
050    protected I18nizableText _description;
051    /** Zone thumbnail 16px */
052    protected String _smallImage;
053    /** Zone thumbnail 32px */
054    protected String _mediumImage;
055    /** Zone thumbnail 48px */
056    protected String _largeImage;
057    /** The zone inheritance description. The key is the name of a template and the value is the name of the zone in that template. This means that this zone will inherit the zone given as value in the parent page if it uses the template given as key */
058    protected Map<String, String> _inheritance;
059    
060    /**
061     * Create a zone
062     * @param skinId The id of the skin owning the template owning the zone
063     * @param templateId The id of the template owning the zone
064     * @param id The id of the zone
065     * @param type The zone type between primary and secondary.
066     * @param label The label of the zone
067     * @param description The description of the zone
068     * @param smallImage The small image to represent the zone
069     * @param mediumImage The medium image to represent the zone
070     * @param largeImage The large image to represent the zone
071     * @param inheritanceString The inheritance scheme following the pattern "TemplateName-&gt;,ZoneNameInThatTemplate,..." where TemplateName can be "*" or "". A completely empty string is equals to "*-&gt;CurrentZoneName". A blank string after the arrow means "no inheritance for this template".
072     */
073    public SkinTemplateZone(String skinId, String templateId, String id, String type, I18nizableText label, I18nizableText description, String smallImage, String mediumImage, String largeImage, String inheritanceString)
074    {
075        _skinId = skinId;
076        _templateId = templateId;
077        _id = id;
078        _type = type;
079        _label = label;
080        _description = description;
081        _smallImage = smallImage;
082        _mediumImage = mediumImage;
083        _largeImage = largeImage;
084        _inheritance = _parseInheritance(inheritanceString);
085    }
086    
087    /**
088     * Parse the inheritance argument for the constructor
089     * @param inheritanceString the representation of the zone inheritance for the templates
090     * @return the inheritance mapping between templates and zones
091     */
092    private Map<String, String> _parseInheritance(String inheritanceString)
093    {
094        if (inheritanceString == null)
095        {
096            return null;
097        }
098
099        Map<String, String> inheritance = new HashMap<>();
100        if (StringUtils.isEmpty(inheritanceString))
101        {
102            inheritance.put(null, _id);
103        }
104        else
105        {
106            String[] inheritenaceParts = inheritanceString.split("[, ]");
107            for (String inheritenacePart : inheritenaceParts)
108            {
109                if (StringUtils.isNotEmpty(inheritenacePart))
110                {
111                    String templateName = null;
112                    String zoneName = "";
113
114                    int i = inheritenacePart.indexOf("->");
115                    if (i == -1)
116                    {
117                        zoneName = inheritenacePart.trim();
118                    }
119                    else
120                    {
121                        templateName = inheritenacePart.substring(0, i).trim();
122                        if ("*".equals(templateName))
123                        {
124                            templateName = null;
125                        }
126                        
127                        zoneName = inheritenacePart.substring(i + 2).trim();
128                        if (StringUtils.isEmpty(zoneName))
129                        {
130                            zoneName = null;
131                        }
132                    }
133
134                    if (inheritance.get(templateName) != null)
135                    {
136                        _logger.warn("In the skin '" + _skinId 
137                                    + "', the template '" + _templateId 
138                                    + "' defines the zone '" + _id 
139                                    + "' which has a problem with its inheritance. It is declared as '" 
140                                    + inheritanceString + "', but this declares twice the same template (" 
141                                    + (templateName != null ? templateName : "*") 
142                                    + "). The first declaration is kept intact.");
143                    }
144                    else
145                    {
146                        inheritance.put(templateName, zoneName);
147                    }
148                }
149            }
150        }
151        
152        return inheritance;
153    }
154
155    /**
156     * The template id
157     * @return the id
158     */
159    public String getId()
160    {
161        return _id;
162    }
163    
164    /**
165     * The template label
166     * @return The label
167     */
168    public I18nizableText getLabel()
169    {
170        return _label;
171    }
172    /**
173     * The template description
174     * @return The description. Can not be null but can be empty
175     */
176    public I18nizableText getDescription()
177    {
178        return _description;
179    }
180    
181    /**
182     * The small image file uri
183     * @return The small image file uri
184     */
185    public String getSmallImage()
186    {
187        return _smallImage;
188    }
189
190    /**
191     * The medium image file uri
192     * @return The medium image file uri
193     */
194    public String getMediumImage()
195    {
196        return _mediumImage;
197    }
198    
199    /**
200     * The large image file uri
201     * @return The large image file uri
202     */
203    public String getLargeImage()
204    {
205        return _largeImage;
206    }
207    
208    /**
209     * Determine if this zone inherits another one
210     * @return true if a inheritance is declared.
211     */
212    public boolean hasInheritance()
213    {
214        return _inheritance != null;
215    }
216    
217    /**
218     * Get the zone type (primary or secondary)
219     * @return the type
220     */
221    public String getType()
222    {
223        return _type;
224    }
225    
226    /**
227     * Get the name of the zone to inherit in the given template
228     * @param templateName The name of the template. Cannot be empty, null or a joker.
229     * @return The name of a zone in that template, or null is not applicable for this template.
230     */
231    public String getInheritance(String templateName)
232    {
233        if (hasInheritance())
234        {
235            if (_inheritance.containsKey(templateName))
236            {
237                return _inheritance.get(templateName);
238            }
239            else
240            {
241                return _inheritance.get(null);
242            }
243        }
244        else
245        {
246            return null;
247        }
248    }
249}