/**
*
*/
package de.peacei.gae.foodsupplier.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
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.TaskNameDAO;
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;
/**
* @author peacei
*
*/
@SuppressWarnings("serial")
@Singleton
public class AutomatedUpdateServlet extends HttpServlet {
private static final Logger logger = Logger.getLogger(AutomatedUpdateServlet.class.getName());
@Inject
private TaskNameDAO taskNameDAO;
@Inject
private WeekplanDAO mensaDAO;
@Inject
private WeekplanCache weekplanCache;
@Inject
private MensaParserService mensaParserService;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
StringBuilder respBuilder = new StringBuilder();
String msg = "";
String content = null;
String mensaShortName = req.getParameter("mensa");
msg = "mensaShortName: " + mensaShortName;
logger.info(msg);
respBuilder.append(msg);
Mensa mensa = Mensa.getById(mensaShortName);
//URL url = new URL("http://www.peacei.de/foodsupplier/uniessen.txt");
URL url = new URL(mensa.getUrl());
try {
String agent = "Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.2.16) Gecko/20110323 Ubuntu/10.10 (maverick) Firefox/3.6.16,gzip(gfe)";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", agent);
connection.addRequestProperty("Cache-Control", "no-cache,max-age=0");
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "ISO-8859-1"));
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
content = builder.toString();
//content = new String(content.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e) {
//Queue queue = QueueFactory.getDefaultQueue();
//queue.deleteTask(mensaShortName);
respBuilder.append("<br><br>Exception: "+e.getMessage());
Version.writeHTML(resp, respBuilder.toString());
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// content = new String(content.getBytes("ISO-8859-1"), "UTF-8");
//logger.info(content);
if (content != null && !content.isEmpty()) {
try {
Date now = new Date();
int week = CalendarUtil.getWeek(now);
int year = CalendarUtil.getYear(now);
Weekplan weekplan = mensaParserService.readCurrentWeekplanForMensaWeekAndYear(mensa, week, year, content);
weekplan.setYear(year);
mensaDAO.deleteWeekplansByMensaWeekAndYear(mensa, week, year);
weekplanCache.removeFromCache(Finals.FORMAT_JSON, mensa.getId(), week, year);
weekplanCache.removeFromCache(Finals.FORMAT_XML, mensa.getId(), week, year);
mensaDAO.saveWeekplan(weekplan);
logger.info("data parsed and written");
respBuilder.append("<br>data parsed");
respBuilder.append("<br>data written");
taskNameDAO.deleteTaskName(mensaShortName);
} catch (Exception e) {
logger.info("failed to parse and write data");
logger.severe(e.getClass()+": "+e.getLocalizedMessage());
respBuilder.append("<br>failed to parse and write data");
}
}
else {
logger.info("no update started");
respBuilder.append("<br>no update started");
}
Version.writeHTML(resp, respBuilder.toString());
resp.setStatus(HttpServletResponse.SC_OK);
/*
try {
URL url = new URL("http://www.studentenwerk.bremen.de/files/main_info/essen/plaene/uniessen.php");
URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
HTTPRequest innerReq = new HTTPRequest(url);
HTTPHeader header = new HTTPHeader("Cache-Control", "no-cache,max-age=0");
innerReq.addHeader(header);
HTTPResponse innerResp = fetcher.fetch(innerReq);
String html = new String(innerResp.getContent());
resp.getWriter().println(html);
} catch (Exception e) {
resp.getWriter().println("failed");
e.printStackTrace();
}
*/
}
}