package foodtruck.server;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
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 org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.joda.time.LocalDate;

import foodtruck.dao.ConfigurationDAO;
import foodtruck.dao.TruckDAO;
import foodtruck.model.Truck;
import foodtruck.truckstops.FoodTruckStopService;
import foodtruck.util.Clock;


/**
 * @author aviolette
 * @since 5/20/13
 */
@Singleton
public class TrucksServlet extends FrontPageServlet {
  private final TruckDAO truckDAO;
  private final FoodTruckStopService stops;
  private final Clock clock;
  private final DateTimeZone zone;

  @Inject
  public TrucksServlet(ConfigurationDAO configurationDAO, TruckDAO trucks, FoodTruckStopService stops,
      Clock clock, DateTimeZone zone) {
    super(configurationDAO);
    this.truckDAO = trucks;
    this.stops = stops;
    this.clock = clock;
    this.zone = zone;
  }

  @Override protected void doGetProtected(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    final String requestURI = req.getRequestURI();
    String truckId = (requestURI.equals("/trucks") || requestURI.equals("/trucks/") ? null : requestURI.substring(8));
    String jsp = "/WEB-INF/jsp/trucks.jsp";
    req = new GuiceHackRequestWrapper(req, jsp);
    req.setAttribute("tab", "trucks");
    final Collection<Truck> trucks = truckDAO.findVisibleTrucks();
    req.setAttribute("trucks", trucks);
    if (!Strings.isNullOrEmpty(truckId)) {
      Truck truck = truckDAO.findById(truckId);
      req.setAttribute("truck", truck);
      LocalDate firstDay = clock.firstDayOfWeek();
      req.setAttribute("stops", stops.findSchedules(truck.getId(), new Interval(firstDay.toDateTimeAtStartOfDay(zone),
          firstDay.toDateTimeAtStartOfDay(zone).plusDays(7))));
    }
    req.setAttribute("containerType", "fixed");
    req.getRequestDispatcher(jsp).forward(req, resp);
  }
}

@@ -1740,49 +1740,270 @@
     
-Truck truck = truckDAO.findById(truckId);
+if (truckId.endsWith(%22/%22)) %7B%0A        truckId = truckId.substring(0, truckId.length() - 1);%0A      %7D%0A      Truck truck = truckDAO.findById(truckId);%0A      if (truck == null) %7B%0A        resp.sendError(404, %22Page cannot be found: %22 + truckId);%0A        return;%0A      %7D
 %0A   

Fix 500 error related to specifying an invalid truck address.