// 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.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/load-event")
public class LoadEventServlet extends HttpServlet {

  private static final Logger LOGGER = Logger.getLogger(LoadEventServlet.class.getName());

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      Key keyRequested = getEventKey(request);
      if (keyRequested == null) {
        throw new IOException("Could not retrieve event key.");
      } else {
        Query query = new Query("Event", keyRequested);

        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity eventRequested = datastore.prepare(query).asSingleEntity();
        request = populateRequest(request, eventRequested);

        request.getRequestDispatcher("/WEB-INF/jsp/display-event.jsp").forward(request, response);
      }
    } catch (Exception e) {
      LOGGER.info("Could not retrieve event " + e);
      request.getRequestDispatcher("/WEB-INF/jsp/event-not-found.jsp").forward(request, response);
    }
  }

  /** @return the request with attributes set. */
  private HttpServletRequest populateRequest(HttpServletRequest request, Entity event) {
    String name = (String) event.getProperty("eventName");
    String description = (String) event.getProperty("eventDescription");
    String date = (String) event.getProperty("date");
    String start = (String) event.getProperty("startTime");
    String end = (String) event.getProperty("endTime");
    String street = (String) event.getProperty("streetAddress");
    String city = (String) event.getProperty("city");
    String state = (String) event.getProperty("state");

    request.setAttribute("name", name);
    request.setAttribute("description", description);
    request.setAttribute("date", date);
    request.setAttribute("start", start);
    request.setAttribute("end", end);
    request.setAttribute("street", street);
    request.setAttribute("city", city);
    request.setAttribute("state", state);

    return request;
  }

  /** @return the key from the request parameter. */
  private Key getEventKey(HttpServletRequest request) throws IllegalArgumentException, IOException {
    Key eventKey = null;
    // Get the string from the request.
    try {
      if (request.getParameter("Event") != null) {
        String eventKeyString = request.getParameter("Event");
        if (eventKeyString instanceof String) {
          // Convert String to type Key.
          eventKey = KeyFactory.stringToKey(eventKeyString);
        } else {
          throw new IllegalArgumentException("Not a valid key");
        }
      } else {
        throw new IOException("Request is missing parameter");
      }
    } catch (Exception e) {
      LOGGER.info("Could not retrieve event key: " + e);
    }
    return eventKey;
  }
}

@@ -1523,24 +1523,46 @@
 Exception %7B%0A
+    Key keyRequested;%0A
     try %7B%0A  
@@ -1561,28 +1561,24 @@
 try %7B%0A      
-Key 
 keyRequested
@@ -1625,17 +1625,17 @@
 quested 
-=
+!
 = null) 
@@ -1640,87 +1640,8 @@
 ) %7B%0A
-        throw new IOException(%22Could not retrieve event key.%22);%0A      %7D else %7B%0A
     
@@ -3412,28 +3412,16 @@
 equest.%0A
-    try %7B%0A  
     if (
@@ -3457,26 +3457,24 @@
  != null) %7B%0A
-  
       String
@@ -3526,60 +3526,9 @@
 %22);%0A
-        if (eventKeyString instanceof String) %7B%0A    
+%0A
     
@@ -3566,20 +3566,16 @@
 .%0A      
-    
 eventKey
@@ -3617,20 +3617,16 @@
 tring);%0A
-    
     %7D el
@@ -3640,245 +3640,60 @@
     
-    throw new IllegalArgumentException(%22Not a valid key%22);%0A        %7D%0A      %7D else %7B%0A        throw new IOException(%22Request is missing parameter%22);%0A      %7D%0A    %7D catch (Exception e) %7B%0A      LOGGER.info(%22Could not retrieve event key: %22 + e
+throw new IOException(%22Request is missing parameter%22
 );%0A 

Remove catch blocks inside getEventKey