package com.nevernote.editor;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.RequestDispatcher;
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.Users;
import com.nevernote.service.NotesService;
import com.nevernote.service.UsersService;
/**
* Servlet implementation class SaveServlet
*/
public class SaveServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4438478107774943416L;
/**
* @see HttpServlet#HttpServlet()
*/
/**
* @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();
//Keeping track of user session and notes
NotesService notesService = ctx.getBean("notesService", NotesService.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");
}
//Retrieve the not name and also what the user entered into the note
String wholeNote = request.getParameter("wholeNote");
String noteName = request.getParameter("noteName");
//Find the relative path of the notesRepo folder to append to note name for saving
//Under windows running in Eclipse that location is:
//C:\eclipse workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\NeverNote\WEB-INF\notesRepo\
String relativeWebPath = "WEB-INF/notesRepo/" + userFromDB.getId();
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
File file = new File(absoluteDiskPath + "/" + noteName);
//Save the note
FileWriter writer = new FileWriter(file);
writer.write(wholeNote);
writer.flush();
writer.close();
//Update database to add a new note
Notes theNote = new Notes(noteName);
if (notesService.findOne(theNote.getFileName()) == null) {
Set<Users> noteUsers = new HashSet<Users>();
noteUsers.add(u);
theNote.setUsers(noteUsers);
notesService.save(theNote);
userFromDB.addNote(theNote);
Users savedUser = usersService.save(userFromDB.getId(), userFromDB);
session.setAttribute("userSession", savedUser);
}
String url = "/ClientDashServlet";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
return;
}
}