001/*
002 *  Copyright 2019 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.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.odf.ProgramItem;
028import org.ametys.odf.orgunit.OrgUnit;
029import org.ametys.odf.tree.ODFTreeIndicator.IndicatorData;
030import org.ametys.plugins.contentstree.ContentsTreeHelper;
031
032/**
033 * Helper ODF contents tree adding indicators
034 */
035public class ODFContentsTreeHelper extends ContentsTreeHelper
036{
037    /** The Avalon role */
038    @SuppressWarnings("hiding")
039    public static final String ROLE = ODFContentsTreeHelper.class.getName();
040
041    /** Extension point for indicators */
042    protected ODFTreeIndicatorExtensionPoint _odfTreeIndicatorEP;
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _odfTreeIndicatorEP = (ODFTreeIndicatorExtensionPoint) smanager.lookup(ODFTreeIndicatorExtensionPoint.ROLE);
049    }
050    
051    @Override
052    protected Map<String, Object> content2Json(Content content, List<String> path)
053    {
054        Map<String, Object> infos = super.content2Json(content, path);
055        infos.putAll(getIndicatorData(content));
056        return infos;
057    }
058    
059    /**
060     * Get the indicator data to JSON format
061     * @param content the contet
062     * @return the indicator data to JSON format
063     */
064    protected Map<String, Object> getIndicatorData(Content content)
065    {
066        Map<String, Object> indicatorsData = new HashMap<>();
067        
068        for (String indicatorId : _odfTreeIndicatorEP.getExtensionsIds())
069        {
070            ODFTreeIndicator indicator = _odfTreeIndicatorEP.getExtension(indicatorId);
071            IndicatorData indicatorData = indicator.getIndicatorData(content);
072            if (indicatorData != null)
073            {
074                indicatorsData.put(indicator.getId(), _indicatorData2json(indicatorData));
075            }
076        }
077        
078        return indicatorsData;
079    }
080    
081    private Map<String, Object> _indicatorData2json(IndicatorData indicatorData)
082    {
083        Map<String, Object> data = new HashMap<>();
084        data.put("tooltip", indicatorData.tooltip());
085        data.put("cssClass", indicatorData.cssClass());
086        data.put("text", indicatorData.text());
087        
088        Map<String, Object> additionalData = indicatorData.additionalData();
089        if (additionalData != null)
090        {
091            data.putAll(additionalData);
092        }
093        return data;
094    }
095    
096    /**
097     * Get the ProgramItem code we want to display.
098     * @param <T> The type of the program element, should be a subclasse of {@link ProgramItem} and {@link Content}
099     * @param programItem The program item
100     * @return The code to display
101     */
102    public <T extends ProgramItem & Content> String getProgramItemDisplayCode(T programItem)
103    {
104        return programItem.getCode();
105    }
106    
107    @Override
108    protected boolean isContentMatching(Content content, String value)
109    {
110        boolean matchTitle = super.isContentMatching(content, value);
111        if (!matchTitle)
112        {
113            if (content instanceof ProgramItem)
114            {
115                // Try with code
116                String code =  StringUtils.stripAccents(((ProgramItem) content).getCode().toLowerCase());
117                return code.contains(value);
118            }
119            else if (content instanceof OrgUnit)
120            {
121                // Try with code
122                String code =  StringUtils.stripAccents(((OrgUnit) content).getUAICode().toLowerCase());
123                return code.contains(value);
124            }
125        }
126        
127        return matchTitle;
128    }
129}