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.model.checker;
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.xml.sax.ContentHandler;
026import org.xml.sax.SAXException;
027
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.runtime.util.ModifiableLabelable;
030
031/**
032 * Descriptor of a parameter checker
033 */
034public class ItemCheckerDescriptor extends AbstractLogEnabled implements ModifiableLabelable
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 ItemChecker _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 = "[" + getLinkedParamsPaths().parallelStream().map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")) + "]";
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    public String getName()
121    {
122        return _id;
123    }
124    
125    /**
126     * Retrieves the parameter checker's id
127     * @return _id the id of the parameter checker
128     * @deprecated use {@link #getName()} instead
129     */
130    @Deprecated
131    public String getId()
132    {
133        return _id;
134    }
135    
136    public void setName(String name)
137    {
138        _id = name;
139    }
140    
141    /**
142     * Sets the parameter checker's id
143     * @param id the id of the parameter checker
144     * @deprecated use {@link #setName(String)} instead
145     */
146    @Deprecated
147    public void setId(String id)
148    {
149        _id = id;
150    }
151    
152    public I18nizableText getLabel()
153    {
154        return _label;
155    }
156
157    public void setLabel(I18nizableText label)
158    {
159        _label = label;
160    }
161    
162    public I18nizableText getDescription()
163    {
164        return _description;
165    }
166    
167    public void setDescription(I18nizableText description)
168    {
169        _description = description;
170    }
171    
172    /**
173     * Retrieves the parameter checker's icon
174     * @return _iconPath the path to the icon representing the parameter checker
175     */
176    public String getSmallIconPath()
177    {
178        return _smallIconPath;
179    }
180    
181    /**
182     * Sets the icon path of the parameter checker
183     * @param path the path of the small icon
184     */
185    public void setSmallIconPath(String path)
186    {
187        _smallIconPath = path;
188    }
189    
190    /**
191     * Retrieves the parameter checker's icon
192     * @return _iconPath the path to the icon representing the parameter checker
193     */
194    public String getMediumIconPath()
195    {
196        return _mediumIconPath;
197    }
198    
199    /**
200     * Sets the icon path of the parameter checker
201     * @param path the path of the medium icon
202     */
203    public void setMediumIconPath(String path)
204    {
205        _mediumIconPath = path;
206    }
207    
208    /**
209     * Retrieves the parameter checker's icon
210     * @return _iconPath the path to the icon representing the parameter checker
211     */
212    public String getLargeIconPath()
213    {
214        return _largeIconPath;
215    }
216    
217    /**
218     * Sets the icon path of the parameter checker
219     * @param path the path of the large icon
220     */
221    public void setLargeIconPath(String path)
222    {
223        _largeIconPath = path;
224    }
225  
226    /**
227     * Retrieves the class of the parameter checker
228     * @return _concreteClass the class of the parameter checker.
229     */
230    public String getConcreteClass()
231    {
232        return _concreteClass;
233    }
234
235    /**
236     * Sets the class of the parameter checker
237     * @param concreteClass the class of the parameter checker
238     */
239    public void setClass(String concreteClass)
240    {
241        this._concreteClass = concreteClass;
242    }
243
244    /**
245     * Gets the ui order of the parameter checker
246     * @return _uiRefOrder the ui order
247     */
248    public int getUiRefOrder()
249    {
250        return _uiRefOrder;
251    }
252    
253    /**
254     * Sets the ui order
255     * @param uiRefOrder the ui order
256     */
257    public void setUiRefOrder(int uiRefOrder)
258    {
259        _uiRefOrder = uiRefOrder;
260    }
261    
262    /**
263     * Get the location of the parameter checker
264     * @return _uiRefOrder the ui order
265     */
266    public String getUiRefLocation()
267    {
268        return _uiRefLocation;
269    }
270    
271    /**
272     * Set the location of the parameter checker
273     * @param uiRefLocation the location of the parameter checker
274     */
275    public void setUiRefLocation(String uiRefLocation)
276    {
277        _uiRefLocation = uiRefLocation;
278    }
279    
280    /**
281     * Retrieve the path of the parameters used by the parameter checker
282     * @return _linkedParamsPaths the paths of the parameters used by the parameter checker
283     **/
284    public Set<String> getLinkedParamsPaths()
285    {
286        return _linkedParamsPaths;
287    }
288    
289    /**
290     * Sets the parameters' ids used by the parameter checker
291     * @param linkedParamsPaths the parameters' ids used by the parameter checker
292     */
293    public void setLinkedParamsPaths(Set<String> linkedParamsPaths)
294    {
295        _linkedParamsPaths = linkedParamsPaths;
296    }
297    
298    /**
299     * Retrieves the parameter checker.
300     * @return _parameterChecker the parameter checker
301     */
302    public ItemChecker getParameterChecker()
303    {
304        return _parameterChecker;
305    }
306    
307    /**
308     * Sets the parameter checker
309     * @param parameterChecker the parameter checker
310     */
311    public void setParameterChecker(ItemChecker parameterChecker)
312    {
313        _parameterChecker = parameterChecker;
314    }
315}