package de.peacei.gae.foodsupplier.servlet;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.out.RenderingService;
import de.peacei.gae.foodsupplier.util.CalendarUtil;
@SuppressWarnings("serial")
@Singleton
public class MensaServlet extends AbstractServlet {
private static final Logger logger = Logger.getLogger(MensaServlet.class.getName());
@Inject
private WeekplanCache weekplanCache;
@Inject
private WeekplanDAO weekplanDAO;
@Inject
private RenderingService renderingService;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int actualWeek = CalendarUtil.getWeek(new Date());
int actualYear = CalendarUtil.getYear(new Date());
String format;
String callback;
int week = actualWeek;
int year = actualYear;
Mensa mensa = null;
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;
}
format = req.getParameter(Finals.FORMAT_PARAM);
if(format == null) format = Finals.FORMAT_XML;
else if(!format.equals(Finals.FORMAT_JSON) && !format.equals(Finals.FORMAT_XML)) {
resp.getOutputStream().print("format not available");
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
callback = req.getParameter(Finals.CALLBACK_PARAM);
try { week = Integer.parseInt(req.getParameter(Finals.WEEK_PARAM));
} catch (Exception ex) { }
try { year = Integer.parseInt(req.getParameter(Finals.YEAR_PARAM));
} catch (Exception ex) { }
if((year == 2011 && week < 24) || year > actualYear || (week > actualWeek && year == actualYear) || year < 2011) {
resp.getOutputStream().print("no data for week "+week+" in year "+year+" available");
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String weekplanStr = null;
if((weekplanStr = weekplanCache.get(format, mensa.getId(), week, year)) != null) {
resp.setCharacterEncoding("UTF-8");
setContentType(resp, format);
resp.setStatus(HttpServletResponse.SC_OK);
OutputStreamWriter osWriter = new OutputStreamWriter(resp.getOutputStream(), "UTF8");
if(callback!=null) weekplanStr = jsWrap(weekplanStr, callback);
osWriter.write(weekplanStr);
osWriter.close();
logger.info("supplied from memcache");
} else {
Weekplan weekplan = weekplanDAO.getWeekplanForMensaWeekAndYear(mensa, week, year);
if (weekplan != null) {
renderingService.renderWeekplan(weekplan, resp.getOutputStream(), format, callback);
setContentType(resp, format);
resp.setCharacterEncoding("UTF-8");
resp.setStatus(HttpServletResponse.SC_OK);
weekplanCache.put(format, weekplan);
logger.info("supplied from datastore");
} else {
resp.getOutputStream().print("there was a problem reading the data");
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
}
private String jsWrap(String content, String callback) {
return callback + '(' + content + ')';
};
}