protected void create(HttpServletRequest request,
HttpServletResponse response, String mime) throws IOException,
ServletException
{
String userId = (String)request.getAttribute(UsersController.IDENTIFIER_ATTR);
User user = userId!=null ? _userDao.get(Long.valueOf(userId)) : null;
if(user==null)
{
response.sendError(404);
return;
}
String title = request.getParameter(Constants.TITLE);
String content = request.getParameter(Constants.CONTENT);
boolean created = false;
Todo todo = null;
if(title!=null && title.length()>0)
{
todo = new Todo();
todo.setTitle(title);
todo.setContent(content);
todo.setUser(user);
created = _todoDao.create(todo);
}
if(Constants.XML.equals(mime))
{
writeXML(created ? todo : Feedback.COULD_NOT_CREATE_TODO, request, response);
}
else if(Constants.JSON.equals(mime))
{
writeJSON(created ? todo : Feedback.COULD_NOT_CREATE_TODO, request, response);
}
else
{
if(created)
{
/*request.setAttribute(Constants.MSG, Feedback.TODO_CREATED.getMsg());
request.setAttribute(Constants.USER, user);
response.setContentType(Constants.TEXT_HTML);
getWebContext().getJSPDispatcher().dispatch("users/id.jsp",
request, response);*/
response.sendRedirect(request.getContextPath() + "/users/" + user.getId() + "/todos");
}
else
{
request.setAttribute(Constants.MSG, Feedback.COULD_NOT_CREATE_TODO.getMsg());
request.setAttribute(Constants.ACTION, Constants.ACTION_CREATE);