001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt; 029 030import org.opencms.configuration.CmsDefaultUserSettings; 031import org.opencms.configuration.preferences.I_CmsPreference; 032import org.opencms.file.CmsObject; 033import org.opencms.gwt.shared.CmsGwtConstants; 034import org.opencms.gwt.shared.CmsUserSettingsBean; 035import org.opencms.i18n.CmsMultiMessages; 036import org.opencms.jsp.CmsJspActionElement; 037import org.opencms.main.CmsLog; 038import org.opencms.main.OpenCms; 039import org.opencms.util.CmsMacroResolver; 040import org.opencms.workplace.CmsWorkplace; 041import org.opencms.xml.content.CmsXmlContentProperty; 042import org.opencms.xml.content.CmsXmlContentPropertyHelper; 043 044import java.util.Locale; 045import java.util.Map; 046 047import javax.servlet.http.HttpServletRequest; 048import javax.servlet.http.HttpServletResponse; 049 050import org.apache.commons.logging.Log; 051 052/** 053 * Helper class to deal with loading and saving user preferences from the ADE user interface.<p> 054 */ 055public class CmsClientUserSettingConverter { 056 057 /** 058 * Subclass of the normal action element which can be created even if we are not being called from a JSP.<p> 059 */ 060 class NoJspActionElement extends CmsJspActionElement { 061 062 /** The CMS object to use. */ 063 private CmsObject m_setCms; 064 065 /** 066 * Creates a new instance.<p> 067 * 068 * @param cms the CMS context to use 069 * @param req the current request 070 * @param res the current response 071 */ 072 public NoJspActionElement(CmsObject cms, HttpServletRequest req, HttpServletResponse res) { 073 074 super(null, req, res); 075 m_setCms = cms; 076 077 } 078 079 /** 080 * @see org.opencms.jsp.CmsJspBean#getCmsObject() 081 */ 082 @Override 083 public CmsObject getCmsObject() { 084 085 return m_setCms; 086 } 087 088 /** 089 * @see org.opencms.jsp.CmsJspBean#handleMissingFlexController() 090 */ 091 @Override 092 protected void handleMissingFlexController() { 093 094 // ignore 095 } 096 097 } 098 099 /** Logger instance for this class. */ 100 private static final Log LOG = CmsLog.getLog(CmsClientUserSettingConverter.class); 101 102 /** The CMS context to use. */ 103 private CmsObject m_cms; 104 105 /** The current preferences. */ 106 private CmsDefaultUserSettings m_currentPreferences; 107 108 /** The macro resolver used for macros in preference property definitions. */ 109 private CmsMacroResolver m_macroResolver = new CmsMacroResolver(); 110 111 /** The current request. */ 112 private HttpServletRequest m_request; 113 114 /** 115 * Creates a new instance.<p> 116 * 117 * @param cms the current CMS context 118 * @param request the current request 119 * @param response the current response 120 */ 121 public CmsClientUserSettingConverter(CmsObject cms, HttpServletRequest request, HttpServletResponse response) { 122 123 m_cms = cms; 124 m_request = request; 125 m_currentPreferences = new CmsDefaultUserSettings(); 126 m_currentPreferences.init(cms.getRequestContext().getCurrentUser()); 127 Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms); 128 CmsMultiMessages messages = new CmsMultiMessages(locale); 129 messages.addMessages(OpenCms.getWorkplaceManager().getMessages(locale)); 130 messages.addMessages(org.opencms.workplace.commons.Messages.get().getBundle(locale)); 131 m_macroResolver.setMessages(messages); 132 133 } 134 135 /** 136 * Loads the current user's preferences into a CmsUserSettingsBean.<p> 137 * 138 * @return the bean representing the current user's preferences 139 */ 140 public CmsUserSettingsBean loadSettings() { 141 142 CmsUserSettingsBean result = new CmsUserSettingsBean(); 143 CmsDefaultUserSettings currentSettings = new CmsDefaultUserSettings(); 144 currentSettings.init(m_currentPreferences.getUser()); 145 for (I_CmsPreference pref : OpenCms.getWorkplaceManager().getDefaultUserSettings().getPreferences().values()) { 146 String tab = pref.getTab(); 147 if (CmsGwtConstants.TAB_HIDDEN.equals(tab) || pref.isDisabled(m_cms)) { 148 continue; 149 } 150 CmsXmlContentProperty prop2 = pref.getPropertyDefinition(m_cms); 151 String value = pref.getValue(currentSettings); 152 CmsXmlContentProperty resolvedProp = CmsXmlContentPropertyHelper.resolveMacrosInProperty( 153 prop2.withDefaultWidget("string"), 154 m_macroResolver); 155 result.addSetting(value, resolvedProp, CmsGwtConstants.TAB_BASIC.equals(tab)); 156 } 157 return result; 158 } 159 160 /** 161 * Saves the given user preference values.<p> 162 * 163 * @param settings the user preference values to save 164 * 165 * @throws Exception if something goes wrong 166 */ 167 public void saveSettings(Map<String, String> settings) throws Exception { 168 169 for (Map.Entry<String, String> entry : settings.entrySet()) { 170 String key = entry.getKey(); 171 String value = entry.getValue(); 172 saveSetting(key, value); 173 } 174 m_currentPreferences.save(m_cms); 175 CmsWorkplace.updateUserPreferences(m_cms, m_request); 176 } 177 178 /** 179 * Saves an individual user preference value.<p> 180 * 181 * @param key the key of the user preference 182 * @param value the value of the user preference 183 * 184 * @throws Exception if something goes wrong 185 */ 186 private void saveSetting(String key, String value) throws Exception { 187 188 Map<String, I_CmsPreference> prefs = OpenCms.getWorkplaceManager().getDefaultUserSettings().getPreferences(); 189 if (prefs.containsKey(key)) { 190 prefs.get(key).setValue(m_currentPreferences, value); 191 } else { 192 LOG.error("Can't save user setting '" + key + "'"); 193 } 194 } 195 196}