/**
 * 
 */
package cz.hsrs.servlet.lang;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cz.hsrs.servlet.provider.DBServlet;
import cz.hsrs.servlet.security.JSPHelper;
import cz.hsrs.servlet.security.LoginUser;

/**
 * Servlet to change languages on pages
 * @author MiKe
 *
 */
public class ChangeLangServlet extends DBServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		String lang = req.getParameter("lang");
		String coming =  req.getParameter("coming");

		Cookie[] cookies = req.getCookies();
		for (Cookie cookie : cookies) {
			if (cookie.getName().equalsIgnoreCase("language")) {
				cookie.setValue(lang);
				cookie.setPath("/");
				resp.addCookie(cookie);
			}
		}	
	
		LoginUser user = (LoginUser) req.getSession().getAttribute(JSPHelper.USERATTRIBUTE);
		user.setUserLanguage(lang);
		JSPHelper.redirect(resp, req.getContextPath() + coming);
	}	
}
