/**
*
*/
package de.peacei.gae.foodsupplier.servlet;
import java.io.IOException;
import java.util.Date;
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.Mensa;
import de.peacei.gae.foodsupplier.data.Rating;
import de.peacei.gae.foodsupplier.data.User;
import de.peacei.gae.foodsupplier.data.dao.CommentDAO;
import de.peacei.gae.foodsupplier.data.dao.FoodDAO;
import de.peacei.gae.foodsupplier.data.dao.RatingDAO;
import de.peacei.gae.foodsupplier.data.dao.UserDAO;
import de.peacei.gae.foodsupplier.util.CalendarUtil;
/**
* @author peacei
*
*/
@SuppressWarnings("serial")
@Singleton
public class RatingServlet extends AbstractServlet {
@Inject
private CommentDAO commentDAO;
@Inject
private FoodDAO foodDAO;
@Inject
private RatingDAO ratingDAO;
@Inject
private UserDAO userDAO;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
String foodKey = null;
try {
foodKey = req.getParameter(Finals.FOODKEY_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;
}
int value = 0;
try {
value = Integer.valueOf(req.getParameter(Finals.VALUE_PARAM)).intValue();
} 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(email) && isValuable(client) && isValuable(foodKey)) {
final Key key = KeyFactory.stringToKey(foodKey);
if(foodDAO.isFoodKey(key)) {
User user = userDAO.getUserViaMail(email);
if(user == null) {
user = new User();
user.setEmail(email);
user.setName(name);
}
final Rating rating = new Rating();
rating.setUser(user);
rating.setFoodKey(key);
if(value==Finals.RATING_POSITIVE) {
rating.setValue(Rating.Value.positive);
} else {
rating.setValue(Rating.Value.negative);
}
DateTime dateTime = new DateTime(DateTimeZone.forTimeZone(CalendarUtil.getCalendar().getTimeZone()));
rating.setTimestamp(dateTime.getMillis());
ratingDAO.saveRating(rating);
// final Comment comment = new Comment();
// comment.setUser(user);
// comment.setMenuKey(key);
// comment.setIsRating(true);
// comment.setClient(client);
//
// String bewertungsText;
// if(value==RATING_POSITIVE) bewertungsText = "negative";
// else bewertungsText = "positive";
// final String msg = name + "gab eine " + bewertungsText
// + " Bewertung für "
// comment.setText(msg);
//
// DateTime dateTime = new DateTime(DateTimeZone.forTimeZone(CalendarUtil.getCalendar().getTimeZone()));
// comment.setTimestamp(dateTime.getMillis());
// commentDAO.saveComment(comment);
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int actualWeek = CalendarUtil.getWeek(new Date());
int actualYear = CalendarUtil.getYear(new Date());
int weekNumber = actualWeek;
try {
weekNumber = Integer.valueOf(req.getParameter(Finals.WEEK_PARAM));
} catch(NumberFormatException ex) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
} catch(Exception ex) { }
Mensa mensa;
String id = req.getParameter(Finals.MENSA_ID_PARAM);
if(id == null) mensa = Mensa.Airport;
else mensa = Mensa.getById(id);
if(mensa == null) {
resp.getOutputStream().print("no data for mensa "+id+" available");
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
}
}