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