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.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.parameter.Enumerator;
029import org.ametys.web.repository.site.Site;
030import org.ametys.web.repository.site.SiteManager;
031
032/**
033 * Blog site enumerator.
034 */
035public class BlogSiteEnumerator extends AbstractLogEnabled implements Serviceable, Enumerator
036{
037    
038    /** The blog site type. */
039    public static final String BLOG_SITE_TYPE = "org.ametys.plugins.blog.sitetype.Blog";
040    
041    /** The site manager. */
042    protected SiteManager _siteManager;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
048    }
049    
050    @Override
051    public Map<Object, I18nizableText> getEntries() throws Exception
052    {
053        Map<Object, I18nizableText> entries = new LinkedHashMap<>();
054        
055        entries.put("", new I18nizableText("plugin.blog", "PLUGINS_BLOG_ENUMERATOR_SITES_ALL"));
056        
057        for (Site site : _siteManager.getSites())
058        {
059            if (site.getType().equals(BLOG_SITE_TYPE))
060            {
061                I18nizableText label = new I18nizableText(site.getTitle() + " (" + site.getName() + ")");
062                entries.put(site.getName(), label);
063            }
064        }
065        
066        return entries;
067    }
068    
069    @Override
070    public I18nizableText getEntry(String value) throws Exception
071    {
072        I18nizableText entry;
073        
074        if (StringUtils.isEmpty(value))
075        {
076            entry = new I18nizableText("plugin.blog", "PLUGINS_BLOG_ENUMERATOR_SITES_ALL");
077        }
078        else
079        {
080            Site site = _siteManager.getSite(value);
081            entry = new I18nizableText(site.getTitle());
082        }
083        
084        return entry;
085    }
086
087}