// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
package com.google.sps.servlets;
 
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.common.flogger.FluentLogger;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.sps.data.Trip;
import java.io.IOException;
import java.lang.NullPointerException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/** Servlet that handles data for stops */
@WebServlet("/api/stop")
public final class StopsServlet extends HttpServlet {
  private static final Type ARRAYLIST_STRING = new TypeToken<ArrayList<String>>() {}.getType();
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
 
  private final Gson gson = new Gson();
  private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  // TODO: get key from query string params
  private final Key key = KeyFactory.createKey(Trip.DATASTORE_ENTITY_KIND, 1234);
  private Trip trip;

  /* Passes saved route to be shown in the schedule panel */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
 
    try {
      // trip = Trip.fromEntity(datastore.get(key));
      trip = Trip.getTrip(datastore, (String) request.getParameter("tripKey"));
 
    } catch (NullPointerException e) {
      logger.atInfo().withCause(e).log(
          "Could not retrieve Entity for Trip with key %s while trying to get the stops", key);
      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
 
    response.setContentType("application/json");
    response.getWriter().println(gson.toJson(trip));
  }
 
  /* Modifies the route array of Trip entity in datastore */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ArrayList<String> stops = gson.fromJson(request.getReader(), ARRAYLIST_STRING);
    try {
      trip = Trip.getTrip(datastore, request.getParameter("tripKey"));
 
    } catch (NullPointerException e) {
      logger.atInfo().withCause(e).log(
          "Could not retrieve Entity for Trip with key %s while trying to update the stops", key);
      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
    trip.setRoute(stops);
    datastore.put(trip.toEntity());
    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
  }
}

@@ -581,18 +581,17 @@
 icense.%0A
- 
 %0A
+
 package 
@@ -615,18 +615,17 @@
 rvlets;%0A
- 
 %0A
+
 import c
@@ -747,75 +747,8 @@
 ry;%0A
-import com.google.appengine.api.datastore.EntityNotFoundException;%0A
 impo
@@ -1027,47 +1027,8 @@
 on;%0A
-import java.lang.NullPointerException;%0A
 impo
@@ -1258,18 +1258,17 @@
 sponse;%0A
- 
 %0A
+
 /** Serv
@@ -1556,18 +1556,17 @@
 lass();%0A
- 
 %0A
+
   privat
@@ -2009,18 +2009,17 @@
 ption %7B%0A
- 
 %0A
+
     try 
@@ -2145,34 +2145,33 @@
 er(%22tripKey%22));%0A
- 
 %0A
+
     %7D catch (Nul
@@ -2409,18 +2409,17 @@
 ;%0A    %7D%0A
- 
 %0A
+
     resp
@@ -2516,18 +2516,17 @@
 ));%0A  %7D%0A
- 
 %0A
+
   /* Mod
@@ -2855,17 +2855,16 @@
 Key%22));%0A
- 
 %0A    %7D c

fix Java format error