package com.nevernote.editor;
import java.io.File;
import java.io.IOException;
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;
/**
* Servlet implementation class DeleteServlet
*/
public class DeleteServlet extends HttpServlet {
private static final long serialVersionUID = -4438478107774943416L;
/**
* @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);
//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 noteName = request.getParameter("delete");
//Find the relative path of the notesRepo folder to append to note name for saving
String relativeWebPath = "WEB-INF/notesRepo";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
File file = new File(absoluteDiskPath + "/" + u.getId() + "/" + noteName);
boolean success = file.delete();
if (success) {
//Update database with the note deletion
Notes theNote = notesService.findOne(noteName);
if (theNote != null) {
theNote.removeUsers(u);
notesService.delete(theNote);
}
}
String url = "/ClientDashServlet";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
return;
}
}