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.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Retrieves a unique sessionID for each user (logged in or not) in order to keep track of their
 * stored BookQuery information
 */
@WebServlet("/id")
public class SessionIdServlet extends HttpServlet {

  UserService userService = createUserService();
  DatastoreService datastore = createDatastore();

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String id;
    String loginUrl = userService.createLoginURL("/index.html");
    String authText;

    if (userService.isUserLoggedIn()) {
      id = userService.getCurrentUser().getUserId();
    } else {
      int prevId = 0;
      Query query = new Query("GuestID");
      PreparedQuery results = datastore.prepare(query);

      int numIds = 0;
      for (Entity entity : results.asIterable()) {
        Long lngValue = (Long) entity.getProperty("idNum");
        prevId = lngValue.intValue();
        datastore.delete(entity.getKey());
        ++numIds;
      }
      Entity entity = new Entity("GuestID");
      entity.setProperty("idNum", prevId + 1);
      datastore.put(entity);
      id = "guest" + Integer.toString(prevId + 1);
    }

    response.getWriter().write(id);
  }

  protected UserService createUserService() {
    return UserServiceFactory.getUserService();
  }

  protected DatastoreService createDatastore() {
    return DatastoreServiceFactory.getDatastoreService();
  }
}

@@ -430,27 +430,45 @@
 ort 
-java.io.IOException
+com.google.sps.utils.BooksMemoryUtils
 ;%0Aim
@@ -480,27 +480,27 @@
 java.io.
-PrintWriter
+IOException
 ;%0Aimport
@@ -691,17 +691,8 @@
 ves 
-a unique 
 sess
@@ -696,16 +696,17 @@
 essionID
+s
  for eac
@@ -762,19 +762,16 @@
 of their
-%0A *
  stored 
@@ -778,25 +778,88 @@
 Book
-Query information
+%0A * information and deletes stored session Book Information when session is over
 %0A */
@@ -1031,16 +1031,285 @@
 ore();%0A%0A
+  /**%0A   * Retrieves a unique sessionID for each user in order to keep track of their stored Book%0A   * information%0A   *%0A   * %3Cp%3EIf the user is logged in, their sessionID is their userID. Otherwise, a unique guest session%0A   * ID is generated for their session ID%0A   */%0A
   @Overr
@@ -1461,147 +1461,17 @@
     
-PrintWriter out = response.getWriter();%0A    String id;%0A    String loginUrl = userService.createLoginURL(%22/index.html%22);%0A    String authText
+String id
 ;%0A%0A 
@@ -1628,21 +1628,23 @@
  Query(%22
-Guest
+Session
 ID%22);%0A  
@@ -1976,13 +1976,15 @@
 ty(%22
-Guest
+Session
 ID%22)
@@ -2118,17 +2118,16 @@
 ;%0A    %7D%0A
-%0A
     resp
@@ -2151,32 +2151,358 @@
 write(id);%0A  %7D%0A%0A
+  /** Deletes all stored Entity in Datastore that match the %22session-id%22 parameter */%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    String sessionID = request.getParameter(%22session-id%22);%0A    BooksMemoryUtils.deleteAllStoredBookInformation(sessionID);%0A  %7D%0A%0A
   protected User

Add ID assigning and deleting after sessions