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.plugins.glossary.theme;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.cms.tag.AbstractTagsDAO;
029import org.ametys.cms.tag.Tag;
030import org.ametys.cms.tag.TagProvider;
031import org.ametys.cms.tag.jcr.AbstractJCRTagsDAO;
032import org.ametys.runtime.i18n.I18nizableText;
033
034/**
035 * DAO for manipulating themes
036 */
037public class ThemesDAO extends AbstractTagsDAO
038{
039    /** The avalon role */
040    public static final String ROLE = ThemesDAO.class.getName();
041
042    /** The JCR Themes DAO */
043    protected JCRThemesDAO _jcrThemesDAO;
044    
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _jcrThemesDAO = (JCRThemesDAO) manager.lookup(JCRThemesDAO.ROLE);
050    }
051    
052    @Override
053    public String getTagProviderEPRole()
054    {
055        return ThemeProviderExtensionPoint.ROLE;
056    }
057    
058    @Override
059    protected AbstractJCRTagsDAO _getTagJCRDAO()
060    {
061        return _jcrThemesDAO;
062    }
063
064    @Override
065    protected List<TagProvider< ? extends Tag>> getCustomTagProvider()
066    {
067        List<TagProvider<? extends Tag>> providers = new ArrayList<>();
068        providers.add(_tagProviderExtPt.getExtension(JCRThemeProvider.class.getName()));
069        
070        return providers;
071    }
072    
073    /**
074     * Get theme's title from its name
075     * @param themeName the theme name
076     * @param siteName the site's name
077     * @param language the site's language
078     * @return the title of the theme. Null if the theme doesn't exist
079     */
080    public I18nizableText getThemeTitle(String themeName, String siteName, String language)
081    {
082        Map<String, Object> contextualParameters = new HashMap<>();
083        contextualParameters.put("language", language);
084        contextualParameters.put("siteName", siteName);
085        if (themeExists(themeName, siteName, language))
086        {
087            Tag tag = getTag(themeName, contextualParameters);
088            return tag.getTitle();
089        }
090        else
091        {
092            getLogger().warn("Can't find theme with name " + themeName + " for site " + siteName + " and language " + language);
093        }
094            
095        return null;
096    }
097    
098    /**
099     * Verify the existence of a theme
100     * @param themeName the id of the theme to verify
101     * @param siteName the site's name
102     * @param language the site's language
103     * @return true if the theme exists, false otherwise
104     */
105    public boolean themeExists(String themeName, String siteName, String language)
106    {
107        if (StringUtils.isBlank(themeName))
108        {
109            return false;
110        }
111        Map<String, Object> contextualParameters = new HashMap<>();
112        contextualParameters.put("language", language);
113        contextualParameters.put("siteName", siteName);
114        List<String> checkTags = checkTags(List.of(themeName), false, Collections.EMPTY_MAP, contextualParameters);
115        return !checkTags.isEmpty();
116    }
117}