001/*
002 *  Copyright 2010 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.newsletter.category;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.List;
021
022import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
023
024/**
025 * This class is in charge to load and initialize newsletter category providers.
026 */
027public class CategoryProviderExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<CategoryProvider>
028{
029    /** Avalon Role */
030    public static final String ROLE = CategoryProviderExtensionPoint.class.getName();
031    
032    /**
033     * Get all the categories for a given siteName and language.
034     * @param siteName the site name.
035     * @param language the language.
036     * @return a Collection containing all the categories.
037     */
038    public Collection<Category> getCategories(String siteName, String language)
039    {
040        List<Category> categories = new ArrayList<>();
041        
042        for (String id : getExtensionsIds())
043        {
044            CategoryProvider provider = getExtension(id);
045            
046            categories.addAll(provider.getCategories(siteName, language));
047        }
048        
049        return categories;
050    }
051    
052    
053    /**
054     * Get a category by its ID.
055     * @param categoryId the category ID.
056     * @return the Category or null if not found.
057     */
058    public Category getCategory(String categoryId)
059    {
060        for (String id : getExtensionsIds())
061        {
062            CategoryProvider provider = getExtension(id);
063            if (provider.hasCategory(categoryId))
064            {
065                return provider.getCategory(categoryId);
066            }
067        }
068        
069        return null;
070    }
071    
072    /**
073     * Tests if a category exists.
074     * @param categoryId the category ID.
075     * @return true if the category exists, false otherwise..
076     */
077    public boolean hasCategory(String categoryId)
078    {
079        for (String id : getExtensionsIds())
080        {
081            CategoryProvider provider = getExtension(id);
082            if (provider.hasCategory(categoryId))
083            {
084                return true;
085            }
086        }
087        
088        return false;
089    }
090    
091    /**
092     * Get a category's provider.
093     * @param categoryId the category ID.
094     * @return the CategoryProvider or null if not found.
095     */
096    public CategoryProvider getCategoryProvider(String categoryId)
097    {
098        for (String id : getExtensionsIds())
099        {
100            CategoryProvider provider = getExtension(id);
101            if (provider.hasCategory(categoryId))
102            {
103                return provider;
104            }
105        }
106        
107        return null;
108    }
109    
110}