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.model.Enumerator;
027
028/**
029 * {@link Enumerator} for the ODF Catalog
030 */
031public class CatalogEnumerator implements Enumerator<String>, 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<String, I18nizableText> getEntries() throws Exception
046    {
047        Map<String, 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
058    @Override
059    public I18nizableText getEntry(String value) throws Exception
060    {
061        Catalog catalog = _catalogsManager.getCatalog(value);
062        
063        if (catalog != null)
064        {
065            return new I18nizableText(catalog.getTitle());
066        }
067        
068        return null;
069    }
070}