001/*
002 *  Copyright 2024 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.odf.tree;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.ui.ClientSideElementHelper;
027import org.ametys.core.util.JSONUtils;
028import org.ametys.plugins.contentstree.ui.TreeToolClientSideElement;
029
030/**
031 * Client side element for ODF tree tool that supports indicators
032 *
033 */
034public class ODFTreeToolClientSideElement extends TreeToolClientSideElement
035{
036    /** Extension point for indicators */
037    protected ODFTreeIndicatorExtensionPoint _odfTreeIndicatorEP;
038    /** JSON utils */
039    protected JSONUtils _jsonUtils;
040    
041    @Override
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        super.service(smanager);
045        _odfTreeIndicatorEP = (ODFTreeIndicatorExtensionPoint) smanager.lookup(ODFTreeIndicatorExtensionPoint.ROLE);
046        _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE);
047    }
048    
049    @Override
050    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
051    {
052        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
053        
054        if (scripts.size() > 0)
055        {
056            Script script = ClientSideElementHelper.cloneScript(scripts.get(0));
057            
058            scripts = new ArrayList<>();
059            
060            List<Map<String, Object>> indicators = new ArrayList<>();
061            
062            for (String indicatorId : _odfTreeIndicatorEP.getExtensionsIds())
063            {
064                ODFTreeIndicator indicator = _odfTreeIndicatorEP.getExtension(indicatorId);
065                if (indicator.isEnabled())
066                {
067                    indicators.add(_indicator2json(indicator));
068                    
069                    List<Script> indicatorScripts = indicator.getScripts(contextParameters);
070                    if (indicatorScripts.size() > 0)
071                    {
072                        // Add required scripts and/or css files for indicator
073                        Script indicatorScript = ClientSideElementHelper.cloneScript(indicatorScripts.get(0));
074                        script.getScriptFiles().addAll(indicatorScript.getScriptFiles());
075                        script.getCSSFiles().addAll(indicatorScript.getCSSFiles());
076                    }
077                }
078            }
079            
080            script.getParameters().put("indicators", _jsonUtils.convertObjectToJson(indicators));
081            
082            scripts = new ArrayList<>();
083            scripts.add(script);
084        }
085        
086        return scripts;
087    }
088    
089    private Map<String, Object> _indicator2json(ODFTreeIndicator indicator)
090    {
091        Map<String, Object> indicatorInfo = new HashMap<>();
092        indicatorInfo.put("id", indicator.getId());
093        indicatorInfo.put("label", indicator.getLabel());
094        indicatorInfo.put("description", indicator.getDescription());
095        indicatorInfo.put("iconGlyph", indicator.getIconGlyph());
096        indicatorInfo.put("matchFn", indicator.getMatchJSFunction());
097        indicatorInfo.put("applyFn", indicator.getApplyJSFunction());
098        return indicatorInfo;
099    }
100}