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