// 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.sps.data.SmallCityService;

import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/data")
public class DataServlet extends HttpServlet {
  
  SmallCityService smallCityService;

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // TODO: Return businessList from smallCityService as JSON
  }

    @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // TODO: Create smallCityService instance from mapLocation inputted location communicated through JSON
    // TODO: Convert lat and long maps variable to a mapLocation object
    String latString = request.getParameter("lat");
    String lngString = request.getParameter("lng");
    double lat = Double.parseDouble(latString);
    double lng = Double.parseDouble(lngString);
    System.out.println(lat + ", " + lng);
  }

}

@@ -660,16 +660,92 @@
 Service;
+%0Aimport com.google.sps.data.Listing;%0Aimport com.google.sps.data.MapLocation;
 %0A%0Aimport
@@ -785,24 +785,53 @@
 .util.List;%0A
+import java.util.LinkedList;%0A
 import com.g
@@ -1050,16 +1050,49 @@
 esponse;
+%0Aimport java.util.logging.Logger;
 %0A%0A@WebSe
@@ -1192,16 +1192,89 @@
 Service;
+%0A  private Logger logger = Logger.getLogger(DataServlet.class.getName());
 %0A%0A  @Ove
@@ -1299,10 +1299,11 @@
 d do
-Ge
+Pos
 t(Ht
@@ -1387,74 +1387,446 @@
     
-// TODO: Return businessList from smallCityService as JSON
+String latString = request.getParameter(%22lat%22);%0A    String lngString = request.getParameter(%22lng%22);%0A    double lat = convertToDouble(latString);%0A    double lng = convertToDouble(lngString);%0A    // Google Pittsburgh Office Location (hardcoded prototype)%0A    // Will be populated with lat and lng doubles%0A    MapLocation userLocation = new MapLocation(40.457410, -79.916573);%0A    smallCityService = new SmallCityService(userLocation);
 %0A  %7D%0A%0A
-  
   @O
@@ -1841,35 +1841,34 @@
   public void do
-Pos
+Ge
 t(HttpServletReq
@@ -1940,335 +1940,432 @@
     
-// TODO: Create smallCityService instance from mapLocation inputted location communicated through JSON%0A    // TODO: Convert lat and long maps variable to a mapLocation object%0A    String latString = request.getParameter(%22lat%22);%0A    String lngString = request.getParameter(%22lng%22);%0A    double lat = Double.parseDouble(latString);%0A
+response.setContentType(%22application/json;%22);%0A    response.setCharacterEncoding(%22UTF-8%22);%0A    response.getWriter().println(convertToJson(smallCityService.businesses()));%0A  %7D%0A%0A  private String convertToJson(List%3CListing%3E businesses) %7B%0A    Gson gson = new Gson();%0A    String json = gson.toJson(businesses);%0A    return json;%0A  %7D%0A%0A  private double convertToDouble(String doubleAsString) %7B%0A    double doubleAsDouble;%0A    try %7B%0A  
     
@@ -2370,20 +2370,24 @@
   double
- lng
+AsDouble
  = Doubl
@@ -2396,27 +2396,32 @@
 parseDouble(
-lng
+doubleAs
 String);%0A   
@@ -2425,49 +2425,174 @@
     
-System.out.println(lat + %22, %22 + lng);%0A  %7D%0A
+%7D catch(NumberFormatException e) %7B%0A      logger.warning(%22Location services failure - default set%22);%0A      return 0; // Null Island%0A    %7D%0A    return doubleAsDouble;%0A  %7D
 %0A%7D%0A

Add error checking