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.Collections;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.plugins.repository.AmetysObjectIterable;
027import org.ametys.plugins.repository.UnknownAmetysObjectException;
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.runtime.model.Enumerator;
030import org.ametys.web.repository.site.Site;
031import org.ametys.web.repository.site.SiteManager;
032
033/**
034 * Enumerates all sites. The code is the site name, the label is the site title.
035 */
036public class SiteEnumerator implements Enumerator<String>, org.ametys.runtime.parameter.Enumerator, Serviceable
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    public Map<String, I18nizableText> getTypedEntries() throws Exception
048    {
049        Map<String, I18nizableText> entries = new HashMap<>();
050        
051        AmetysObjectIterable<Site> sites = _siteManager.getSites();
052        
053        for (Site site : sites)
054        {
055            entries.put(site.getName(), new I18nizableText(site.getTitle()));
056        }
057        
058        return entries;
059    }
060    
061    @Override
062    public Map<Object, I18nizableText> getEntries() throws Exception
063    {
064        return (Map<Object, I18nizableText>) (Object) getTypedEntries();
065    }
066    
067    @Override
068    public I18nizableText getEntry(String value) throws Exception
069    {
070        try
071        {
072            Site site = _siteManager.getSite(value);
073            return new I18nizableText(site.getTitle());
074        }
075        catch (UnknownAmetysObjectException e)
076        {
077            // Ignore, just return null
078        }
079        
080        return null;
081    }
082    
083    @Override
084    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
085    public Map<String, Object> getConfiguration()
086    {
087        return Collections.EMPTY_MAP;
088    }
089    
090}