001/*
002 *  Copyright 2022 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.plugins.workspaces.util;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.runtime.i18n.I18nizableText;
026
027/**
028 * This class represents a result column
029 */
030public class StatisticColumn
031{
032
033    private String _id;
034
035    private I18nizableText _label;
036    
037    private StatisticsColumnType _type;
038
039    private Integer _width;
040
041    private String _renderer;
042
043    private String _converter;
044
045    private Boolean _hidden;
046
047    private Boolean _editable;
048
049    private Boolean _sortable;
050
051    private Boolean _multiple;
052
053    private Boolean _filter;
054
055    private String _defaultSorter;
056
057    private List<StatisticColumn> _subColumns;
058
059    private String _group;
060
061    /**
062     * Create a StatisticColumn
063     * @param id the id of StatisticColumn
064     * @param label the label of StatisticColumn
065     */
066    public StatisticColumn(String id, I18nizableText label)
067    {
068        this._id = id;
069        this._label = label;
070        this._subColumns = new ArrayList<>();
071        this._filter = true;
072    }
073        
074    /**
075     * Set the id
076     * @param id the id
077     * @return StatisticColumn with a set id
078     */
079    public StatisticColumn withId(String id)
080    {
081        this._id = id;
082        return this;
083    }
084
085    /**
086     * Set the label
087     * @param label the label
088     * @return StatisticColumn with a set label
089     */
090    public StatisticColumn withLabel(I18nizableText label)
091    {
092        this._label = label;
093        return this;
094    }
095    
096    /**
097     * Set the type
098     * @param type the type
099     * @return StatisticColumn with a set type
100     */
101    public StatisticColumn withType(StatisticsColumnType type)
102    {
103        this._type = type;
104        return this;
105    }
106
107    /**
108     * Set the width
109     * @param width the width
110     * @return StatisticColumn with a set width
111     */
112    public StatisticColumn withWidth(int width)
113    {
114        this._width = width;
115        return this;
116    }
117
118    /**
119     * Set the renderer
120     * @param renderer the renderer
121     * @return StatisticColumn with a set renderer
122     */
123    public StatisticColumn withRenderer(String renderer)
124    {
125        this._renderer = renderer;
126        return this;
127    }
128
129    /**
130     * Set the converter
131     * @param converter the converter
132     * @return StatisticColumn with a set converter
133     */
134    public StatisticColumn withConverter(String converter)
135    {
136        this._converter = converter;
137        return this;
138    }
139
140    /**
141     * Set the hidden boolean
142     * @param hidden is the column hidden
143     * @return StatisticColumn with a set hidden value
144     */
145    public StatisticColumn isHidden(boolean hidden)
146    {
147        this._hidden = hidden;
148        return this;
149    }
150
151    /**
152     * Set the editable boolean
153     * @param editable is the column editable
154     * @return StatisticColumn with a set editable value
155     */
156    public StatisticColumn withEditable(boolean editable)
157    {
158        this._editable = editable;
159        return this;
160    }
161
162    /**
163     * Set the sortable boolean
164     * @param sortable is the column sortable
165     * @return StatisticColumn with a set sortable value
166     */
167    public StatisticColumn withSortable(boolean sortable)
168    {
169        this._sortable = sortable;
170        return this;
171    }
172
173    /**
174     * Set the multiple boolean
175     * @param multiple is the column multiple
176     * @return StatisticColumn with a set multiple value
177     */
178    public StatisticColumn withMultiple(boolean multiple)
179    {
180        this._multiple = multiple;
181        return this;
182    }
183
184    /**
185     * Set the defaultSorter
186     * @param defaultSorter the defaultSorter
187     * @return StatisticColumn with a set defaultSorter
188     */
189    public StatisticColumn withDefaultSorter(String defaultSorter)
190    {
191        this._defaultSorter = defaultSorter;
192        return this;
193    }
194
195    /**
196     * Set the group
197     * @param group the group
198     * @return StatisticColumn with a set group
199     */
200    public StatisticColumn withGroup(String group)
201    {
202        this._group = group;
203        return this;
204    }
205
206    /**
207     * Set the filter boolean
208     * @param filter is the column filterable
209     * @return StatisticColumn with a set filter value
210     */
211    public StatisticColumn withFilter(boolean filter)
212    {
213        this._filter = filter;
214        return this;
215    }
216    
217    /**
218     * Set the columns
219     * @param subColumn the column
220     * @return StatisticColumn with a set columns
221     */
222    public StatisticColumn addSubColumn(StatisticColumn subColumn)
223    {
224        this._subColumns.add(subColumn);
225        return this;
226    }
227    
228    /**
229     * Set the columns
230     * @param subColumns the columns
231     * @return StatisticColumn with a set columns
232     */
233    public StatisticColumn addSubColumns(List<StatisticColumn> subColumns)
234    {
235        this._subColumns.addAll(subColumns);
236        return this;
237    }
238
239    /**
240     * Get the group of the column
241     * @return the group of the column
242     */
243    public String getGroup()
244    {
245        return _group;
246    }
247    
248    /**
249     * Get the id of the column
250     * @return the id of the column
251     */
252    public String getId()
253    {
254        return _id;
255    }
256    
257    /**
258     * Get the label
259     * @return The label
260     */
261    public I18nizableText getLabel()
262    {
263        return _label;
264    }
265
266    /**
267     * convert the StatisticColumn into a map
268     * @return the map of StatisticColumn attributes
269     */
270    public Map<String, Object> convertToJSON()
271    {
272        Map<String, Object> json = new HashMap<>();
273        
274        json.put("id", _id);
275        json.put("path", _id);
276        json.put("label", _label);
277        json.put("type", _type != null ? _type.getId().toLowerCase() : StringUtils.EMPTY);
278        json.put("width", _width);
279        json.put("renderer", _renderer);
280        json.put("converter", _converter);
281        json.put("hidden", _hidden);
282        json.put("editable", _editable);
283        json.put("sortable", _sortable);
284        json.put("multiple", _multiple);
285        json.put("filter", _filter);
286        json.put("defaultSorter", _defaultSorter);
287
288        if (!this._subColumns.isEmpty())
289        {
290            List<Object> jsonObject = new ArrayList<>();
291            
292            for (StatisticColumn column : _subColumns)
293            {
294                jsonObject.add(column.convertToJSON());
295            }
296            
297            json.put("subColumns", jsonObject);
298        }
299
300        return json;
301    }
302}