package com.nevernote.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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.Notes;
import com.nevernote.domain.Preferences;
import com.nevernote.domain.Users;
import com.nevernote.enums.ColorScheme;
import com.nevernote.service.ClientService;
import com.nevernote.service.PreferencesService;
/*
* What sort of requests should this Servlet receive?
* 1) Post-Login / Default Request
* Should Receive:
* - UserID / UserBean
* Will Need to Do:
* - Retrieve full User Bean if only ID is passed
* - Retrieve Preferences for User
* - Retrieve Collection of Notes for User
* - Use Sorting Algo from Preferences to Sort Notes and package in Collection to be passed to jsp
*
*
* 2) Note Delete Request
* ShouldReceive:
* - UserBean (at this point)
* - Note name or Bean
* Will Need to Do:
* - Delete Note from DB
* - Either call "Default" Request, or perform same actions
*
* 3) Change of Preferences?
* - Seperate page or allow Dynamic option? (e.g. DropDown of color scheme on main jsp?)
*/
/**
* Servlet implementation class ClientDashServlet
*/
public class ClientDashServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ClientDashServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setMaxInactiveInterval(-1);
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context.xml");
ctx.refresh();
PreferencesService prefService = ctx.getBean("preferencesService", PreferencesService.class);
ClientService cs = ctx.getBean("clientService", ClientService.class);
Users user = (Users) session.getAttribute("userSession");
if (user == null){
response.sendRedirect("Login.jsp");
}
if (!user.getEnabled()) {
response.sendRedirect("Login.jsp");
}
//Test that we got the user
System.out.println("********Users object passed from Login**************");
System.out.println( user.getId() );
System.out.println("********************");
// Obtain Collection of all Nodes (Folders + Notes )
List<Notes> userNotes = new ArrayList<Notes>();
userNotes = cs.findAllForUser(user.getId());
if (userNotes == null || !userNotes.isEmpty()) {
//Test Notes results on console
for (Notes note : userNotes) {
System.out.println("Here is a file name: " + note.getFileName());
}
String sortMethod = prefService.findOne(user.getId()).getSort();
if (sortMethod.equals("ALPHA_ASCENDING")) {
Collections.sort(userNotes);
} else if (sortMethod.equals("ALPHA_DESCENDING")) {
Collections.sort(userNotes, Collections.reverseOrder());
}
session.setAttribute("userNotes", userNotes);
} else {
session.setAttribute("userNotes", new ArrayList<Notes>());
}
System.out.println("No More notes for this user");
Preferences userPref = prefService.findOne(user.getId());
session.setAttribute("fontColor", ColorScheme.valueOf(userPref.getColors()).getFontColor());
session.setAttribute("backColor", ColorScheme.valueOf(userPref.getColors()).getBackColor());
response.sendRedirect("ClientDash.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}