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 */
016
017package org.ametys.runtime.config;
018
019import java.io.IOException;
020import java.io.StringWriter;
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.runtime.parameter.Parameter;
028import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
029
030import com.fasterxml.jackson.core.JsonFactory;
031import com.fasterxml.jackson.core.JsonGenerator;
032import com.fasterxml.jackson.databind.ObjectMapper;
033
034/**
035 * This class represents a configuration parameter
036 */
037public class ConfigParameter extends Parameter<ParameterType> implements Comparable<ConfigParameter>
038{
039    private I18nizableText _displayCategory;
040    private I18nizableText _displayGroup;
041    private boolean _groupSwitch;
042    private long _order;
043    private DisableConditions _disableConditions;
044    private final JsonFactory _jsonFactory = new JsonFactory();
045    private final ObjectMapper _objectMapper = new ObjectMapper();
046    
047    /**
048     * Retrieves the display category of the parameter.
049     * @return _displayCategory the display category
050     */
051    public I18nizableText getDisplayCategory()
052    {
053        return _displayCategory;
054    }
055    
056    /**
057     * Sets the display category of the parameter.
058     * @param displayCategory The category of the parameter
059     */
060    public void setDisplayCategory(I18nizableText displayCategory)
061    {
062        _displayCategory = displayCategory;
063    }
064    
065    /**
066     * Retrieves the display group of the parameter
067     * @return _displayGroup the display group
068     */
069    public I18nizableText getDisplayGroup()
070    {
071        return _displayGroup;
072    }
073    
074    /**
075     * Sets the display group of the parameter
076     * @param displayGroup the display group
077     */
078    public void setDisplayGroup(I18nizableText displayGroup)
079    {
080        _displayGroup = displayGroup;
081    }
082    
083    /**
084     * Retrieves the group switch of the parameter if it has one, <code>null</code> otherwise
085     * @return _groupSwitch the group-switch
086     */
087    public boolean isGroupSwitch()
088    {
089        return _groupSwitch;
090    }
091    
092    /***
093     * Sets the group switch of a parameter 
094     * @param groupSwitch the group switch
095     */
096    public void setGroupSwitch(boolean groupSwitch)
097    {
098        _groupSwitch = groupSwitch;
099    }
100    
101    /**
102     * Retrieves the order of a parameter
103     * @return _order the order 
104     */
105    public long getOrder()
106    {
107        return _order;
108    }
109    
110    /**
111     * Sets the order of the parameter
112     * @param order the order
113     */
114    public void setOrder(long order)
115    {
116        _order = order;
117    }
118    
119    /**
120     * Retrieves the disable condition.
121     * @return the disable condition or <code>null</code> if none is defined.
122     */
123    public DisableConditions getDisableConditions()
124    {
125        return _disableConditions;
126    }
127
128    /**
129     * Sets the disable condition.
130     * @param disableConditions the disable condition.
131     */
132    public void setDisableConditions(DisableConditions disableConditions)
133    {
134        _disableConditions = disableConditions;
135    }
136    
137    @Override
138    public int compareTo(ConfigParameter o)
139    {
140        int cat = getDisplayCategory().toString().compareTo(o.getDisplayCategory().toString());
141        if (cat != 0)
142        {
143            return cat;
144        }
145        
146        int gro = getDisplayGroup().toString().compareTo(o.getDisplayGroup().toString());
147        if (gro != 0)
148        {
149            return gro;
150        }
151        
152        int ord = ((Long) this.getOrder()).compareTo(o.getOrder());
153        if (ord != 0)
154        {
155            return ord;
156        }
157        
158        return getId().compareTo(o.getId());
159    }
160 
161    
162    /**
163     * Formats disable conditions into JSON. 
164     * @return the Object as a JSON string.
165     */
166    public String disableConditionsToJSON()
167    {
168        try
169        {
170            StringWriter writer = new StringWriter();
171            
172            JsonGenerator jsonGenerator = _jsonFactory.createGenerator(writer);
173            
174            Map<String, Object> asJson = _disableConditionsAsMap(this.getDisableConditions());
175            _objectMapper.writeValue(jsonGenerator, asJson);
176            
177            return writer.toString();
178        }
179        catch (IOException e)
180        {
181            throw new IllegalArgumentException("The object can not be converted to json string", e);
182        }
183    }
184
185    private Map<String, Object> _disableConditionsAsMap(DisableConditions disableConditions)
186    {
187        Map<String, Object> map = new HashMap<>();
188        
189        // Handle simple conditions
190        List<Map<String, String>> disableConditionList = new ArrayList<>();
191        map.put("condition", disableConditionList);
192        for (DisableCondition disableCondition : disableConditions.getConditions())
193        {
194            Map<String, String> disableConditionAsMap = _disableConditionAsMap(disableCondition);
195            disableConditionList.add(disableConditionAsMap);
196        }
197
198        // Handle nested conditions
199        List<Map<String, Object>> disableConditionsList = new ArrayList<>();
200        map.put("conditions", disableConditionsList);
201        for (DisableConditions subDisableConditions : disableConditions.getSubConditions())
202        {
203            Map<String, Object> disableConditionsAsMap = _disableConditionsAsMap(subDisableConditions);
204            disableConditionsList.add(disableConditionsAsMap);
205        }
206        
207        // Handle type
208        map.put("type", disableConditions.getAssociationType().toString().toLowerCase());
209        
210        return map; 
211    }
212
213    /**
214     * Formats an Object into JSON. 
215     * @param disableCondition The disable condition to convert
216     * @return the Object as a JSON string.
217     */
218    private Map<String, String> _disableConditionAsMap(DisableCondition disableCondition)
219    {
220        Map<String, String> map = new HashMap<>();
221        map.put("id", disableCondition.getId());
222        map.put("operator", disableCondition.getOperator().toString().toLowerCase());
223        map.put("value", disableCondition.getValue());
224        return map;
225    }
226}