package com.nevernote.preferences;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.nevernote.domain.Users;
import com.nevernote.service.PreferencesService;
import com.nevernote.service.UsersService;
/**
* Servlet implementation class LoginServlet
*/
public class DisplayPreferencesServlet extends HttpServlet {
private static final long serialVersionUID = 8379761632383399763L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
HttpSession session = request.getSession();
session.setMaxInactiveInterval(-1);
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context.xml");
ctx.refresh();
//Keeping track of user session and notes
PreferencesService preferencesService = ctx.getBean("preferencesService", PreferencesService.class);
UsersService usersService = ctx.getBean("usersService", UsersService.class);
//Pulls user session and confirms they are logged in
Users u = (Users) session.getAttribute("userSession");
Users userFromDB = usersService.findById(u.getId());
if(userFromDB == null){
response.sendRedirect("Login.jsp");
}
if (!userFromDB.getEnabled()) {
response.sendRedirect("Login.jsp");
}
String sort = preferencesService.findOne(u.getId()).getSort();
String colors = preferencesService.findOne(u.getId()).getColors();
session.setAttribute("sort", sort);
session.setAttribute("colors", colors);
response.sendRedirect("Preferences.jsp");
}
}