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