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.model.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<String>
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    public void service(ServiceManager manager) throws ServiceException
045    {
046        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
047    }
048    
049    public Map<String, I18nizableText> getTypedEntries() throws Exception
050    {
051        Map<String, I18nizableText> entries = new LinkedHashMap<>();
052        
053        entries.put("", new I18nizableText("plugin.blog", "PLUGINS_BLOG_ENUMERATOR_SITES_ALL"));
054        
055        for (Site site : _siteManager.getSites())
056        {
057            if (site.getType().equals(BLOG_SITE_TYPE))
058            {
059                I18nizableText label = new I18nizableText(site.getTitle() + " (" + site.getName() + ")");
060                entries.put(site.getName(), label);
061            }
062        }
063        
064        return entries;
065    }
066    
067    public I18nizableText getEntry(String value) throws Exception
068    {
069        I18nizableText entry;
070        
071        if (StringUtils.isEmpty(value))
072        {
073            entry = new I18nizableText("plugin.blog", "PLUGINS_BLOG_ENUMERATOR_SITES_ALL");
074        }
075        else
076        {
077            Site site = _siteManager.getSite(value);
078            entry = new I18nizableText(site.getTitle());
079        }
080        
081        return entry;
082    }
083}