package foodtruck.server.job;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import foodtruck.dao.TruckDAO;
import foodtruck.truckstops.FoodTruckStopService;
import foodtruck.twitter.TwitterService;
import foodtruck.util.Clock;

/**
 * @author aviolette@gmail.com
 * @since Jul 13, 2011
 */
@Singleton
public class RecacheServlet extends HttpServlet {
  private final FoodTruckStopService service;
  private final Clock clock;
  private final TwitterService twitterService;
  private final TruckDAO truckDAO;

  @Inject
  public RecacheServlet(FoodTruckStopService service, Clock clock,
      TwitterService twitterService, TruckDAO truckDAO) {
    this.twitterService = twitterService;
    this.service = service;
    this.clock = clock;
    this.truckDAO = truckDAO;
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    final String truck = req.getParameter("truck");
    if (!Strings.isNullOrEmpty(truck)) {
      service.updateStopsForTruck(clock.currentDay(), truckDAO.findById(truck));
    } else {
      service.updateStopsFor(clock.currentDay());
    }
    twitterService.twittalyze();
  }
}

@@ -51,16 +51,81 @@
 ception;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -329,16 +329,58 @@
 trings;%0A
+import com.google.common.base.Throwables;%0A
 import c
@@ -441,16 +441,175 @@
 leton;%0A%0A
+import org.joda.time.DateTimeZone;%0Aimport org.joda.time.LocalDate;%0Aimport org.joda.time.format.DateTimeFormat;%0Aimport org.joda.time.format.DateTimeFormatter;%0A%0A
 import f
@@ -1031,16 +1031,151 @@
 ruckDAO;
+%0A  private final DateTimeFormatter timeFormatter;%0A  private static final Logger log = Logger.getLogger(RecacheServlet.class.getName());
 %0A%0A  @Inj
@@ -1299,16 +1299,35 @@
 truckDAO
+, DateTimeZone zone
 ) %7B%0A    
@@ -1446,16 +1446,96 @@
 uckDAO;%0A
+    this.timeFormatter = DateTimeFormat.forPattern(%22YYYYMMdd%22).withZone(zone);%0A%0A
   %7D%0A%0A  @
@@ -1713,16 +1713,104 @@
 ruck%22);%0A
+    final String date = req.getParameter(%22date%22);%0A    LocalDate when = parseDate(date);%0A
     if (
@@ -1876,34 +1876,20 @@
 orTruck(
-clock.currentDay()
+when
 , truckD
@@ -1955,64 +1955,401 @@
 For(
-clock.currentDay());%0A    %7D%0A    twitterService.twittalyze
+when);%0A    %7D%0A    twitterService.twittalyze();%0A  %7D%0A%0A  private LocalDate parseDate(String date) %7B%0A    if (!Strings.isNullOrEmpty(date)) %7B%0A      try %7B%0A        return timeFormatter.parseDateTime(date).toDateMidnight().toLocalDate();%0A      %7D catch (Exception e) %7B%0A        log.log(Level.WARNING, %22Error formatting time%22, e);%0A        Throwables.propagate(e);%0A      %7D%0A    %7D%0A    return clock.currentDay
 ();%0A

Allow specification of a date on the recache servlet