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            if (indicator.isEnabled())
072            {
073                IndicatorData indicatorData = indicator.getIndicatorData(content);
074                if (indicatorData != null)
075                {
076                    indicatorsData.put(indicator.getId(), _indicatorData2json(indicatorData));
077                }
078            }
079        }
080        
081        return indicatorsData;
082    }
083    
084    private Map<String, Object> _indicatorData2json(IndicatorData indicatorData)
085    {
086        Map<String, Object> data = new HashMap<>();
087        data.put("tooltip", indicatorData.tooltip());
088        data.put("cssClass", indicatorData.cssClass());
089        data.put("text", indicatorData.text());
090        
091        Map<String, Object> additionalData = indicatorData.additionalData();
092        if (additionalData != null)
093        {
094            data.putAll(additionalData);
095        }
096        return data;
097    }
098    
099    @Override
100    protected boolean isContentMatching(Content content, String value)
101    {
102        boolean matchTitle = super.isContentMatching(content, value);
103        if (!matchTitle)
104        {
105            if (content instanceof ProgramItem)
106            {
107                // Try with code
108                String code =  StringUtils.stripAccents(((ProgramItem) content).getCode().toLowerCase());
109                return code.contains(value);
110            }
111            else if (content instanceof OrgUnit)
112            {
113                // Try with code
114                String code =  StringUtils.stripAccents(((OrgUnit) content).getUAICode().toLowerCase());
115                return code.contains(value);
116            }
117        }
118        
119        return matchTitle;
120    }
121}