001/*
002 *  Copyright 2023 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.plugins.admin.statistics;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.ametys.runtime.i18n.I18nizableText;
022
023/**
024 * A terminal node in statistics 
025 */
026public class StatisticsValue implements Statistics
027{
028    private Object _value;
029    private String _name;
030    private I18nizableText _label;
031    private String _icon;
032
033    /**
034     * Create a value
035     * @param name The identifier of the node
036     * @param label The label
037     * @param icon The glyph icon 
038     * @param value A jsonizable value. Can be null.
039     */
040    public StatisticsValue(String name, I18nizableText label, String icon, Object value)
041    {
042        _name = name;
043        _label = label;
044        _icon = icon;
045        _value = value;
046    }
047    
048    public String getName()
049    {
050        return _name;
051    }
052    
053    public I18nizableText getLabel()
054    {
055        return _label;
056    }
057    
058    public String getIcon()
059    {
060        return _icon;
061    }
062    
063    /**
064     * The wrapped value
065     * @return the nullable and jsonizable value
066     */
067    public Object getValue()
068    {
069        return _value;
070    }
071    
072    public Map<String, Object> toJSONForExport()
073    {
074        Map<String, Object> m = new HashMap<>();
075        m.put(_name, _value);
076        return m;
077    }
078    
079    public Map<String, Object> toJSONTree()
080    {
081        Map<String, Object> m = new HashMap<>();
082        m.put("name", _name);
083        m.put("label", _label.isI18n() ? StatisticsProviderExtensionPoint._I18N_PREFIX + _label.getCatalogue() + StatisticsProviderExtensionPoint._I18N_SEPARATOR + _label.getKey() + StatisticsProviderExtensionPoint._I18N_SUFFIX : _label.getLabel());
084        m.put("iconCls", _icon);
085        m.put("value", _value);
086        m.put("leaf", true);
087        return m;
088    }
089}