// 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.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.gson.Gson;
import com.google.maps.model.PlaceType;
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();
  private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  @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 {
    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("interests");
      addInterestsForUser(interestsAsString, userEntity);
    } 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) throws IOException {

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

@@ -789,75 +789,8 @@
 ty;%0A
-import com.google.appengine.api.datastore.EntityNotFoundException;%0A
 impo
@@ -838,24 +838,24 @@
 paredQuery;%0A
+
 import com.g
@@ -1326,16 +1326,19 @@
 e;%0A%0A%0A/**
+%0A *
  Servlet
@@ -1382,16 +1382,121 @@
 ces API.
+%0A * Stores which placeTypes, or interests, user selects%0A * in datastore with their specific Trip Entity.%0A
  */%0A@Web
@@ -2754,22 +2754,28 @@
   Query 
+s
 pe
-rson
+cificTrip
  =%0A     
@@ -2786,20 +2786,20 @@
  Query(%22
-user
+trip
 %22)%0A     
@@ -2913,39 +2913,104 @@
 are(
+s
 pe
-rson
+cificTrip
 );%0A
+%0A
     
-Entity user
+// Guaranteed to have one entity, since id would be unique.%0A    
 Entity 
+trip 
 = pq
@@ -3032,39 +3032,8 @@
 ();%0A
-    if(userEntity != null) %7B%0A  
     
@@ -3098,18 +3098,16 @@
 %22);%0A    
-  
 addInter
@@ -3113,20 +3113,20 @@
 restsFor
-User
+Trip
 (interes
@@ -3141,88 +3141,14 @@
 ng, 
-userEntity);%0A    %7D else %7B%0A      response.setStatus(response.SC_NOT_FOUND);%0A    %7D
+trip);
 %0A  %7D
@@ -3513,20 +3513,20 @@
 restsFor
-User
+Trip
 (String 
@@ -3551,27 +3551,20 @@
  Entity 
-currentUser
+trip
 ) throws
@@ -3668,27 +3668,20 @@
 s);%0A    
-currentUser
+trip
 .setProp
@@ -3728,27 +3728,20 @@
 ore.put(
-currentUser
+trip
 );%0A  %7D%0A%7D

Remove case where entity may be null