package de.paulwein.notes;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import de.paulwein.notes.dao.DAOException;
import de.paulwein.notes.dao.DAOFactory;
import de.paulwein.notes.dao.NotesDAO;
import de.paulwein.notes.pojo.Note;
import de.paulwein.notes.pojo.NoteList;
/**
* NotesListsServlet
* loads all note lists of the corresponding user
* corresponding to noteslists.jsp
* @author Paul
*
*/
@SuppressWarnings("serial")
public class NoteListsServlet extends NotesAppServlet {
// load note lists and show them in jsp page
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
super.doGet(req, resp);
try {
DAOFactory df = DAOFactory.getInstance();
NotesDAO nDao = df.getDAO(Note.class, NotesDAO.class);
List<NoteList> notesLists = nDao.fetchNotesLists(user.getUserId());
req.setAttribute("list", notesLists);
RequestDispatcher requestDispatcher = req.getRequestDispatcher(NOTELISTS_SERVLET + ".jsp");
requestDispatcher.forward(req, resp);
} catch (DAOException e) {
errorOccured(resp);
} catch (ServletException e) {
errorOccured(resp);
}
}
// create or delete note list
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
super.doPost(req,resp);
String action = req.getParameter("action");
try {
DAOFactory df = DAOFactory.getInstance();
NotesDAO nDao = df.getDAO(Note.class, NotesDAO.class);
if(action.equals("create")){
String name = req.getParameter("name");
if(name != null && !name.equals(""))
nDao.createNotesList(user.getUserId(), name);
}
else if(action.equals("delete")){
String keyString = req.getParameter("key");
Key notesListKey = KeyFactory.stringToKey(keyString);
NoteList nl = new NoteList();
nl.setKey(notesListKey);
nDao.deleteNotesList(nl);
}
resp.sendRedirect(NOTELISTS_SERVLET);
} catch (DAOException e) {
errorOccured(resp);
}
}
}