package de.peacei.gae.foodsupplier.servlet;
import java.util.Date;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
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.Weekplan;
import de.peacei.gae.foodsupplier.data.WeekplanCache;
import de.peacei.gae.foodsupplier.data.dao.WeekplanDAO;
import de.peacei.gae.foodsupplier.parser.MensaParserService;
import de.peacei.gae.foodsupplier.util.CalendarUtil;
import de.peacei.gae.foodsupplier.util.Version;
@SuppressWarnings("serial")
@Singleton
public class PostDataServlet extends HttpServlet {
private static final Logger logger = Logger.getLogger(PostDataServlet.class.getName());
@Inject
private WeekplanDAO mensaDAO;
@Inject
private WeekplanCache weekplanCache;
@Inject
private MensaParserService mensaParserService;
protected void doPost(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
throws javax.servlet.ServletException, java.io.IOException {
StringBuilder respBuilder = new StringBuilder();
String weekNumberStr = req.getParameter("week_number");
int weekNumber = 0;
try {
weekNumber = Integer.valueOf(weekNumberStr);
}
catch(NumberFormatException ex) {}
String mensaShortName = req.getParameter("mensa_short_name");
String msg = "Mensa: " + mensaShortName;
logger.info(msg);
respBuilder.append(msg);
String content = req.getParameter("html_content");
//resp.getWriter().append(content);
logger.info(content);
// content = new String(content.getBytes("ISO-8859-1"), "UTF-8");
// logger.info(content);
boolean commitData = "yes".equals(req.getParameter("commit_data"));
if (content != null && !content.isEmpty() && 0<weekNumber && weekNumber<54 && mensaShortName != null && mensaShortName.matches("air|bhv|gw2|hsb|uni|wer")) {
try {
Mensa mensa = null;
if ("air".equals(mensaShortName)) {
mensa = Mensa.Airport;
} else if ("bhv".equals(mensaShortName)) {
mensa = Mensa.Bremerhaven;
} else if ("gw2".equals(mensaShortName)) {
mensa = Mensa.GW2;
} else if ("hsb".equals(mensaShortName)) {
mensa = Mensa.Neustadtswall;
} else if ("uni".equals(mensaShortName)) {
mensa = Mensa.Uniboulevard;
} else if ("wer".equals(mensaShortName)) {
mensa = Mensa.Werderstr;
}
Date now = new Date();
int year = CalendarUtil.getYear(now);
Weekplan weekplan = mensaParserService.readCurrentWeekplanForMensaWeekAndYear(mensa, weekNumber, year, content);
weekplan.setYear(year);
if (commitData) {
mensaDAO.deleteWeekplansByMensaWeekAndYear(mensa, weekNumber, year);
weekplanCache.removeFromCache(Finals.FORMAT_JSON, mensa.getId(), weekNumber, year);
weekplanCache.removeFromCache(Finals.FORMAT_XML, mensa.getId(), weekNumber, year);
mensaDAO.saveWeekplan(weekplan);
}
respBuilder.append("<br>data parsed");
if (commitData) {
respBuilder.append("<br>data written");
}
respBuilder.append("<br><a href=\"http://").append(Version.shortId).append(".foodspl.appspot.com/mensa?id=").
append(mensa.getId()).append("&format=xml&week=").append(CalendarUtil.getWeek(new Date())).append("\">XML-Daten</a>");
respBuilder.append("<br><a href=\"http://").append(Version.shortId).
append(".foodspl.appspot.com/admin.jsp\">Administrationsbereich</a>");
} catch (Exception e) {
respBuilder.append("<br>failed to parse and write data");
}
} else {
respBuilder.append("<br>content was empty, mensa not selected or wrong week");
}
Version.writeHTML(resp, respBuilder.toString());
}
}