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);