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.userpref;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.logger.AbstractLogEnabled;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029import org.apache.commons.lang.StringUtils;
030import org.apache.commons.lang.mutable.MutableInt;
031
032import org.ametys.plugins.newsletter.category.Category;
033import org.ametys.plugins.newsletter.category.CategoryProvider;
034import org.ametys.plugins.newsletter.category.CategoryProviderExtensionPoint;
035import org.ametys.core.userpref.UserPreference;
036import org.ametys.core.userpref.UserPreferenceProvider;
037import org.ametys.runtime.i18n.I18nizableText;
038import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
039import org.ametys.web.userpref.FOUserPreferencesConstants;
040
041/**
042 * Provides all newsletter categories as boolean user preferences.
043 * Used to build a general subscription service, with one checkbox by category.
044 */
045public class NewsletterUserPreferenceProvider extends AbstractLogEnabled implements UserPreferenceProvider, Serviceable
046{
047    
048    /** The newsletter pref group. */
049    protected static final I18nizableText _NEWSLETTER_PREF_GROUP = new I18nizableText("plugin.newsletter", "PLUGINS_NEWSLETTER_USER_PREFS_GROUP");
050    
051    /** The newsletter preferences storage component role. */
052    protected static final String _NEWSLETTER_PREF_STORAGE_COMPONENT = "org.ametys.plugins.newsletter.userpref.NewsletterUserPreferencesStorage";
053    
054    /** The category provider extension point. */
055    protected CategoryProviderExtensionPoint _categoryEP;
056    
057    @Override
058    public void service(ServiceManager serviceManager) throws ServiceException
059    {
060        _categoryEP = (CategoryProviderExtensionPoint) serviceManager.lookup(CategoryProviderExtensionPoint.ROLE);
061    }
062    
063    @Override
064    public Collection<UserPreference> getPreferences(Map<String, String> contextVars)
065    {
066        Set<UserPreference> prefs = new HashSet<>();
067        
068        String siteName = contextVars.get(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME);
069        String sitemapName = contextVars.get(FOUserPreferencesConstants.CONTEXT_VAR_LANGUAGE);
070        
071        for (String categoryProviderId : _categoryEP.getExtensionsIds())
072        {
073            CategoryProvider categoryProvider = _categoryEP.getExtension(categoryProviderId);
074            
075            if (StringUtils.isNotEmpty(siteName) && StringUtils.isNotEmpty(sitemapName))
076            {
077                MutableInt index = new MutableInt(1);
078                List<Category> categories = categoryProvider.getCategories(siteName, sitemapName);
079                
080                fillUserPreferences(prefs, categories, categoryProvider, index);
081            }
082        }
083        
084        return prefs;
085    }
086    
087    /**
088     * Create a {@link UserPreference} per newsletter category, recursively.
089     * @param prefs the user preferences Set to fill.
090     * @param categories the categories to work on.
091     * @param categoryProvider the category provider.
092     * @param index the current user preference index, to increase while processing categories.
093     */
094    protected void fillUserPreferences(Set<UserPreference> prefs, List<Category> categories, CategoryProvider categoryProvider, MutableInt index)
095    {
096        for (Category category : categories)
097        {
098            // Create the user preference object, add it to the set and increment the preferences index.
099            prefs.add(getCategoryUserPref(category, index.intValue()));
100            index.increment();
101            
102            // Process the child categories.
103            List<Category> subCategories = categoryProvider.getCategories(category.getId());
104            fillUserPreferences(prefs, subCategories, categoryProvider, index);
105        }
106    }
107
108    /**
109     * Get a boolean {@link UserPreference} representing a category.
110     * @param category the category.
111     * @param index the category index in the extension point.
112     * @return a boolean {@link UserPreference} representing the category.
113     */
114    protected UserPreference getCategoryUserPref(Category category, int index)
115    {
116        UserPreference pref = new UserPreference();
117        
118        Map<String, I18nizableText> params = new HashMap<>();
119        params.put("category", category.getTitle());
120        I18nizableText label = new I18nizableText("plugin.newsletter", "PLUGINS_NEWSLETTER_USER_PREFS_LABEL", params);
121        
122        pref.setId(category.getId());
123        pref.setLabel(label);
124        pref.setDescription(category.getDescription());
125        pref.setType(ParameterType.BOOLEAN);
126        pref.setDisplayGroup(_NEWSLETTER_PREF_GROUP);
127        pref.setOrder(index);
128        pref.setManagerRole(_NEWSLETTER_PREF_STORAGE_COMPONENT);
129        pref.setPluginName("newsletter");
130        
131        return pref;
132    }
133    
134}