001/*
002 *  Copyright 2015 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.web.site;
017
018import java.util.HashMap;
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.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.UnknownAmetysObjectException;
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 * Enumerates all sites. The code is the site name, the label is the site title.
034 */
035public class SiteEnumerator implements Enumerator, Serviceable
036{
037    
038    /** The site manager. */
039    private SiteManager _siteManager;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
045    }
046    
047    @Override
048    public Map<Object, I18nizableText> getEntries() throws Exception
049    {
050        Map<Object, I18nizableText> entries = new HashMap<>();
051        
052        AmetysObjectIterable<Site> sites = _siteManager.getSites();
053        
054        for (Site site : sites)
055        {
056            entries.put(site.getName(), new I18nizableText(site.getTitle()));
057        }
058        
059        return entries;
060    }
061    
062    @Override
063    public I18nizableText getEntry(String value) throws Exception
064    {
065        try
066        {
067            Site site = _siteManager.getSite(value);
068            return new I18nizableText(site.getTitle());
069        }
070        catch (UnknownAmetysObjectException e)
071        {
072            // Ignore, just return null
073        }
074        
075        return null;
076    }
077    
078}