<commit_before>package foodtruck.server.dashboard;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

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

import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;

import foodtruck.dao.LocationDAO;
import foodtruck.dao.TruckDAO;
import foodtruck.dao.TruckStopDAO;
import foodtruck.model.Location;
import foodtruck.model.TruckStop;
import foodtruck.server.GuiceHackRequestWrapper;
import foodtruck.util.Clock;
import foodtruck.util.MilitaryTimeOnlyFormatter;

/**
 * @author aviolette
 * @since 1/8/15
 */
@Singleton
public class CompoundEventServlet extends HttpServlet {
  private static final Logger log = Logger.getLogger(CompoundEventServlet.class.getName());
  private static final String JSP_PATH = "/WEB-INF/jsp/dashboard/compoundEvent.jsp";
  private final TruckDAO truckDAO;
  private final LocationDAO locationDAO;
  private final DateTimeFormatter timeFormatter;
  private final Clock clock;
  private final TruckStopDAO truckStopDAO;
  private final Provider<Calendar> calendarProvider;

  @Inject
  public CompoundEventServlet(TruckDAO truckDAO, LocationDAO locationDAO,
      @MilitaryTimeOnlyFormatter DateTimeFormatter formatter, Clock clock, TruckStopDAO truckStopDAO, Provider<Calendar> calendarProvider) {
    this.truckDAO = truckDAO;
    this.locationDAO = locationDAO;
    this.timeFormatter = formatter;
    this.clock = clock;
    this.truckStopDAO = truckStopDAO;
    this.calendarProvider = calendarProvider;
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String startTimeParam = req.getParameter("startTime"),
        endTimeParam = req.getParameter("endTime"),
        locationID = req.getRequestURI().substring(req.getRequestURI().lastIndexOf('/') + 1);
    String[] trucks = req.getParameterValues("trucks");
    Location location = locationDAO.findById(Long.valueOf(locationID));
    if (location == null) {
      resp.sendError(404);
      return;
    }
    LocalDate now = clock.currentDay();
    DateTime startTime = now.toDateTime(timeFormatter.parseLocalTime(startTimeParam), clock.zone()),
        endTime = now.toDateTime(timeFormatter.parseLocalTime(endTimeParam), clock.zone());
    Calendar calendar = calendarProvider.get();
    for (String truckId : trucks) {
      TruckStop stop = TruckStop.builder()
          .location(location)
          .startTime(startTime)
          .endTime(endTime)
          .truck(truckDAO.findById(truckId))
          .build();
      truckStopDAO.save(stop);
      try {
        Event event = new Event();
        event.setLocation(location.getName());
        event.setSummary(truckId);
        EventDateTime dt = new EventDateTime();
        dt.setDateTime(new com.google.api.client.util.DateTime(startTime.getMillis()));
        event.setStart(dt);
        dt = new EventDateTime();
        dt.setDateTime(new com.google.api.client.util.DateTime(endTime.getMillis()));
        event.setEnd(dt);
        Event createdEvent = calendar.events().insert(truckId, event).execute();
        log.log(Level.FINE, "Created event {}", createdEvent.getId());
      } catch (Exception e) {
        log.log(Level.WARNING, e.getMessage(), e);
      }
    }
    resp.sendRedirect("/admin/trucks");
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String locationID = req.getRequestURI().substring(req.getRequestURI().lastIndexOf('/') + 1);
    Location location = locationDAO.findById(Long.valueOf(locationID));
    if (location == null) {
      resp.sendError(404);
      return;
    }
    req.setAttribute("location", location);
    req.setAttribute("trucks", truckDAO.findActiveTrucks());
    req.setAttribute("nav", "location");
    req = new GuiceHackRequestWrapper(req, JSP_PATH);
    req.getRequestDispatcher(JSP_PATH).forward(req, resp);
  }
}<commit_msg>Fixed bug where calendar wasn't being inserted with correct calendar ID<commit_after>package foodtruck.server.dashboard;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

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

import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;

