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