/**
*
*/
package de.peacei.gae.foodsupplier.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import de.peacei.gae.foodsupplier.Finals;
import de.peacei.gae.foodsupplier.data.Comment;
import de.peacei.gae.foodsupplier.data.User;
import de.peacei.gae.foodsupplier.data.dao.CommentDAO;
import de.peacei.gae.foodsupplier.data.dao.MenuDAO;
import de.peacei.gae.foodsupplier.data.dao.UserDAO;
import de.peacei.gae.foodsupplier.util.CalendarUtil;
/**
* @author peacei
*
*/
@SuppressWarnings("serial")
@Singleton
public class CommentServlet extends AbstractServlet {
@Inject
private CommentDAO commentDAO;
@Inject
private MenuDAO menuDAO;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Key menuKey = null;
try {
menuKey = KeyFactory.stringToKey(req.getParameter(Finals.MENUKEY_PARAM));
} catch(Exception ex) { }
if(menuKey != null) {
String format = req.getParameter(Finals.FORMAT_PARAM);
if(format == null) format = "xml";
List<Comment> comments = commentDAO.getCommentsForMenu(menuKey);
final String callback = req.getParameter(Finals.CALLBACK_PARAM);
if(comments != null && comments.size()>0) {
this.renderingService.renderComments(comments, resp.getOutputStream(), format, callback);
setContentType(resp, format);
resp.setStatus(HttpServletResponse.SC_OK);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().append("no comments found");
}
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().append("failed");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
String menuKey = null;
try {
menuKey = req.getParameter(Finals.MENUKEY_PARAM);
} catch(Exception ex) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String name = null;
try {
name = req.getParameter(Finals.NAME_PARAM);
} catch(Exception ex) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String msg = null;
try {
msg = req.getParameter(Finals.MSG_PARAM);
} catch(Exception ex) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String email = null;
try {
email = req.getParameter(Finals.EMAIL_PARAM);
} catch(Exception ex) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String client = null;
try {
client = req.getParameter(Finals.CLIENT_PARAM);
} catch(Exception ex) {
client = Finals.CLIENT_NOT_SET;
}
if(isValuable(name) && isValuable(msg) && isValuable(email) && isValuable(client) && isValuable(menuKey)) {
final Key key = KeyFactory.stringToKey(menuKey);
if(menuDAO.isMenuKey(key)) {
final Comment comment = new Comment();
final UserDAO userDAO = new UserDAO();
User user = userDAO.getUserViaMail(email);
if(user == null) {
user = new User();
user.setEmail(email);
user.setName(name);
}
comment.setUser(user);
DateTime dateTime = new DateTime(DateTimeZone.forTimeZone(CalendarUtil.getCalendar().getTimeZone()));
comment.setTimestamp(dateTime.getMillis());
comment.setMenuKey(key);
comment.setText(msg);
comment.setClient(client);
comment.setIsRating(false);
commentDAO.saveComment(comment);
}
}
}
}