001/*
002 *  Copyright 2012 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.auto;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.plugins.newsletter.category.Category;
027import org.ametys.plugins.newsletter.category.CategoryProvider;
028import org.ametys.plugins.newsletter.category.CategoryProviderExtensionPoint;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.core.ui.Callable;
031import org.ametys.core.ui.StaticClientSideElement;
032
033/**
034 * Automatic newsletter client side element: indicate which of the selected categories are in "automatic" mode.
035 */
036public class AutomaticNewsletterClientSideElement extends StaticClientSideElement
037{
038    
039    /** The automatic newsletter extension point. */
040    protected AutomaticNewsletterExtensionPoint _autoNewsletterEP;
041    
042    /** The category provider extension point. */
043    protected CategoryProviderExtensionPoint _categoryProviderEP;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        super.service(serviceManager);
049        _autoNewsletterEP = (AutomaticNewsletterExtensionPoint) serviceManager.lookup(AutomaticNewsletterExtensionPoint.ROLE);
050        _categoryProviderEP = (CategoryProviderExtensionPoint) serviceManager.lookup(CategoryProviderExtensionPoint.ROLE);
051    }
052    
053    /**
054     * Get the parameters of the automatic newletter
055     * @param parameters the parameters used to get the automatic newletters parameters
056     * @return the automatic newletter parameters
057     */
058    @Callable
059    public Map<String, I18nizableText> getCurrentParameters(Map<String, Object> parameters)
060    {
061        Map<String, I18nizableText> params = new HashMap<>();
062        
063        @SuppressWarnings("unchecked")
064        List<String> categoryIds = (List<String>) parameters.get("categoryIds");
065        
066        int autoNewsletterCount = 0;
067        
068        for (String categoryId : categoryIds)
069        {
070            Category category = _categoryProviderEP.getCategory(categoryId);
071            CategoryProvider provider = _categoryProviderEP.getCategoryProvider(categoryId);
072            Collection<String> autoIds = provider.getAutomaticIds(categoryId);
073            
074            if (!autoIds.isEmpty())
075            {
076                params.put("auto-newsletter-" + autoNewsletterCount + "-id", new I18nizableText(categoryId));
077                params.put("auto-newsletter-" + autoNewsletterCount + "-title", category.getTitle());
078                autoNewsletterCount++;
079            }
080        }
081        
082        params.put("auto-newsletter-count", new I18nizableText(Integer.toString(autoNewsletterCount)));
083        
084        int availableAutoNewsletterCount = _autoNewsletterEP.getExtensionsIds().size();
085        
086        params.put("available-auto-newsletter-count", new I18nizableText(Integer.toString(availableAutoNewsletterCount)));
087        
088        return params;
089    }
090    
091}