// 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.maps;

import com.google.maps.model.LatLng;
import com.google.maps.model.PlaceType;
import com.google.gson.Gson;
import java.io.IOException;
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 returns all PlaceTypes from Places API. */
@WebServlet("/api/places")
public final class PlacesServlet extends HttpServlet {

  private final Gson gson = new Gson();

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ArrayList<String> places = new ArrayList<>();
    for(PlaceType location: PlaceType.values()) {
      String place = formatLocation(location);
      places.add(place);
    }
    response.setContentType("application/json;");
    response.getWriter().println(gson.toJson(places));
  }

  /** 
   * Format string by capitalizing and adding spaces in-between words.
   * @param location is a PlaceType that indicates a type of location/interest.
   * @return is the modified location name.
   */
  private String formatLocation(PlaceType location) {
    String place = location.toString();
    place = place.substring(0,1).toUpperCase() + place.substring(1);
    place = place.replace('_', ' ');
    return place;
  }

  /**
   * Temporary setup to find entity with hard-coded id value. Set their interests.
   */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query person =
      new Query("user")
        .setFilter(new FilterPredicate("id", FilterOperator.EQUAL, 2452));
    PreparedQuery pq = datastore.prepare(person);
    Entity userEntity = pq.asSingleEntity();
    if(userEntity != null) {
      String interestsAsString = request.getParameter("data");
      addInterestsForUser(interestsAsString, userEntity, datastore);
    } else {
      response.setStatus(response.SC_NOT_FOUND);
    }
  }


  /**
   * Creates a list of interests from the JSON string passed in and adds it
   * to this user's Entity object as a separate property.
   * @param interestsAsString JSON String containing interests.
   * @param currentUser       Entity with user information.
   * @param datastore         DatastoreService with all users.
   */
  private void addInterestsForUser(String interestsAsString, Entity currentUser,
                                    DatastoreService datastore) throws IOException {

    ArrayList<String> interests = gson.fromJson(interestsAsString, ArrayList.class);
    currentUser.setProperty("interests", interests);
    datastore.put(currentUser);
  }
}

@@ -630,25 +630,232 @@
 gle.
-maps.model.LatLng
+appengine.api.datastore.DatastoreService;%0Aimport com.google.appengine.api.datastore.DatastoreServiceFactory;%0Aimport com.google.appengine.api.datastore.Entity;%0Aimport com.google.appengine.api.datastore.EntityNotFoundException
 ;%0Aim
@@ -874,57 +874,292 @@
 gle.
-m
 ap
-s.model.PlaceType;%0Aimport com.google.gson.Gson
+pengine.api.datastore.PreparedQuery;%0Aimport com.google.appengine.api.datastore.Query;%0Aimport com.google.appengine.api.datastore.Query.FilterPredicate;%0Aimport com.google.appengine.api.datastore.Query.FilterOperator;%0Aimport com.google.gson.Gson;%0Aimport com.google.maps.model.PlaceType
 ;%0Aim
@@ -1389,16 +1389,17 @@
 ponse;%0A%0A
+%0A
 /** Serv

Add imports to use other types