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