001/*
002 *  Copyright 2011 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.blog;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.commons.lang.StringUtils;
028
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.model.Enumerator;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033
034/**
035 * Blog site enumerator.
036 */
037public class BlogSiteEnumerator extends AbstractLogEnabled implements Serviceable, Enumerator<String>, org.ametys.runtime.parameter.Enumerator
038{
039    
040    /** The blog site type. */
041    public static final String BLOG_SITE_TYPE = "org.ametys.plugins.blog.sitetype.Blog";
042    
043    /** The site manager. */
044    protected SiteManager _siteManager;
045    
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
049    }
050    
051    @Override
052    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
053    public Map<Object, I18nizableText> getEntries() throws Exception
054    {
055        Map<Object, I18nizableText> result = new HashMap<>();
056        for (Map.Entry<String, I18nizableText> entry : getTypedEntries().entrySet())
057        {
058            result.put(entry.getKey(), entry.getValue());
059        }
060        return Collections.unmodifiableMap(result);
061    }
062    
063    public Map<String, I18nizableText> getTypedEntries() throws Exception
064    {
065        Map<String, I18nizableText> entries = new LinkedHashMap<>();
066        
067        entries.put("", new I18nizableText("plugin.blog", "PLUGINS_BLOG_ENUMERATOR_SITES_ALL"));
068        
069        for (Site site : _siteManager.getSites())
070        {
071            if (site.getType().equals(BLOG_SITE_TYPE))
072            {
073                I18nizableText label = new I18nizableText(site.getTitle() + " (" + site.getName() + ")");
074                entries.put(site.getName(), label);
075            }
076        }
077        
078        return entries;
079    }
080    
081    public I18nizableText getEntry(String value) throws Exception
082    {
083        I18nizableText entry;
084        
085        if (StringUtils.isEmpty(value))
086        {
087            entry = new I18nizableText("plugin.blog", "PLUGINS_BLOG_ENUMERATOR_SITES_ALL");
088        }
089        else
090        {
091            Site site = _siteManager.getSite(value);
092            entry = new I18nizableText(site.getTitle());
093        }
094        
095        return entry;
096    }
097    
098    @Override
099    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
100    public Map<String, Object> getConfiguration()
101    {
102        return Collections.EMPTY_MAP;
103    }
104
105}