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", getName());
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", getName());
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    public void setName(String name)
126    {
127        _id = name;
128    }
129    
130    public I18nizableText getLabel()
131    {
132        return _label;
133    }
134
135    public void setLabel(I18nizableText label)
136    {
137        _label = label;
138    }
139    
140    public I18nizableText getDescription()
141    {
142        return _description;
143    }
144    
145    public void setDescription(I18nizableText description)
146    {
147        _description = description;
148    }
149    
150    /**
151     * Retrieves the parameter checker's icon
152     * @return _iconPath the path to the icon representing the parameter checker
153     */
154    public String getSmallIconPath()
155    {
156        return _smallIconPath;
157    }
158    
159    /**
160     * Sets the icon path of the parameter checker
161     * @param path the path of the small icon
162     */
163    public void setSmallIconPath(String path)
164    {
165        _smallIconPath = path;
166    }
167    
168    /**
169     * Retrieves the parameter checker's icon
170     * @return _iconPath the path to the icon representing the parameter checker
171     */
172    public String getMediumIconPath()
173    {
174        return _mediumIconPath;
175    }
176    
177    /**
178     * Sets the icon path of the parameter checker
179     * @param path the path of the medium icon
180     */
181    public void setMediumIconPath(String path)
182    {
183        _mediumIconPath = path;
184    }
185    
186    /**
187     * Retrieves the parameter checker's icon
188     * @return _iconPath the path to the icon representing the parameter checker
189     */
190    public String getLargeIconPath()
191    {
192        return _largeIconPath;
193    }
194    
195    /**
196     * Sets the icon path of the parameter checker
197     * @param path the path of the large icon
198     */
199    public void setLargeIconPath(String path)
200    {
201        _largeIconPath = path;
202    }
203  
204    /**
205     * Retrieves the class of the parameter checker
206     * @return _concreteClass the class of the parameter checker.
207     */
208    public String getConcreteClass()
209    {
210        return _concreteClass;
211    }
212
213    /**
214     * Sets the class of the parameter checker
215     * @param concreteClass the class of the parameter checker
216     */
217    public void setClass(String concreteClass)
218    {
219        this._concreteClass = concreteClass;
220    }
221
222    /**
223     * Gets the ui order of the parameter checker
224     * @return _uiRefOrder the ui order
225     */
226    public int getUiRefOrder()
227    {
228        return _uiRefOrder;
229    }
230    
231    /**
232     * Sets the ui order
233     * @param uiRefOrder the ui order
234     */
235    public void setUiRefOrder(int uiRefOrder)
236    {
237        _uiRefOrder = uiRefOrder;
238    }
239    
240    /**
241     * Get the location of the parameter checker
242     * @return _uiRefOrder the ui order
243     */
244    public String getUiRefLocation()
245    {
246        return _uiRefLocation;
247    }
248    
249    /**
250     * Set the location of the parameter checker
251     * @param uiRefLocation the location of the parameter checker
252     */
253    public void setUiRefLocation(String uiRefLocation)
254    {
255        _uiRefLocation = uiRefLocation;
256    }
257    
258    /**
259     * Retrieve the path of the parameters used by the parameter checker
260     * @return _linkedParamsPaths the paths of the parameters used by the parameter checker
261     **/
262    public Set<String> getLinkedParamsPaths()
263    {
264        return _linkedParamsPaths;
265    }
266    
267    /**
268     * Sets the parameters' ids used by the parameter checker
269     * @param linkedParamsPaths the parameters' ids used by the parameter checker
270     */
271    public void setLinkedParamsPaths(Set<String> linkedParamsPaths)
272    {
273        _linkedParamsPaths = linkedParamsPaths;
274    }
275    
276    /**
277     * Retrieves the parameter checker.
278     * @return _parameterChecker the parameter checker
279     */
280    public ItemChecker getParameterChecker()
281    {
282        return _parameterChecker;
283    }
284    
285    /**
286     * Sets the parameter checker
287     * @param parameterChecker the parameter checker
288     */
289    public void setParameterChecker(ItemChecker parameterChecker)
290    {
291        _parameterChecker = parameterChecker;
292    }
293}