001/*
002 *  Copyright 2014 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.odf.catalog;
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.runtime.i18n.I18nizableText;
026import org.ametys.runtime.parameter.Enumerator;
027
028/**
029 * {@link Enumerator} for the ODF Catalog
030 */
031public class CatalogEnumerator implements Enumerator, Serviceable
032{
033    /** Avalon Role */
034    public static final String ROLE = CatalogEnumerator.class.getName();
035    
036    private CatalogsManager _catalogsManager;
037    
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _catalogsManager = (CatalogsManager) manager.lookup(CatalogsManager.ROLE);
042    }
043    
044    @Override
045    public Map<Object, I18nizableText> getEntries()
046    {
047        Map<Object, I18nizableText> result = new HashMap<>();
048        
049        for (Catalog catalog : _catalogsManager.getCatalogs())
050        {
051            result.put(catalog.getName(), new I18nizableText(catalog.getTitle()));
052        }
053        
054        return result;
055    }
056
057    @Override
058    public I18nizableText getEntry(String value) throws Exception
059    {
060        Catalog catalog = _catalogsManager.getCatalog(value);
061        
062        if (catalog != null)
063        {
064            return new I18nizableText(catalog.getTitle());
065        }
066        
067        return null;
068    }
069}