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.core.userpref; 017 018import java.util.Collections; 019import java.util.HashMap; 020import java.util.LinkedHashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.context.Context; 025import org.apache.avalon.framework.context.ContextException; 026import org.apache.avalon.framework.context.Contextualizable; 027import org.apache.avalon.framework.logger.AbstractLogEnabled; 028import org.apache.avalon.framework.service.ServiceException; 029import org.apache.avalon.framework.service.ServiceManager; 030import org.apache.avalon.framework.service.Serviceable; 031 032import org.ametys.runtime.i18n.I18nizableText; 033import org.ametys.runtime.model.Enumerator; 034 035/** 036 * Enumerates user preferences. 037 */ 038public class UserPreferencesEnumerator extends AbstractLogEnabled implements Enumerator<String>, org.ametys.runtime.parameter.Enumerator, Contextualizable, Serviceable 039{ 040 041 /** The user preferences extension point. */ 042 protected UserPreferencesExtensionPoint _userPrefEP; 043 044 /** The avalon context */ 045 protected Context _context; 046 047 @Override 048 public void contextualize(Context context) throws ContextException 049 { 050 _context = context; 051 } 052 053 public void service(ServiceManager serviceManager) throws ServiceException 054 { 055 _userPrefEP = (UserPreferencesExtensionPoint) serviceManager.lookup(UserPreferencesExtensionPoint.ROLE); 056 } 057 058 public Map<String, I18nizableText> getTypedEntries() throws Exception 059 { 060 Map<String, String> contextVars = getContextVars(); 061 062 Map<String, I18nizableText> entries = new LinkedHashMap<>(); 063 for (List<UserPreference> prefs : _userPrefEP.getCategorizedPreferences(contextVars).values()) 064 { 065 for (UserPreference pref : prefs) 066 { 067 entries.put(pref.getId(), pref.getLabel()); 068 } 069 } 070 return entries; 071 } 072 073 public I18nizableText getEntry(String value) throws Exception 074 { 075 Map<String, String> contextVars = getContextVars(); 076 077 return _userPrefEP.getUserPreference(contextVars, value).getLabel(); 078 } 079 080 /** 081 * Get the preferences context variables. 082 * @return the preferences context as a Map. 083 */ 084 protected Map<String, String> getContextVars() 085 { 086 return Collections.emptyMap(); 087 } 088 089 // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed 090 public Map<Object, I18nizableText> getEntries() throws Exception 091 { 092 Map<Object, I18nizableText> result = new HashMap<>(); 093 for (Map.Entry<String, I18nizableText> entry : getTypedEntries().entrySet()) 094 { 095 result.put(entry.getKey(), entry.getValue()); 096 } 097 return Collections.unmodifiableMap(result); 098 } 099 100 @Override 101 // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed 102 public Map<String, Object> getConfiguration() 103 { 104 return Collections.EMPTY_MAP; 105 } 106 107}