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.cms.properties.section.technical.impl;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.languages.Language;
026import org.ametys.cms.languages.LanguagesManager;
027import org.ametys.cms.properties.section.technical.AbstractTechnicalItem;
028import org.ametys.plugins.repository.AmetysObject;
029
030/**
031 * Technical section item to display language.
032 */
033public abstract class AbstractLanguageItem extends AbstractTechnicalItem implements Serviceable
034{
035    private LanguagesManager _languagesManager;
036
037    public void service(ServiceManager smanager) throws ServiceException
038    {
039        _languagesManager = (LanguagesManager) smanager.lookup(LanguagesManager.ROLE);
040    }
041    
042    public Map<String, Object> buildData(AmetysObject ametysObject)
043    {
044        Map<String, Object> resultMap = new LinkedHashMap<>();
045        
046        String langCode = _getLanguage(ametysObject);
047        if (langCode != null)
048        {
049            resultMap.put("code", langCode);
050
051            Language language = _languagesManager.getLanguage(langCode);
052            if (language != null)
053            {
054                resultMap.put("label", language.getLabel());
055                resultMap.put("icon", language.getSmallIcon());
056            }
057        }
058        
059        return resultMap;
060    }
061    
062    /**
063     * Get the language code on the Ametys object.
064     * @param ametysObject The Ametys object
065     * @return the language code or null
066     */
067    protected abstract String _getLanguage(AmetysObject ametysObject);
068    
069    @Override
070    protected String _getDefaultXSLT()
071    {
072        return "view:cms://stylesheets/properties/technical/ametys-object/language.xsl";
073    }
074}