package +zoccolo+.web.controller;

import +zoccolo+.web.controller.GenericComponent;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

public class GenericController extends GenericComponent {
		
	@ExceptionHandler(Exception.class)
	  public ModelAndView handleError(HttpServletRequest req, Exception exception) {
	    logger.error("Request: " + req.getRequestURL() + " raised " + exception);

	    exception.printStackTrace();
		ModelAndView mav = new ModelAndView("page.error");
		
	    return mav;
	  }
	
	
	protected String localizeViewName(String viewBaseName, HttpServletRequest request) {

		String ret = viewBaseName;
		String lang = getSessionAttribute("lang", request);

		if ("en".equals(lang))
		ret += ".en";
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	protected <T> T getSessionAttribute(String name, HttpServletRequest request) {
		try {
			T toReturn = (T) request.getSession().getAttribute(name);
			return toReturn == null ? null : toReturn;
		} catch (ClassCastException e) {
			return null;
		}
	}
	
}