import foodtruck.dao.ConfigurationDAO;
import foodtruck.dao.LocationDAO;
import foodtruck.dao.TruckDAO;
import foodtruck.dao.TruckStopDAO;
import foodtruck.model.Location;
import foodtruck.model.TruckStop;
import foodtruck.server.GuiceHackRequestWrapper;
import foodtruck.util.Clock;
import foodtruck.util.MilitaryTimeOnlyFormatter;

/**
 * @author aviolette
 * @since 1/8/15
 */
@Singleton
public class CompoundEventServlet extends HttpServlet {
  private static final Logger log = Logger.getLogger(CompoundEventServlet.class.getName());
  private static final String JSP_PATH = "/WEB-INF/jsp/dashboard/compoundEvent.jsp";
  private final TruckDAO truckDAO;
  private final LocationDAO locationDAO;
  private final DateTimeFormatter timeFormatter;
  private final Clock clock;
  private final TruckStopDAO truckStopDAO;
  private final Provider<Calendar> calendarProvider;
  private final ConfigurationDAO configDAO;

  @Inject
  public CompoundEventServlet(TruckDAO truckDAO, LocationDAO locationDAO, ConfigurationDAO configDAO,
      @MilitaryTimeOnlyFormatter DateTimeFormatter formatter, Clock clock, TruckStopDAO truckStopDAO, Provider<Calendar> calendarProvider) {
    this.truckDAO = truckDAO;
    this.locationDAO = locationDAO;
    this.timeFormatter = formatter;
    this.clock = clock;
    this.truckStopDAO = truckStopDAO;
    this.calendarProvider = calendarProvider;
    this.configDAO = configDAO;
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String startTimeParam = req.getParameter("startTime"),
        endTimeParam = req.getParameter("endTime"),
        locationID = req.getRequestURI().substring(req.getRequestURI().lastIndexOf('/') + 1);
    String[] trucks = req.getParameterValues("trucks");
    Location location = locationDAO.findById(Long.valueOf(locationID));
    if (location == null) {
      resp.sendError(404);
      return;
    }
    LocalDate now = clock.currentDay();
    DateTime startTime = now.toDateTime(timeFormatter.parseLocalTime(startTimeParam), clock.zone()),
        endTime = now.toDateTime(timeFormatter.parseLocalTime(endTimeParam), clock.zone());
    Calendar calendar = calendarProvider.get();
    String calendarID = configDAO.find().getGoogleCalendarAddress();
    for (String truckId : trucks) {
      TruckStop stop = TruckStop.builder()
          .location(location)
          .startTime(startTime)
          .endTime(endTime)
          .truck(truckDAO.findById(truckId))
          .build();
      truckStopDAO.save(stop);
      try {
        Event event = new Event();
        event.setLocation(location.getName());
        event.setSummary(truckId);
        EventDateTime dt = new EventDateTime();
        dt.setDateTime(new com.google.api.client.util.DateTime(startTime.getMillis()));
        event.setStart(dt);
        dt = new EventDateTime();
        dt.setDateTime(new com.google.api.client.util.DateTime(endTime.getMillis()));
        event.setEnd(dt);
        Event createdEvent = calendar.events().insert(calendarID, event).execute();
        log.log(Level.FINE, "Created event {}", createdEvent.getId());
      } catch (Exception e) {
        log.log(Level.WARNING, e.getMessage(), e);
      }
    }
    resp.sendRedirect("/admin/trucks");
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String locationID = req.getRequestURI().substring(req.getRequestURI().lastIndexOf('/') + 1);
    Location location = locationDAO.findById(Long.valueOf(locationID));
    if (location == null) {
      resp.sendError(404);
      return;
    }
    req.setAttribute("location", location);
    req.setAttribute("trucks", truckDAO.findActiveTrucks());
    req.setAttribute("nav", "location");
    req = new GuiceHackRequestWrapper(req, JSP_PATH);
    req.getRequestDispatcher(JSP_PATH).forward(req, resp);
  }
}
{'fraction_non_alphanumeric': 0.1109650564136269, 'fraction_numerical': 0.0026289845547157412, 'mean_word_length': 4.286624203821656, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '258199', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2649, 'n_words': 517}