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.Preferences;
import com.nevernote.domain.Users;
import com.nevernote.service.PreferencesService;
/**
* Servlet implementation class LoginServlet
*/
public class PreferencesServlet 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);
//Pulls user session and confirms they are logged in
Users u = (Users) session.getAttribute("userSession");
if(u == null){
response.sendRedirect("Login.jsp");
}
if (!u.getEnabled()) {
response.sendRedirect("Login.jsp");
}
String sort = request.getParameter("sort");
String colors = request.getParameter("color");
if (sort != null && colors != null) {
Preferences pref = preferencesService.findOne(u.getId());
pref.setColors(colors);
pref.setSort(sort);
preferencesService.save(pref);
}
response.sendRedirect("ClientDashServlet");
}
}