/*
* Copyright 2009-2010 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.mvc.controllers;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.internna.iwebmvc.model.User;
import org.internna.iwebmvc.security.UserManager;
import org.internna.iwebmvc.spring.i18n.ContextLocaleResolver;
import org.springframework.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.internna.iwebmvc.utils.StringUtils.hasText;
/**
* Changes current user preferences like theme or language.
*
* @author Jose Noheda
* @since 1.0
*
*/
@Controller
@RequestMapping("/preferences.iwebmvc")
public class UserPreferencesController {
private UserManager userManager;
private ContextLocaleResolver localeResolver;
@Required public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
@Required public void setLocaleResolver(ContextLocaleResolver localeResolver) {
this.localeResolver = localeResolver;
}
@RequestMapping(method = RequestMethod.GET)
public String change(@RequestParam(value = "locale", required = false) Locale locale, @RequestParam(value = "theme", required = false) String theme, HttpServletRequest request, HttpServletResponse response) {
User user = userManager.getActiveUser();
if (locale != null) user.setLocale(locale);
if (hasText(theme)) user.setTheme(theme);
WebUtils.setSessionAttribute(request, UserManager.SESSION_USER, user);
localeResolver.setUserLocale(request, response, user);
return "redirect:/index.iwebmvc";
}
}