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.LinkedHashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.ametys.runtime.i18n.I18nizableText;
023
024/**
025 * A node in the statistics tree 
026 */
027public class StatisticsNode extends StatisticsValue
028{
029    private List<Statistics> _children;
030    private boolean _expanded;
031
032    /**
033     * Create a node
034     * @param name The identifier of the node
035     * @param label The label
036     * @param icon The glyph icon
037     * @param value A jsonizable value. Can be null.
038     * @param children The children
039     * @param expanded Is the tree expanded by default
040     */
041    public StatisticsNode(String name, I18nizableText label, String icon, Object value, List<Statistics> children, boolean expanded)
042    {
043        super(name, label, icon, value);
044        _children = children;
045        _expanded = expanded;
046    }
047
048    /**
049     * The children of the node
050     * @return the children
051     */
052    public List<Statistics> getChildren()
053    {
054        return _children;
055    }
056    
057    @Override
058    public Map<String, Object> toJSONForExport()
059    {
060        Map<String, Object> children = new LinkedHashMap<>();
061        for (Statistics statistics : _children)
062        {
063            children.putAll(statistics.toJSONForExport());
064        }
065        
066        children.put("_value", getValue());
067        
068        return Map.of(
069            getName(), 
070            children
071        );
072    }
073    
074    @Override
075    public Map<String, Object> toJSONTree()
076    {
077        Map<String, Object> jsonTree = super.toJSONTree();
078        jsonTree.put("leaf", false);
079        jsonTree.put("expanded", _expanded);
080        jsonTree.put("children", _children.stream().map(Statistics::toJSONTree).toList());
081        return jsonTree;
082    }
083}