001/*
002 *  Copyright 2016 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.runtime.parameter;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020import java.util.Set;
021import java.util.stream.Collectors;
022
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.cocoon.xml.XMLUtils;
025import org.apache.commons.lang3.StringUtils;
026import org.xml.sax.ContentHandler;
027import org.xml.sax.SAXException;
028
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * Descriptor of a parameter checker 
033 */
034public class ParameterCheckerDescriptor extends AbstractLogEnabled
035{
036    /** The parameter checker's id */
037    protected String _id;
038    
039    /** The parameters checker's description */
040    protected I18nizableText _description;
041    
042    /** The parameter checker's label */
043    protected I18nizableText _label;
044    
045    /** The path of the small icon*/
046    protected String _smallIconPath;
047    
048    /** The path of the medium icon*/
049    protected String _mediumIconPath;
050    
051    /** The path of the large icon*/
052    protected String _largeIconPath;
053    
054    /** The concrete class of the parameter checker */
055    protected String _concreteClass;
056    
057    /** The order of the parameter checker. When several parameter checkers have the same location, 
058     *  the order allows to order graphically the parameter checkers: the parameter checker with the lowest
059     *  order will be at the top. */
060    protected int _uiRefOrder;
061    
062    /** The location of the parameter checker */
063    protected String _uiRefLocation;
064    
065    /** The configuration of the linked parameters */
066    protected Set<String> _linkedParamsPaths;
067    
068    /** The concrete class of the parameter checker implementing the check */
069    protected ParameterChecker _parameterChecker;
070
071    /**
072     * SAX the description informations
073     * @param handler The handler where to sax
074     * @throws SAXException if an error occurred
075     */
076    public void toSAX(ContentHandler handler) throws SAXException
077    {
078        XMLUtils.createElement(handler, "id", getId());
079        getLabel().toSAX(handler, "label");
080        getDescription().toSAX(handler, "description");
081
082        XMLUtils.createElement(handler, "small-icon-path", getSmallIconPath());
083        XMLUtils.createElement(handler, "medium-icon-path", getMediumIconPath());
084        XMLUtils.createElement(handler, "large-icon-path", getLargeIconPath());
085        
086        if (getLinkedParamsPaths() != null)
087        {
088            String linkedParamsAsJSON = "[" + StringUtils.join(getLinkedParamsPaths().parallelStream().map(s -> "\"" + s + "\"").collect(Collectors.toList()), ", ") + "]"; 
089            XMLUtils.createElement(handler, "linked-fields", linkedParamsAsJSON);
090        }
091
092        XMLUtils.createElement(handler, "order", Integer.toString(getUiRefOrder()));
093    }
094    
095    /**
096     * Get the description information to JSON format
097     * @return The information as a map
098     */
099    public Map<String, Object> toJSON()
100    {
101        Map<String, Object> result = new LinkedHashMap<>();
102        
103        result.put("id", getId());
104        result.put("label", getLabel());
105        result.put("description", getDescription());
106        result.put("small-icon-path", getSmallIconPath());
107        result.put("medium-icon-path", getMediumIconPath());
108        result.put("large-icon-path", getLargeIconPath());
109        
110        if (getLinkedParamsPaths() != null)
111        {
112            result.put("linked-fields", getLinkedParamsPaths());
113        }
114        
115        result.put("order", getUiRefOrder());
116        
117        return result;
118    }
119    
120    /**
121     * Retrieves the parameter checker's id
122     * @return _id the id of the parameter checker 
123     */
124    public String getId()
125    {
126        return _id;
127    }
128    
129    /**
130     * Sets the parameter checker's id
131     * @param id the id of the parameter checker
132     */
133    public void setId(String id)
134    {
135        _id = id;
136    }
137    
138    /**
139     * Retrieves the parameter's checker label.
140     * @return _label the label of the parameter checker
141     */
142    public I18nizableText getLabel()
143    {
144        return _label;
145    }
146
147    /**
148     * Sets the parameter's checker label.
149     * @param label the label of the parameter checker
150     */
151    public void setLabel(I18nizableText label)
152    {
153        _label = label;
154    }
155    
156    /**
157     * Retrieves the parameter's checker description.
158     * @return _description the description of the parameter checker
159     */
160    public I18nizableText getDescription()
161    {
162        return _description;
163    }
164    
165    /**
166     * Sets the parameter's checker description.
167     * @param description the description of the parameter checker
168     */
169    public void setDescription(I18nizableText description)
170    {
171        _description = description;
172    }
173    
174    /** 
175     * Retrieves the parameter checker's icon 
176     * @return _iconPath the path to the icon representing the parameter checker
177     */
178    public String getSmallIconPath()
179    {
180        return _smallIconPath;
181    }
182    
183    /**
184     * Sets the icon path of the parameter checker
185     * @param path the path of the small icon
186     */
187    public void setSmallIconPath(String path)
188    {
189        _smallIconPath = path;
190    }
191    
192    /** 
193     * Retrieves the parameter checker's icon 
194     * @return _iconPath the path to the icon representing the parameter checker
195     */
196    public String getMediumIconPath()
197    {
198        return _mediumIconPath;
199    }
200    
201    /**
202     * Sets the icon path of the parameter checker
203     * @param path the path of the medium icon
204     */
205    public void setMediumIconPath(String path)
206    {
207        _mediumIconPath = path;
208    }
209    
210    /** 
211     * Retrieves the parameter checker's icon 
212     * @return _iconPath the path to the icon representing the parameter checker
213     */
214    public String getLargeIconPath()
215    {
216        return _largeIconPath;
217    }
218    
219    /**
220     * Sets the icon path of the parameter checker
221     * @param path the path of the large icon
222     */
223    public void setLargeIconPath(String path)
224    {
225        _largeIconPath = path;
226    }
227  
228    /**
229     * Retrieves the class of the parameter checker
230     * @return _concreteClass the class of the parameter checker.
231     */
232    public String getConcreteClass()
233    {
234        return _concreteClass;
235    }
236
237    /**
238     * Sets the class of the parameter checker
239     * @param concreteClass the class of the parameter checker
240     */
241    public void setClass(String concreteClass)
242    {
243        this._concreteClass = concreteClass;
244    }
245
246    /**
247     * Gets the ui order of the parameter checker
248     * @return _uiRefOrder the ui order
249     */
250    public int getUiRefOrder()
251    {
252        return _uiRefOrder;
253    }
254    
255    /**
256     * Sets the ui order
257     * @param uiRefOrder the ui order
258     */
259    public void setUiRefOrder(int uiRefOrder)
260    {
261        _uiRefOrder = uiRefOrder;
262    }
263    
264    /**
265     * Get the location of the parameter checker
266     * @return _uiRefOrder the ui order
267     */
268    public String getUiRefLocation()
269    {
270        return _uiRefLocation;
271    }
272    
273    /**
274     * Set the location of the parameter checker
275     * @param uiRefLocation the location of the parameter checker
276     */
277    public void setUiRefLocation(String uiRefLocation)
278    {
279        _uiRefLocation = uiRefLocation;
280    }
281    
282    /**
283     * Retrieve the path of the parameters used by the parameter checker
284     * @return _linkedParamsPaths the paths of the parameters used by the parameter checker
285     **/
286    public Set<String> getLinkedParamsPaths()
287    {
288        return _linkedParamsPaths;
289    }
290    
291    /**
292     * Sets the parameters' ids used by the parameter checker
293     * @param linkedParamsPaths the parameters' ids used by the parameter checker
294     */
295    public void setLinkedParamsPaths(Set<String> linkedParamsPaths)
296    {
297        _linkedParamsPaths = linkedParamsPaths;    
298    }
299    
300    /**
301     * Retrieves the parameter checker.
302     * @return _parameterChecker the parameter checker
303     */
304    public ParameterChecker getParameterChecker()
305    {
306        return _parameterChecker;
307    }
308    
309    /**
310     * Sets the parameter checker
311     * @param parameterChecker the parameter checker 
312     */
313    public void setParameterChecker(ParameterChecker parameterChecker)
314    {
315        _parameterChecker = parameterChecker;
316    }
317}